Skip to content

Commit

Permalink
Showing 9 changed files with 92 additions and 61 deletions.
27 changes: 21 additions & 6 deletions lib/actions-util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.js.map

Large diffs are not rendered by default.

26 changes: 8 additions & 18 deletions lib/actions-util.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.test.js.map
9 changes: 8 additions & 1 deletion lib/init-action.js
2 changes: 1 addition & 1 deletion lib/init-action.js.map
26 changes: 8 additions & 18 deletions src/actions-util.test.ts
@@ -85,31 +85,27 @@ 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) => {
const errors = actionsutil.validateWorkflow({ on: {} });

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]);
});
37 changes: 23 additions & 14 deletions src/actions-util.ts
@@ -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,16 +135,21 @@ 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[] = [];

// .jobs[key].steps[].run
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];
22 changes: 21 additions & 1 deletion src/init-action.ts
@@ -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;

0 comments on commit 33bb875

Please sign in to comment.