Handle the case where branches may be strings, including "*"

This commit is contained in:
Simon Engledew 2020-12-01 10:42:21 +00:00
parent c6dbd5a9bf
commit ac1c081de8
No known key found for this signature in database
GPG key ID: 84302E7B02FE8BCE
6 changed files with 66 additions and 13 deletions

View file

@ -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 },

View file

@ -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 (doc.on.pull_request) {
const pull_request = doc.on.pull_request.branches || [];
if (push !== "*") {
const pull_request = branchesToArray(doc.on.pull_request?.branches);
if (pull_request !== "*") {
const difference = pull_request.filter(
(value) => !push.includes(value)
);