Skip to content

Commit

Permalink
fix(actions): Fixed plugin targets not found (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs authored Feb 9, 2024
2 parents 6ed1bdd + 47e698d commit f4fb983
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 48 deletions.
38 changes: 20 additions & 18 deletions actions/plan/src/plan.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
import * as core from '@actions/core'
import { FsTree } from 'nx/src/generators/tree'
import { getProjects } from 'nx/src/generators/utils/project-configuration'
import { readCachedProjectGraph } from 'nx/src/project-graph/project-graph'
import { resolve } from 'path'

import { buildCommand } from './utils/build-command'
import { execCommand } from './utils/exec'
import { cleanLogConditions, hasOneOfRequiredTags } from './utils/has-one-of-required-tags'

async function run() {
try {
const nxTree = new FsTree(process.cwd(), core.isDebug())
const projects = getProjects(nxTree)

const workingDirectory = core.getInput('workingDirectory') || ''
const affectedOnly = core.getBooleanInput('affectedOnly')
const targets = core.getMultilineInput('targets', { required: true, trimWhitespace: true })

const cwd = resolve(process.cwd(), workingDirectory)

// Get all the project names
const projectsNamesToPlanFor = affectedOnly
? JSON.parse(execCommand<string>(
'npx nx show projects --affected --json',
{
asString: true,
silent: !core.isDebug(),
cwd
}
)).map((projectName: string) => projectName.trim())
: Array.from(projects.keys())
const projectsNamesToPlanFor = JSON.parse(execCommand<string>(
buildCommand([
'npx nx show projects --json',
affectedOnly && '--affected'
]),
{
asString: true,
silent: !core.isDebug(),
cwd
}
))

// Make sure to still log the project names
if (!affectedOnly) {
core.debug(JSON.stringify(projectsNamesToPlanFor))
}

const projectGraph = readCachedProjectGraph()

// Get all affected projects
const enabledProjects = projectsNamesToPlanFor.filter((projectName: string) => {
if (!projects.has(projectName)) {
const project = projectGraph.nodes?.[projectName]?.data

if (!project) {
return false
}

return !(projects.get(projectName).tags || []).includes('ci=off')
return !(project.tags || []).includes('ci=off')
})

const matrixInclude = []
Expand All @@ -60,7 +62,7 @@ async function run() {
core.info(`- ${target}PostTargets: ${postTargets.join(' AND ')}`)

const amountOfProjectsWithTarget = enabledProjects.map((projectName: string) => {
const { targets, tags } = projects.get(projectName)
const { targets, tags } = projectGraph.nodes[projectName].data

if (Object.keys(targets).includes(target)) {
return hasOneOfRequiredTags(projectName, tags, tagConditions)
Expand Down
5 changes: 5 additions & 0 deletions actions/plan/src/utils/build-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Part = string | boolean;

export const buildCommand = (parts: Part[]): string => {
return parts.filter(Boolean).join(' ')
}
44 changes: 21 additions & 23 deletions actions/run-many/src/run-many.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as core from '@actions/core'
import { FsTree } from 'nx/src/generators/tree'
import { getProjects } from 'nx/src/generators/utils/project-configuration'
import { readCachedProjectGraph } from 'nx/src/project-graph/project-graph'
import { resolve } from 'path'
import { hideBin } from 'yargs/helpers'
import yargs from 'yargs/yargs'
Expand All @@ -23,9 +22,6 @@ export const argv = yargs(hideBin(process.argv))

async function run() {
try {
const nxTree = new FsTree(process.cwd(), (core.isDebug() || argv.verbose))
const projects = getProjects(nxTree)

// Get all options
const tagConditions = (argv.tag ? [argv.tag] : core.getMultilineInput('tag', { trimWhitespace: true }))
const target = core.getInput('target', { required: !argv.target }) || argv.target
Expand Down Expand Up @@ -54,32 +50,34 @@ async function run() {

const cwd = resolve(process.cwd(), workingDirectory)

const projectsNamesToRun = affectedOnly
? JSON.parse(execCommand<string>(
buildCommand([
'npx nx show projects --affected --json',
`-t ${target}`
]),
{
asString: true,
silent: !(core.isDebug() || argv.verbose),
cwd
}
))
: Array.from(projects.keys())
const projectsNamesToRun = JSON.parse(execCommand<string>(
buildCommand([
'npx nx show projects --json',
affectedOnly && '--affected',
`-t ${target}`
]),
{
asString: true,
silent: !(core.isDebug() || argv.verbose),
cwd
}
))

// Make sure to still log the project names
if (!affectedOnly) {
core.debug(JSON.stringify(projectsNamesToRun))
}

const projectGraph = readCachedProjectGraph()

// Get all affected projects
const projectsToRun = projectsNamesToRun.filter((projectName: string) => {
if (!projects.has(projectName)) {
const project = projectGraph.nodes?.[projectName]?.data

if (!project) {
return false
}

const project = projects.get(projectName)
const tags = project.tags || []

// If the project has ci=off then don't run it
Expand Down Expand Up @@ -112,7 +110,7 @@ async function run() {
const [target, targetConfig] = targetParts.split(':')
await runTarget(
cwd,
projects,
projectGraph.nodes,
runProjects,
target,
targetConfig,
Expand All @@ -123,7 +121,7 @@ async function run() {

await runTarget(
cwd,
projects,
projectGraph.nodes,
runProjects,
target,
config,
Expand All @@ -138,7 +136,7 @@ async function run() {
const [target, targetConfig] = targetParts.split(':')
await runTarget(
cwd,
projects,
projectGraph.nodes,
runProjects,
target,
targetConfig,
Expand Down
10 changes: 5 additions & 5 deletions actions/run-many/src/utils/get-projects-with-target.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json'
import type { ProjectGraphProjectNode } from 'nx/src/config/project-graph'

export function getProjectsWithTarget(projects: Map<string, ProjectConfiguration>, runProjects: string[], target: string): string[] {
export function getProjectsWithTarget(projects: Record<string, ProjectGraphProjectNode>, runProjects: string[], target: string): string[] {
// Filter out all projects that are not allowed
return Array.from(projects).filter(([project, config]) => {
return Object.keys(projects).filter((project) => {
// Check if the project has the provided target
return Object.keys(config?.targets ?? {}).includes(target)
return Object.keys(projects[project].data?.targets ?? {}).includes(target)
&& runProjects.includes(project)
}).map(([project]) => project)
}).map((project) => project)
}
4 changes: 2 additions & 2 deletions actions/run-many/src/utils/run-target.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'

import type { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json'
import type { ProjectGraphProjectNode } from 'nx/src/config/project-graph'

import { argv } from '../run-many'
import { buildCommand } from './build-command'
Expand All @@ -11,7 +11,7 @@ import { getRestArgs } from './get-rest-args'

export async function runTarget(
cwd: string,
projects: Map<string, ProjectConfiguration>,
projects: Record<string, ProjectGraphProjectNode>,
runProjects: string[],
target: string,
config?: string,
Expand Down

0 comments on commit f4fb983

Please sign in to comment.