Skip to content

Commit 3792ed8

Browse files
Merge branch 'main' into robertbrignull/recursive_sarif_test
2 parents bd4e3ad + b1e0b46 commit 3792ed8

6 files changed

Lines changed: 128 additions & 10 deletions

File tree

lib/actions-util.js

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.test.js

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions-util.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ test("validateWorkflow() when on.pull_request for mismatched wildcard branches",
336336
});
337337

338338
test("validateWorkflow() when HEAD^2 is checked out", (t) => {
339+
process.env.GITHUB_JOB = "test";
340+
339341
const errors = actionsutil.validateWorkflow({
340342
on: ["push", "pull_request"],
341343
jobs: { test: { steps: [{ run: "git checkout HEAD^2" }] } },
@@ -432,3 +434,61 @@ on:
432434

433435
t.deepEqual(errors, []);
434436
});
437+
438+
test("validateWorkflow() should only report the current job's CheckoutWrongHead", (t) => {
439+
process.env.GITHUB_JOB = "test";
440+
441+
const errors = actionsutil.validateWorkflow(
442+
yaml.safeLoad(`
443+
name: "CodeQL"
444+
on:
445+
push:
446+
branches: [master]
447+
pull_request:
448+
# The branches below must be a subset of the branches above
449+
branches: [master]
450+
jobs:
451+
test:
452+
steps:
453+
- run: "git checkout HEAD^2"
454+
455+
test2:
456+
steps:
457+
- run: "git checkout HEAD^2"
458+
459+
test3:
460+
steps: []
461+
`)
462+
);
463+
464+
t.deepEqual(errors, [actionsutil.WorkflowErrors.CheckoutWrongHead]);
465+
});
466+
467+
test("validateWorkflow() should not report a different job's CheckoutWrongHead", (t) => {
468+
process.env.GITHUB_JOB = "test3";
469+
470+
const errors = actionsutil.validateWorkflow(
471+
yaml.safeLoad(`
472+
name: "CodeQL"
473+
on:
474+
push:
475+
branches: [master]
476+
pull_request:
477+
# The branches below must be a subset of the branches above
478+
branches: [master]
479+
jobs:
480+
test:
481+
steps:
482+
- run: "git checkout HEAD^2"
483+
484+
test2:
485+
steps:
486+
- run: "git checkout HEAD^2"
487+
488+
test3:
489+
steps: []
490+
`)
491+
);
492+
493+
t.deepEqual(errors, []);
494+
});

src/actions-util.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,23 @@ export const WorkflowErrors = toCodedErrors({
211211
export function validateWorkflow(doc: Workflow): CodedError[] {
212212
const errors: CodedError[] = [];
213213

214-
// .jobs[key].steps[].run
215-
for (const job of Object.values(doc?.jobs || {})) {
216-
if (Array.isArray(job?.steps)) {
217-
for (const step of job?.steps) {
214+
const jobName = process.env.GITHUB_JOB;
215+
216+
if (jobName) {
217+
const job = doc?.jobs?.[jobName];
218+
219+
const steps = job?.steps;
220+
221+
if (Array.isArray(steps)) {
222+
for (const step of steps) {
218223
// this was advice that we used to give in the README
219224
// we actually want to run the analysis on the merge commit
220225
// to produce results that are more inline with expectations
221226
// (i.e: this is what will happen if you merge this PR)
222227
// and avoid some race conditions
223228
if (step?.run === "git checkout HEAD^2") {
224229
errors.push(WorkflowErrors.CheckoutWrongHead);
230+
break;
225231
}
226232
}
227233
}

0 commit comments

Comments
 (0)