Skip to content

Commit

Permalink
Only check the steps of the job currently being run
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Engledew committed Jan 4, 2021
1 parent e89a24b commit 1511db3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
12 changes: 7 additions & 5 deletions lib/actions-util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.js.map

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions lib/actions-util.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.test.js.map

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions src/actions-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ test("validateWorkflow() when on.pull_request for mismatched wildcard branches",
});

test("validateWorkflow() when HEAD^2 is checked out", (t) => {
process.env.GITHUB_JOB = "test";

const errors = actionsutil.validateWorkflow({
on: ["push", "pull_request"],
jobs: { test: { steps: [{ run: "git checkout HEAD^2" }] } },
Expand Down Expand Up @@ -434,6 +436,8 @@ on:
});

test("validateWorkflow() should only report the first CheckoutWrongHead", (t) => {
process.env.GITHUB_JOB = "test";

const errors = actionsutil.validateWorkflow(
yaml.safeLoad(`
name: "CodeQL"
Expand All @@ -447,9 +451,13 @@ jobs:
test:
steps:
- run: "git checkout HEAD^2"
test2:
steps:
- run: "git checkout HEAD^2"
- run: "git checkout HEAD^2"
- run: "git checkout HEAD^2"
test3:
steps:
- run: "git checkout HEAD^2"
`)
);
Expand Down
15 changes: 10 additions & 5 deletions src/actions-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,23 @@ export const WorkflowErrors = toCodedErrors({
export function validateWorkflow(doc: Workflow): CodedError[] {
const errors: CodedError[] = [];

// .jobs[key].steps[].run
outer: for (const job of Object.values(doc?.jobs || {})) {
if (Array.isArray(job?.steps)) {
for (const step of job?.steps) {
const jobName = process.env.GITHUB_JOB;

if (jobName) {
const job = doc?.jobs?.[jobName];

const steps = job?.steps;

if (Array.isArray(steps)) {
for (const step of steps) {
// this was advice that we used to give in the README
// we actually want to run the analysis on the merge commit
// to produce results that are more inline with expectations
// (i.e: this is what will happen if you merge this PR)
// and avoid some race conditions
if (step?.run === "git checkout HEAD^2") {
errors.push(WorkflowErrors.CheckoutWrongHead);
break outer;
break;
}
}
}
Expand Down

0 comments on commit 1511db3

Please sign in to comment.