Handle the case where branches may be strings, including "*"
This commit is contained in:
parent
c6dbd5a9bf
commit
ac1c081de8
6 changed files with 66 additions and 13 deletions
|
|
@ -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 },
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue