Skip to content

Commit

Permalink
Workflow triggers are null if unspecified
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Engledew committed Nov 24, 2020
1 parent c0bd7b0 commit 754f502
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
15 changes: 10 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.

9 changes: 9 additions & 0 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

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

11 changes: 11 additions & 0 deletions src/actions-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ test("validateWorkflow() when on.push is mismatched for pull_request", (t) => {
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
});

test("validateWorkflow() when on.pull_request for every branch but push specifies branches", (t) => {
const errors = actionsutil.validateWorkflow({
on: {
push: { branches: ["main"] },
pull_request: null,
},
});

t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
});

test("validateWorkflow() when HEAD^2 is checked out", (t) => {
const errors = actionsutil.validateWorkflow({
on: ["push", "pull_request"],
Expand Down
20 changes: 13 additions & 7 deletions src/actions-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ interface WorkflowTrigger {
}

interface WorkflowTriggers {
push?: WorkflowTrigger;
pull_request?: WorkflowTrigger;
push?: WorkflowTrigger | null;
pull_request?: WorkflowTrigger | null;
}

interface Workflow {
Expand All @@ -136,7 +136,7 @@ enum MissingTriggers {
}

export const ErrCheckoutWrongHead = `Git checkout HEAD^2 is no longer necessary. Please remove this line.`;
export const ErrMismatchedBranches = `Please make sure that any branches in on.pull_request are also in on.push so that CodeQL can establish a baseline.`;
export const ErrMismatchedBranches = `Please make sure that every branch in on.pull_request is also in on.push so that CodeQL can establish a baseline.`;
export const ErrMissingHooks = `Please specify on.push and on.pull_request hooks.`;
export const ErrMissingPushHook = `Please specify an on.push hook so CodeQL can establish a baseline.`;
export const ErrMissingPullRequestHook = `Please specify an on.pull_request hook so CodeQL is run against new pull requests.`;
Expand Down Expand Up @@ -190,13 +190,19 @@ export function validateWorkflow(doc: Workflow): string[] {
}
}

if (doc.on.pull_request !== undefined && doc.on.push !== undefined) {
if (doc.on.push) {
const push = doc.on.push.branches || [];
const pull_request = doc.on.pull_request.branches || [];
if (doc.on.pull_request) {
const pull_request = doc.on.pull_request.branches || [];

const intersects = pull_request.filter((value) => !push.includes(value));
const intersects = pull_request.filter(
(value) => !push.includes(value)
);

if (intersects.length > 0) {
if (intersects.length > 0) {
errors.push(ErrMismatchedBranches);
}
} else if (push.length > 0) {
errors.push(ErrMismatchedBranches);
}
}
Expand Down

0 comments on commit 754f502

Please sign in to comment.