Write a warning if there is an error with the workflow

This commit is contained in:
Simon Engledew 2020-11-24 09:51:00 +00:00
parent 7eb9dfcc60
commit 33bb87523e
No known key found for this signature in database
GPG key ID: 84302E7B02FE8BCE
9 changed files with 92 additions and 61 deletions

View file

@ -85,7 +85,7 @@ test("prepareEnvironment() when a local run", (t) => {
test("validateWorkflow() when on is missing", (t) => {
const errors = actionsutil.validateWorkflow({});
t.deepEqual(errors, ["Please specify on.push and on.pull_request hooks."]);
t.deepEqual(errors, [actionsutil.ErrMissingHooks]);
});
test("validateWorkflow() when on.push is missing", (t) => {
@ -93,23 +93,19 @@ test("validateWorkflow() when on.push is missing", (t) => {
console.log(errors);
t.deepEqual(errors, ["Please specify on.push and on.pull_request hooks."]);
t.deepEqual(errors, [actionsutil.ErrMissingHooks]);
});
test("validateWorkflow() when on.push is an array missing pull_request", (t) => {
const errors = actionsutil.validateWorkflow({ on: ["push"] });
t.deepEqual(errors, [
"Please specify an on.pull_request hook so CodeQL is run against new pull requests.",
]);
t.deepEqual(errors, [actionsutil.ErrMissingPullRequestHook]);
});
test("validateWorkflow() when on.push is an array missing push", (t) => {
const errors = actionsutil.validateWorkflow({ on: ["pull_request"] });
t.deepEqual(errors, [
"Please specify an on.push hook so CodeQL can establish a baseline.",
]);
t.deepEqual(errors, [actionsutil.ErrMissingPushHook]);
});
test("validateWorkflow() when on.push is valid", (t) => {
@ -136,7 +132,7 @@ test("validateWorkflow() when on.push should not have a path", (t) => {
},
});
t.deepEqual(errors, ["Please do not specify paths at on.pull."]);
t.deepEqual(errors, [actionsutil.ErrPathsSpecified]);
});
test("validateWorkflow() when on.push is a correct object", (t) => {
@ -165,9 +161,7 @@ test("validateWorkflow() when on.push is mismatched", (t) => {
},
});
t.deepEqual(errors, [
"Please make sure that any branches in on.pull_request: are also in on.push: so that CodeQL can establish a baseline.",
]);
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
});
test("validateWorkflow() when on.push is not mismatched", (t) => {
@ -189,9 +183,7 @@ test("validateWorkflow() when on.push is mismatched for pull_request", (t) => {
},
});
t.deepEqual(errors, [
"Please make sure that any branches in on.pull_request: are also in on.push: so that CodeQL can establish a baseline.",
]);
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
});
test("validateWorkflow() when HEAD^2 is checked out", (t) => {
@ -200,7 +192,5 @@ test("validateWorkflow() when HEAD^2 is checked out", (t) => {
jobs: { test: { steps: [{ run: "git checkout HEAD^2" }] } },
});
t.deepEqual(errors, [
"Git checkout HEAD^2 is no longer necessary. Please remove this line from your workflow.",
]);
t.deepEqual(errors, [actionsutil.ErrCheckoutWrongHead]);
});

View file

@ -1,8 +1,10 @@
import * as fs from "fs";
import * as path from "path";
import * as core from "@actions/core";
import * as toolrunner from "@actions/exec/lib/toolrunner";
import * as safeWhich from "@chrisgavin/safe-which";
import * as yaml from "js-yaml";
import * as api from "./api-client";
import * as sharedEnv from "./shared-environment";
@ -133,6 +135,13 @@ enum MissingTriggers {
PULL_REQUEST = 2,
}
export const ErrCheckoutWrongHead = `Git checkout HEAD^2 is no longer necessary. Please remove this line.`;
export const ErrMismatchedBranches = `Please make sure that any branches in on.pull_request are also in on.push so that CodeQL can establish a baseline.`;
export const ErrMissingHooks = `Please specify on.push and on.pull_request hooks.`;
export const ErrMissingPushHook = `Please specify an on.push hook so CodeQL can establish a baseline.`;
export const ErrMissingPullRequestHook = `Please specify an on.pull_request hook so CodeQL is run against new pull requests.`;
export const ErrPathsSpecified = `Please do not specify paths at on.pull.`;
export function validateWorkflow(doc: Workflow): string[] {
const errors: string[] = [];
@ -140,9 +149,7 @@ export function validateWorkflow(doc: Workflow): string[] {
for (const job of Object.values(doc?.jobs || {})) {
for (const step of job?.steps || []) {
if (step?.run === "git checkout HEAD^2") {
errors.push(
`Git checkout HEAD^2 is no longer necessary. Please remove this line from your workflow.`
);
errors.push(ErrCheckoutWrongHead);
}
}
}
@ -179,7 +186,7 @@ export function validateWorkflow(doc: Workflow): string[] {
} else {
const paths = doc.on.push?.paths;
if (Array.isArray(paths) && paths.length > 0) {
errors.push("Please do not specify paths at on.pull.");
errors.push(ErrPathsSpecified);
}
}
@ -190,36 +197,38 @@ export function validateWorkflow(doc: Workflow): string[] {
const intersects = pull_request.filter((value) => !push.includes(value));
if (intersects.length > 0) {
errors.push(
"Please make sure that any branches in on.pull_request: are also in on.push: so that CodeQL can establish a baseline."
);
errors.push(ErrMismatchedBranches);
}
}
}
switch (missing) {
case MissingTriggers.PULL_REQUEST | MissingTriggers.PUSH:
errors.push("Please specify on.push and on.pull_request hooks.");
errors.push(ErrMissingHooks);
break;
case MissingTriggers.PULL_REQUEST:
errors.push(
"Please specify an on.pull_request hook so CodeQL is run against new pull requests."
);
errors.push(ErrMissingPullRequestHook);
break;
case MissingTriggers.PUSH:
errors.push(
"Please specify an on.push hook so CodeQL can establish a baseline."
);
errors.push(ErrMissingPushHook);
break;
}
return errors;
}
export async function getWorkflow(): Promise<Workflow> {
return yaml.safeLoad(fs.readFileSync(await getWorkflowPath(), "utf-8"));
}
/**
* Get the path of the currently executing workflow.
*/
async function getWorkflowPath(): Promise<string> {
if (isLocalRun()) {
return getRequiredEnvParam("WORKFLOW_PATH");
}
const repo_nwo = getRequiredEnvParam("GITHUB_REPOSITORY").split("/");
const owner = repo_nwo[0];
const repo = repo_nwo[1];

View file

@ -96,9 +96,29 @@ async function run() {
try {
actionsUtil.prepareLocalRunEnvironment();
const workflowErrors = actionsUtil.validateWorkflow(
await actionsUtil.getWorkflow()
);
const workflowErrorMessage =
workflowErrors.length > 0
? `${workflowErrors.length} issue${
workflowErrors.length === 1 ? " was" : "s were"
} detected with this workflow: ${workflowErrors.join(", ")}`
: undefined;
if (workflowErrorMessage !== undefined) {
core.warning(workflowErrorMessage);
}
if (
!(await actionsUtil.sendStatusReport(
await actionsUtil.createStatusReportBase("init", "starting", startedAt)
await actionsUtil.createStatusReportBase(
"init",
"starting",
startedAt,
workflowErrorMessage
)
))
) {
return;