Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle the case where branches may be strings, including "*"
Simon Engledew committed Dec 1, 2020

Unverified

No user is associated with the committer email.
1 parent c6dbd5a commit ac1c081
Showing 6 changed files with 66 additions and 13 deletions.
22 changes: 17 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.

12 changes: 12 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.

16 changes: 16 additions & 0 deletions src/actions-util.test.ts
@@ -143,6 +143,22 @@ test("validateWorkflow() when on.push is a correct object", (t) => {
t.deepEqual(errors.length, 0);
});

test("validateWorkflow() when on.pull_requests is a string", (t) => {
const errors = actionsutil.validateWorkflow({
on: { push: { branches: ["main"] }, pull_request: { branches: "*" } },
});

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

test("validateWorkflow() when on.pull_requests is a string and correct", (t) => {
const errors = actionsutil.validateWorkflow({
on: { push: { branches: "*" }, pull_request: { branches: "*" } },
});

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

test("validateWorkflow() when on.push is correct with empty objects", (t) => {
const errors = actionsutil.validateWorkflow({
on: { push: undefined, pull_request: undefined },
25 changes: 19 additions & 6 deletions src/actions-util.ts
@@ -111,7 +111,7 @@ interface WorkflowJob {
}

interface WorkflowTrigger {
branches?: string[];
branches?: string[] | string;
paths?: string[];
}

@@ -134,6 +134,19 @@ function isObject(o: unknown): o is object {
return o !== null && typeof o === "object";
}

function branchesToArray(branches?: string | null | string[]): string[] | "*" {
if (typeof branches === 'string') {
if (branches === "*") {
return "*";
}
return [branches];
}
if (!branches || branches.length === 0) {
return "*";
}
return branches;
}

enum MissingTriggers {
None = 0,
Push = 1,
@@ -144,7 +157,6 @@ interface CodedError {
message: string;
code: string;
}

function toCodedErrors(errors: {
[key: string]: string;
}): { [key: string]: CodedError } {
@@ -224,11 +236,12 @@ export function validateWorkflow(doc: Workflow): CodedError[] {
}
}

if (doc.on.push) {
const push = doc.on.push.branches || [];
const push = branchesToArray(doc.on.push?.branches);

if (push !== "*") {
const pull_request = branchesToArray(doc.on.pull_request?.branches);

if (doc.on.pull_request) {
const pull_request = doc.on.pull_request.branches || [];
if (pull_request !== "*") {
const difference = pull_request.filter(
(value) => !push.includes(value)
);

0 comments on commit ac1c081

Please sign in to comment.