mirror of
https://github.com/github/codeql-action.git
synced 2025-12-30 19:20:08 +08:00
Tidy up ready for review
This commit is contained in:
57
lib/actions-util.js
generated
57
lib/actions-util.js
generated
@@ -107,30 +107,20 @@ var MissingTriggers;
|
||||
MissingTriggers[MissingTriggers["Push"] = 1] = "Push";
|
||||
MissingTriggers[MissingTriggers["PullRequest"] = 2] = "PullRequest";
|
||||
})(MissingTriggers || (MissingTriggers = {}));
|
||||
exports.ErrCheckoutWrongHead = {
|
||||
message: `Git checkout HEAD^2 is no longer necessary. Please remove this line.`,
|
||||
code: "CheckoutWrongHead",
|
||||
};
|
||||
exports.ErrMismatchedBranches = {
|
||||
message: `Please make sure that every branch in on.pull_request is also in on.push so that CodeQL can establish a baseline.`,
|
||||
code: "MismatchedBranches",
|
||||
};
|
||||
exports.ErrMissingHooks = {
|
||||
message: `Please specify on.push and on.pull_request hooks.`,
|
||||
code: "MissingHooks",
|
||||
};
|
||||
exports.ErrMissingPushHook = {
|
||||
message: `Please specify an on.push hook so CodeQL can establish a baseline.`,
|
||||
code: "MissingPushHook",
|
||||
};
|
||||
exports.ErrMissingPullRequestHook = {
|
||||
message: `Please specify an on.pull_request hook so CodeQL is run against new pull requests.`,
|
||||
code: "MissingPullRequestHook",
|
||||
};
|
||||
exports.ErrPathsSpecified = {
|
||||
message: `Please do not specify paths at on.pull.`,
|
||||
code: "PathsSpecified",
|
||||
};
|
||||
function toCodedErrors(errors) {
|
||||
return Object.entries(errors).reduce((acc, [key, value]) => {
|
||||
acc[key] = { message: value, code: key };
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
exports.WorkflowErrors = toCodedErrors({
|
||||
MismatchedBranches: `Please make sure that every branch in on.pull_request is also in on.push so that CodeQL can establish a baseline.`,
|
||||
MissingHooks: `Please specify on.push and on.pull_request hooks.`,
|
||||
MissingPullRequestHook: `Please specify an on.pull_request hook so CodeQL is run against new pull requests.`,
|
||||
MissingPushHook: `Please specify an on.push hook so CodeQL can establish a baseline.`,
|
||||
PathsSpecified: `Please do not specify paths at on.pull.`,
|
||||
CheckoutWrongHead: `Git checkout HEAD^2 is no longer necessary. Please remove this line.`,
|
||||
});
|
||||
function validateWorkflow(doc) {
|
||||
var _a, _b, _c, _d;
|
||||
const errors = [];
|
||||
@@ -138,7 +128,7 @@ function validateWorkflow(doc) {
|
||||
for (const job of Object.values(((_a = doc) === null || _a === void 0 ? void 0 : _a.jobs) || {})) {
|
||||
for (const step of ((_b = job) === null || _b === void 0 ? void 0 : _b.steps) || []) {
|
||||
if (((_c = step) === null || _c === void 0 ? void 0 : _c.run) === "git checkout HEAD^2") {
|
||||
errors.push(exports.ErrCheckoutWrongHead);
|
||||
errors.push(exports.WorkflowErrors.CheckoutWrongHead);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,7 +167,7 @@ function validateWorkflow(doc) {
|
||||
else {
|
||||
const paths = (_d = doc.on.push) === null || _d === void 0 ? void 0 : _d.paths;
|
||||
if (Array.isArray(paths) && paths.length > 0) {
|
||||
errors.push(exports.ErrPathsSpecified);
|
||||
errors.push(exports.WorkflowErrors.PathsSpecified);
|
||||
}
|
||||
}
|
||||
if (doc.on.push) {
|
||||
@@ -186,23 +176,23 @@ function validateWorkflow(doc) {
|
||||
const pull_request = doc.on.pull_request.branches || [];
|
||||
const intersects = pull_request.filter((value) => !push.includes(value));
|
||||
if (intersects.length > 0) {
|
||||
errors.push(exports.ErrMismatchedBranches);
|
||||
errors.push(exports.WorkflowErrors.MismatchedBranches);
|
||||
}
|
||||
}
|
||||
else if (push.length > 0) {
|
||||
errors.push(exports.ErrMismatchedBranches);
|
||||
errors.push(exports.WorkflowErrors.MismatchedBranches);
|
||||
}
|
||||
}
|
||||
}
|
||||
switch (missing) {
|
||||
case MissingTriggers.PullRequest | MissingTriggers.Push:
|
||||
errors.push(exports.ErrMissingHooks);
|
||||
errors.push(exports.WorkflowErrors.MissingHooks);
|
||||
break;
|
||||
case MissingTriggers.PullRequest:
|
||||
errors.push(exports.ErrMissingPullRequestHook);
|
||||
errors.push(exports.WorkflowErrors.MissingPullRequestHook);
|
||||
break;
|
||||
case MissingTriggers.Push:
|
||||
errors.push(exports.ErrMissingPushHook);
|
||||
errors.push(exports.WorkflowErrors.MissingPushHook);
|
||||
break;
|
||||
}
|
||||
return errors;
|
||||
@@ -221,7 +211,10 @@ async function getWorkflowErrors() {
|
||||
}
|
||||
exports.getWorkflowErrors = getWorkflowErrors;
|
||||
function formatWorkflowErrors(errors) {
|
||||
return `${errors.length} issue${errors.length === 1 ? " was" : "s were"} detected with this workflow: ${errors.map((e) => e.message).join(", ")}`;
|
||||
const issuesWere = errors.length === 1 ? "issue was" : "issues were";
|
||||
return `${errors.length} ${issuesWere} detected with this workflow: ${errors
|
||||
.map((e) => e.message)
|
||||
.join(", ")}`;
|
||||
}
|
||||
exports.formatWorkflowErrors = formatWorkflowErrors;
|
||||
function formatWorkflowCause(errors) {
|
||||
|
||||
File diff suppressed because one or more lines are too long
28
lib/actions-util.test.js
generated
28
lib/actions-util.test.js
generated
@@ -70,20 +70,20 @@ ava_1.default("prepareEnvironment() when a local run", (t) => {
|
||||
});
|
||||
ava_1.default("validateWorkflow() when on is missing", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({});
|
||||
t.deepEqual(errors, [actionsutil.ErrMissingHooks]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MissingHooks]);
|
||||
});
|
||||
ava_1.default("validateWorkflow() when on.push is missing", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({ on: {} });
|
||||
console.log(errors);
|
||||
t.deepEqual(errors, [actionsutil.ErrMissingHooks]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MissingHooks]);
|
||||
});
|
||||
ava_1.default("validateWorkflow() when on.push is an array missing pull_request", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({ on: ["push"] });
|
||||
t.deepEqual(errors, [actionsutil.ErrMissingPullRequestHook]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MissingPullRequestHook]);
|
||||
});
|
||||
ava_1.default("validateWorkflow() when on.push is an array missing push", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({ on: ["pull_request"] });
|
||||
t.deepEqual(errors, [actionsutil.ErrMissingPushHook]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MissingPushHook]);
|
||||
});
|
||||
ava_1.default("validateWorkflow() when on.push is valid", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({
|
||||
@@ -104,7 +104,7 @@ ava_1.default("validateWorkflow() when on.push should not have a path", (t) => {
|
||||
pull_request: { branches: ["main"] },
|
||||
},
|
||||
});
|
||||
t.deepEqual(errors, [actionsutil.ErrPathsSpecified]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.PathsSpecified]);
|
||||
});
|
||||
ava_1.default("validateWorkflow() when on.push is a correct object", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({
|
||||
@@ -126,7 +126,7 @@ ava_1.default("validateWorkflow() when on.push is mismatched", (t) => {
|
||||
pull_request: { branches: ["feature"] },
|
||||
},
|
||||
});
|
||||
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MismatchedBranches]);
|
||||
});
|
||||
ava_1.default("validateWorkflow() when on.push is not mismatched", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({
|
||||
@@ -144,7 +144,7 @@ ava_1.default("validateWorkflow() when on.push is mismatched for pull_request",
|
||||
pull_request: { branches: ["main", "feature"] },
|
||||
},
|
||||
});
|
||||
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MismatchedBranches]);
|
||||
});
|
||||
ava_1.default("validateWorkflow() when on.pull_request for every branch but push specifies branches", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({
|
||||
@@ -153,32 +153,32 @@ ava_1.default("validateWorkflow() when on.pull_request for every branch but push
|
||||
pull_request: null,
|
||||
},
|
||||
});
|
||||
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MismatchedBranches]);
|
||||
});
|
||||
ava_1.default("validateWorkflow() when HEAD^2 is checked out", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({
|
||||
on: ["push", "pull_request"],
|
||||
jobs: { test: { steps: [{ run: "git checkout HEAD^2" }] } },
|
||||
});
|
||||
t.deepEqual(errors, [actionsutil.ErrCheckoutWrongHead]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.CheckoutWrongHead]);
|
||||
});
|
||||
ava_1.default("formatWorkflowErrors() when there is one error", (t) => {
|
||||
const message = actionsutil.formatWorkflowErrors([
|
||||
actionsutil.ErrCheckoutWrongHead,
|
||||
actionsutil.WorkflowErrors.CheckoutWrongHead,
|
||||
]);
|
||||
t.true(message.startsWith("1 issue was detected with this workflow:"));
|
||||
});
|
||||
ava_1.default("formatWorkflowErrors() when there are multiple errors", (t) => {
|
||||
const message = actionsutil.formatWorkflowErrors([
|
||||
actionsutil.ErrCheckoutWrongHead,
|
||||
actionsutil.ErrPathsSpecified,
|
||||
actionsutil.WorkflowErrors.CheckoutWrongHead,
|
||||
actionsutil.WorkflowErrors.PathsSpecified,
|
||||
]);
|
||||
t.true(message.startsWith("2 issues were detected with this workflow:"));
|
||||
});
|
||||
ava_1.default("formatWorkflowCause()", (t) => {
|
||||
const message = actionsutil.formatWorkflowCause([
|
||||
actionsutil.ErrCheckoutWrongHead,
|
||||
actionsutil.ErrPathsSpecified,
|
||||
actionsutil.WorkflowErrors.CheckoutWrongHead,
|
||||
actionsutil.WorkflowErrors.PathsSpecified,
|
||||
]);
|
||||
t.deepEqual(message, "CheckoutWrongHead,PathsSpecified");
|
||||
t.deepEqual(actionsutil.formatWorkflowCause(undefined), undefined);
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user