remove consts MismatchedBranches and MissingPushHook
This commit is contained in:
parent
95cfca769b
commit
edb138ff88
6 changed files with 4 additions and 268 deletions
65
lib/workflow.js
generated
65
lib/workflow.js
generated
|
|
@ -34,9 +34,6 @@ const core = __importStar(require("@actions/core"));
|
|||
const yaml = __importStar(require("js-yaml"));
|
||||
const api = __importStar(require("./api-client"));
|
||||
const util_1 = require("./util");
|
||||
function isObject(o) {
|
||||
return o !== null && typeof o === "object";
|
||||
}
|
||||
const GLOB_PATTERN = new RegExp("(\\*\\*?)");
|
||||
function escapeRegExp(string) {
|
||||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
||||
|
|
@ -65,18 +62,6 @@ function patternIsSuperset(patternA, patternB) {
|
|||
return patternToRegExp(patternA).test(patternB);
|
||||
}
|
||||
exports.patternIsSuperset = patternIsSuperset;
|
||||
function branchesToArray(branches) {
|
||||
if (typeof branches === "string") {
|
||||
return [branches];
|
||||
}
|
||||
if (Array.isArray(branches)) {
|
||||
if (branches.length === 0) {
|
||||
return "**";
|
||||
}
|
||||
return branches;
|
||||
}
|
||||
return "**";
|
||||
}
|
||||
function toCodedErrors(errors) {
|
||||
return Object.entries(errors).reduce((acc, [code, message]) => {
|
||||
acc[code] = { message, code };
|
||||
|
|
@ -86,8 +71,6 @@ function toCodedErrors(errors) {
|
|||
// code to send back via status report
|
||||
// message to add as a warning annotation to the run
|
||||
exports.WorkflowErrors = toCodedErrors({
|
||||
MismatchedBranches: `Please make sure that every branch in on.pull_request is also in on.push so that Code Scanning can compare pull requests against the state of the base branch.`,
|
||||
MissingPushHook: `Please specify an on.push hook so that Code Scanning can compare pull requests against the state of the base branch.`,
|
||||
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 getWorkflowErrors(doc) {
|
||||
|
|
@ -110,54 +93,6 @@ function getWorkflowErrors(doc) {
|
|||
}
|
||||
}
|
||||
}
|
||||
let missingPush = false;
|
||||
if (doc.on === undefined) {
|
||||
// this is not a valid config
|
||||
}
|
||||
else if (typeof doc.on === "string") {
|
||||
if (doc.on === "pull_request") {
|
||||
missingPush = true;
|
||||
}
|
||||
}
|
||||
else if (Array.isArray(doc.on)) {
|
||||
const hasPush = doc.on.includes("push");
|
||||
const hasPullRequest = doc.on.includes("pull_request");
|
||||
if (hasPullRequest && !hasPush) {
|
||||
missingPush = true;
|
||||
}
|
||||
}
|
||||
else if (isObject(doc.on)) {
|
||||
const hasPush = Object.prototype.hasOwnProperty.call(doc.on, "push");
|
||||
const hasPullRequest = Object.prototype.hasOwnProperty.call(doc.on, "pull_request");
|
||||
if (!hasPush && hasPullRequest) {
|
||||
missingPush = true;
|
||||
}
|
||||
// if doc.on.pull_request is null that means 'all branches'
|
||||
// if doc.on.pull_request is undefined that means 'off'
|
||||
// we only want to check for mismatched branches if pull_request is on.
|
||||
if (doc.on.pull_request !== undefined) {
|
||||
const push = branchesToArray(doc.on.push?.branches);
|
||||
if (push !== "**") {
|
||||
const pull_request = branchesToArray(doc.on.pull_request?.branches);
|
||||
if (pull_request !== "**") {
|
||||
const difference = pull_request.filter((value) => !push.some((o) => patternIsSuperset(o, value)));
|
||||
if (difference.length > 0) {
|
||||
// there are branches in pull_request that may not have a baseline
|
||||
// because we are not building them on push
|
||||
errors.push(exports.WorkflowErrors.MismatchedBranches);
|
||||
}
|
||||
}
|
||||
else if (push.length > 0) {
|
||||
// push is set up to run on a subset of branches
|
||||
// and you could open a PR against a branch with no baseline
|
||||
errors.push(exports.WorkflowErrors.MismatchedBranches);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (missingPush) {
|
||||
errors.push(exports.WorkflowErrors.MissingPushHook);
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
exports.getWorkflowErrors = getWorkflowErrors;
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
57
lib/workflow.test.js
generated
57
lib/workflow.test.js
generated
|
|
@ -42,10 +42,6 @@ function errorCodes(actual, expected) {
|
|||
const errors = (0, workflow_1.getWorkflowErrors)({ on: ["push"] });
|
||||
t.deepEqual(...errorCodes(errors, []));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when on.push is an array missing push", (t) => {
|
||||
const errors = (0, workflow_1.getWorkflowErrors)({ on: ["pull_request"] });
|
||||
t.deepEqual(...errorCodes(errors, [workflow_1.WorkflowErrors.MissingPushHook]));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when on.push is valid", (t) => {
|
||||
const errors = (0, workflow_1.getWorkflowErrors)({
|
||||
on: ["push", "pull_request"],
|
||||
|
|
@ -64,12 +60,6 @@ function errorCodes(actual, expected) {
|
|||
});
|
||||
t.deepEqual(...errorCodes(errors, []));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when on.pull_requests is a string", (t) => {
|
||||
const errors = (0, workflow_1.getWorkflowErrors)({
|
||||
on: { push: { branches: ["main"] }, pull_request: { branches: "*" } },
|
||||
});
|
||||
t.deepEqual(...errorCodes(errors, [workflow_1.WorkflowErrors.MismatchedBranches]));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when on.pull_requests is a string and correct", (t) => {
|
||||
const errors = (0, workflow_1.getWorkflowErrors)({
|
||||
on: { push: { branches: "*" }, pull_request: { branches: "*" } },
|
||||
|
|
@ -84,15 +74,6 @@ function errorCodes(actual, expected) {
|
|||
`));
|
||||
t.deepEqual(...errorCodes(errors, []));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when on.push is mismatched", (t) => {
|
||||
const errors = (0, workflow_1.getWorkflowErrors)({
|
||||
on: {
|
||||
push: { branches: ["main"] },
|
||||
pull_request: { branches: ["feature"] },
|
||||
},
|
||||
});
|
||||
t.deepEqual(...errorCodes(errors, [workflow_1.WorkflowErrors.MismatchedBranches]));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when on.push is not mismatched", (t) => {
|
||||
const errors = (0, workflow_1.getWorkflowErrors)({
|
||||
on: {
|
||||
|
|
@ -102,15 +83,6 @@ function errorCodes(actual, expected) {
|
|||
});
|
||||
t.deepEqual(...errorCodes(errors, []));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when on.push is mismatched for pull_request", (t) => {
|
||||
const errors = (0, workflow_1.getWorkflowErrors)({
|
||||
on: {
|
||||
push: { branches: ["main"] },
|
||||
pull_request: { branches: ["main", "feature"] },
|
||||
},
|
||||
});
|
||||
t.deepEqual(...errorCodes(errors, [workflow_1.WorkflowErrors.MismatchedBranches]));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() for a range of malformed workflows", (t) => {
|
||||
t.deepEqual(...errorCodes((0, workflow_1.getWorkflowErrors)({
|
||||
on: {
|
||||
|
|
@ -175,16 +147,6 @@ function errorCodes(actual, expected) {
|
|||
},
|
||||
}), []));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when on.pull_request for every branch but push specifies branches", (t) => {
|
||||
const errors = (0, workflow_1.getWorkflowErrors)(yaml.load(`
|
||||
name: "CodeQL"
|
||||
on:
|
||||
push:
|
||||
branches: ["main"]
|
||||
pull_request:
|
||||
`));
|
||||
t.deepEqual(...errorCodes(errors, [workflow_1.WorkflowErrors.MismatchedBranches]));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when on.pull_request for wildcard branches", (t) => {
|
||||
const errors = (0, workflow_1.getWorkflowErrors)({
|
||||
on: {
|
||||
|
|
@ -194,15 +156,6 @@ function errorCodes(actual, expected) {
|
|||
});
|
||||
t.deepEqual(...errorCodes(errors, []));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when on.pull_request for mismatched wildcard branches", (t) => {
|
||||
const errors = (0, workflow_1.getWorkflowErrors)({
|
||||
on: {
|
||||
push: { branches: ["feature/moose"] },
|
||||
pull_request: { branches: "feature/*" },
|
||||
},
|
||||
});
|
||||
t.deepEqual(...errorCodes(errors, [workflow_1.WorkflowErrors.MismatchedBranches]));
|
||||
});
|
||||
(0, ava_1.default)("getWorkflowErrors() when HEAD^2 is checked out", (t) => {
|
||||
process.env.GITHUB_JOB = "test";
|
||||
const errors = (0, workflow_1.getWorkflowErrors)({
|
||||
|
|
@ -215,13 +168,6 @@ function errorCodes(actual, expected) {
|
|||
const message = (0, workflow_1.formatWorkflowErrors)([workflow_1.WorkflowErrors.CheckoutWrongHead]);
|
||||
t.true(message.startsWith("1 issue was detected with this workflow:"));
|
||||
});
|
||||
(0, ava_1.default)("formatWorkflowErrors() when there are multiple errors", (t) => {
|
||||
const message = (0, workflow_1.formatWorkflowErrors)([
|
||||
workflow_1.WorkflowErrors.CheckoutWrongHead,
|
||||
workflow_1.WorkflowErrors.MismatchedBranches,
|
||||
]);
|
||||
t.true(message.startsWith("2 issues were detected with this workflow:"));
|
||||
});
|
||||
(0, ava_1.default)("formatWorkflowCause() with no errors", (t) => {
|
||||
const message = (0, workflow_1.formatWorkflowCause)([]);
|
||||
t.deepEqual(message, undefined);
|
||||
|
|
@ -229,9 +175,8 @@ function errorCodes(actual, expected) {
|
|||
(0, ava_1.default)("formatWorkflowCause()", (t) => {
|
||||
const message = (0, workflow_1.formatWorkflowCause)([
|
||||
workflow_1.WorkflowErrors.CheckoutWrongHead,
|
||||
workflow_1.WorkflowErrors.MismatchedBranches,
|
||||
]);
|
||||
t.deepEqual(message, "CheckoutWrongHead,MismatchedBranches");
|
||||
t.deepEqual(message, "CheckoutWrongHead");
|
||||
t.deepEqual((0, workflow_1.formatWorkflowCause)([]), undefined);
|
||||
});
|
||||
(0, ava_1.default)("patternIsSuperset()", (t) => {
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -34,12 +34,6 @@ test("getWorkflowErrors() when on.push is an array missing pull_request", (t) =>
|
|||
t.deepEqual(...errorCodes(errors, []));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when on.push is an array missing push", (t) => {
|
||||
const errors = getWorkflowErrors({ on: ["pull_request"] });
|
||||
|
||||
t.deepEqual(...errorCodes(errors, [WorkflowErrors.MissingPushHook]));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when on.push is valid", (t) => {
|
||||
const errors = getWorkflowErrors({
|
||||
on: ["push", "pull_request"],
|
||||
|
|
@ -64,14 +58,6 @@ test("getWorkflowErrors() when on.push is a correct object", (t) => {
|
|||
t.deepEqual(...errorCodes(errors, []));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when on.pull_requests is a string", (t) => {
|
||||
const errors = getWorkflowErrors({
|
||||
on: { push: { branches: ["main"] }, pull_request: { branches: "*" } },
|
||||
});
|
||||
|
||||
t.deepEqual(...errorCodes(errors, [WorkflowErrors.MismatchedBranches]));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when on.pull_requests is a string and correct", (t) => {
|
||||
const errors = getWorkflowErrors({
|
||||
on: { push: { branches: "*" }, pull_request: { branches: "*" } },
|
||||
|
|
@ -92,17 +78,6 @@ test("getWorkflowErrors() when on.push is correct with empty objects", (t) => {
|
|||
t.deepEqual(...errorCodes(errors, []));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when on.push is mismatched", (t) => {
|
||||
const errors = getWorkflowErrors({
|
||||
on: {
|
||||
push: { branches: ["main"] },
|
||||
pull_request: { branches: ["feature"] },
|
||||
},
|
||||
});
|
||||
|
||||
t.deepEqual(...errorCodes(errors, [WorkflowErrors.MismatchedBranches]));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when on.push is not mismatched", (t) => {
|
||||
const errors = getWorkflowErrors({
|
||||
on: {
|
||||
|
|
@ -114,17 +89,6 @@ test("getWorkflowErrors() when on.push is not mismatched", (t) => {
|
|||
t.deepEqual(...errorCodes(errors, []));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when on.push is mismatched for pull_request", (t) => {
|
||||
const errors = getWorkflowErrors({
|
||||
on: {
|
||||
push: { branches: ["main"] },
|
||||
pull_request: { branches: ["main", "feature"] },
|
||||
},
|
||||
});
|
||||
|
||||
t.deepEqual(...errorCodes(errors, [WorkflowErrors.MismatchedBranches]));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() for a range of malformed workflows", (t) => {
|
||||
t.deepEqual(
|
||||
...errorCodes(
|
||||
|
|
@ -251,20 +215,6 @@ test("getWorkflowErrors() for a range of malformed workflows", (t) => {
|
|||
);
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when on.pull_request for every branch but push specifies branches", (t) => {
|
||||
const errors = getWorkflowErrors(
|
||||
yaml.load(`
|
||||
name: "CodeQL"
|
||||
on:
|
||||
push:
|
||||
branches: ["main"]
|
||||
pull_request:
|
||||
`) as Workflow
|
||||
);
|
||||
|
||||
t.deepEqual(...errorCodes(errors, [WorkflowErrors.MismatchedBranches]));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when on.pull_request for wildcard branches", (t) => {
|
||||
const errors = getWorkflowErrors({
|
||||
on: {
|
||||
|
|
@ -276,17 +226,6 @@ test("getWorkflowErrors() when on.pull_request for wildcard branches", (t) => {
|
|||
t.deepEqual(...errorCodes(errors, []));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when on.pull_request for mismatched wildcard branches", (t) => {
|
||||
const errors = getWorkflowErrors({
|
||||
on: {
|
||||
push: { branches: ["feature/moose"] },
|
||||
pull_request: { branches: "feature/*" },
|
||||
},
|
||||
});
|
||||
|
||||
t.deepEqual(...errorCodes(errors, [WorkflowErrors.MismatchedBranches]));
|
||||
});
|
||||
|
||||
test("getWorkflowErrors() when HEAD^2 is checked out", (t) => {
|
||||
process.env.GITHUB_JOB = "test";
|
||||
|
||||
|
|
@ -303,14 +242,6 @@ test("formatWorkflowErrors() when there is one error", (t) => {
|
|||
t.true(message.startsWith("1 issue was detected with this workflow:"));
|
||||
});
|
||||
|
||||
test("formatWorkflowErrors() when there are multiple errors", (t) => {
|
||||
const message = formatWorkflowErrors([
|
||||
WorkflowErrors.CheckoutWrongHead,
|
||||
WorkflowErrors.MismatchedBranches,
|
||||
]);
|
||||
t.true(message.startsWith("2 issues were detected with this workflow:"));
|
||||
});
|
||||
|
||||
test("formatWorkflowCause() with no errors", (t) => {
|
||||
const message = formatWorkflowCause([]);
|
||||
|
||||
|
|
@ -320,10 +251,9 @@ test("formatWorkflowCause() with no errors", (t) => {
|
|||
test("formatWorkflowCause()", (t) => {
|
||||
const message = formatWorkflowCause([
|
||||
WorkflowErrors.CheckoutWrongHead,
|
||||
WorkflowErrors.MismatchedBranches,
|
||||
]);
|
||||
|
||||
t.deepEqual(message, "CheckoutWrongHead,MismatchedBranches");
|
||||
t.deepEqual(message, "CheckoutWrongHead");
|
||||
t.deepEqual(formatWorkflowCause([]), undefined);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -44,10 +44,6 @@ export interface Workflow {
|
|||
on?: string | string[] | WorkflowTriggers;
|
||||
}
|
||||
|
||||
function isObject(o: unknown): o is object {
|
||||
return o !== null && typeof o === "object";
|
||||
}
|
||||
|
||||
const GLOB_PATTERN = new RegExp("(\\*\\*?)");
|
||||
|
||||
function escapeRegExp(string) {
|
||||
|
|
@ -79,18 +75,6 @@ export function patternIsSuperset(patternA: string, patternB: string): boolean {
|
|||
return patternToRegExp(patternA).test(patternB);
|
||||
}
|
||||
|
||||
function branchesToArray(branches?: string | null | string[]): string[] | "**" {
|
||||
if (typeof branches === "string") {
|
||||
return [branches];
|
||||
}
|
||||
if (Array.isArray(branches)) {
|
||||
if (branches.length === 0) {
|
||||
return "**";
|
||||
}
|
||||
return branches;
|
||||
}
|
||||
return "**";
|
||||
}
|
||||
export interface CodedError {
|
||||
message: string;
|
||||
code: string;
|
||||
|
|
@ -108,8 +92,6 @@ function toCodedErrors(errors: {
|
|||
// code to send back via status report
|
||||
// message to add as a warning annotation to the run
|
||||
export const WorkflowErrors = toCodedErrors({
|
||||
MismatchedBranches: `Please make sure that every branch in on.pull_request is also in on.push so that Code Scanning can compare pull requests against the state of the base branch.`,
|
||||
MissingPushHook: `Please specify an on.push hook so that Code Scanning can compare pull requests against the state of the base branch.`,
|
||||
CheckoutWrongHead: `git checkout HEAD^2 is no longer necessary. Please remove this step as Code Scanning recommends analyzing the merge commit for best results.`,
|
||||
});
|
||||
|
||||
|
|
@ -138,62 +120,6 @@ export function getWorkflowErrors(doc: Workflow): CodedError[] {
|
|||
}
|
||||
}
|
||||
|
||||
let missingPush = false;
|
||||
|
||||
if (doc.on === undefined) {
|
||||
// this is not a valid config
|
||||
} else if (typeof doc.on === "string") {
|
||||
if (doc.on === "pull_request") {
|
||||
missingPush = true;
|
||||
}
|
||||
} else if (Array.isArray(doc.on)) {
|
||||
const hasPush = doc.on.includes("push");
|
||||
const hasPullRequest = doc.on.includes("pull_request");
|
||||
if (hasPullRequest && !hasPush) {
|
||||
missingPush = true;
|
||||
}
|
||||
} else if (isObject(doc.on)) {
|
||||
const hasPush = Object.prototype.hasOwnProperty.call(doc.on, "push");
|
||||
const hasPullRequest = Object.prototype.hasOwnProperty.call(
|
||||
doc.on,
|
||||
"pull_request"
|
||||
);
|
||||
|
||||
if (!hasPush && hasPullRequest) {
|
||||
missingPush = true;
|
||||
}
|
||||
|
||||
// if doc.on.pull_request is null that means 'all branches'
|
||||
// if doc.on.pull_request is undefined that means 'off'
|
||||
// we only want to check for mismatched branches if pull_request is on.
|
||||
if (doc.on.pull_request !== undefined) {
|
||||
const push = branchesToArray(doc.on.push?.branches);
|
||||
|
||||
if (push !== "**") {
|
||||
const pull_request = branchesToArray(doc.on.pull_request?.branches);
|
||||
|
||||
if (pull_request !== "**") {
|
||||
const difference = pull_request.filter(
|
||||
(value) => !push.some((o) => patternIsSuperset(o, value))
|
||||
);
|
||||
if (difference.length > 0) {
|
||||
// there are branches in pull_request that may not have a baseline
|
||||
// because we are not building them on push
|
||||
errors.push(WorkflowErrors.MismatchedBranches);
|
||||
}
|
||||
} else if (push.length > 0) {
|
||||
// push is set up to run on a subset of branches
|
||||
// and you could open a PR against a branch with no baseline
|
||||
errors.push(WorkflowErrors.MismatchedBranches);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (missingPush) {
|
||||
errors.push(WorkflowErrors.MissingPushHook);
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue