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

22
lib/actions-util.js generated
View file

@ -101,6 +101,18 @@ exports.getCommitOid = async function () {
function isObject(o) {
return o !== null && typeof o === "object";
}
function branchesToArray(branches) {
if (typeof branches === 'string') {
if (branches === "*") {
return "*";
}
return [branches];
}
if (!branches || branches.length === 0) {
return "*";
}
return branches;
}
var MissingTriggers;
(function (MissingTriggers) {
MissingTriggers[MissingTriggers["None"] = 0] = "None";
@ -123,7 +135,7 @@ exports.WorkflowErrors = toCodedErrors({
CheckoutWrongHead: `git checkout HEAD^2 is no longer necessary. Please remove this step as Code Scanning recommends analyzing the merge commit for best results.`,
});
function validateWorkflow(doc) {
var _a, _b, _c, _d, _e;
var _a, _b, _c, _d, _e, _f, _g;
const errors = [];
// .jobs[key].steps[].run
for (const job of Object.values(((_a = doc) === null || _a === void 0 ? void 0 : _a.jobs) || {})) {
@ -183,10 +195,10 @@ function validateWorkflow(doc) {
errors.push(exports.WorkflowErrors.PathsIgnoreSpecified);
}
}
if (doc.on.push) {
const push = doc.on.push.branches || [];
if (doc.on.pull_request) {
const pull_request = doc.on.pull_request.branches || [];
const push = branchesToArray((_f = doc.on.push) === null || _f === void 0 ? void 0 : _f.branches);
if (push !== "*") {
const pull_request = branchesToArray((_g = doc.on.pull_request) === null || _g === void 0 ? void 0 : _g.branches);
if (pull_request !== "*") {
const difference = pull_request.filter((value) => !push.includes(value));
if (difference.length > 0) {
// there are branches in pull_request that may not have a baseline

File diff suppressed because one or more lines are too long

View file

@ -112,6 +112,18 @@ ava_1.default("validateWorkflow() when on.push is a correct object", (t) => {
});
t.deepEqual(errors.length, 0);
});
ava_1.default("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]);
});
ava_1.default("validateWorkflow() when on.pull_requests is a string and correct", (t) => {
const errors = actionsutil.validateWorkflow({
on: { push: { branches: "*" }, pull_request: { branches: "*" } },
});
t.deepEqual(errors, []);
});
ava_1.default("validateWorkflow() when on.push is correct with empty objects", (t) => {
const errors = actionsutil.validateWorkflow({
on: { push: undefined, pull_request: undefined },

File diff suppressed because one or more lines are too long

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)
);