Skip to content

Commit

Permalink
Correctly report WorkflowMissing
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Engledew committed Jan 22, 2021
1 parent 4547749 commit 28e2860
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 50 deletions.
24 changes: 13 additions & 11 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.

9 changes: 1 addition & 8 deletions lib/init-action.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/init-action.js.map

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

35 changes: 18 additions & 17 deletions src/actions-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ export interface CodedError {
message: string;
code: string;
}
function toCodedErrors(errors: {
[key: string]: string;
}): { [key: string]: CodedError } {
function toCodedErrors<T>(errors: T): Record<keyof T, CodedError> {
return Object.entries(errors).reduce((acc, [key, value]) => {
acc[key] = { message: value, code: key };
return acc;
}, {} as ReturnType<typeof toCodedErrors>);
}, {} as Record<keyof T, CodedError>);
}

// codes to send back via status report
// if message is set to a string a warning annotation will also be added 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.`,
MissingHooks: `Please specify on.push and on.pull_request hooks so that Code Scanning can compare pull requests against the state of the base branch.`,
Expand All @@ -205,7 +205,6 @@ export const WorkflowErrors = toCodedErrors({
PathsSpecified: `Using on.push.paths can prevent Code Scanning annotating new alerts in your pull requests.`,
PathsIgnoreSpecified: `Using on.push.paths-ignore can prevent Code Scanning annotating new alerts in your pull requests.`,
CheckoutWrongHead: `git checkout HEAD^2 is no longer necessary. Please remove this step as Code Scanning recommends analyzing the merge commit for best results.`,
LintFailed: `Unable to lint workflow for CodeQL.`,
});

export function validateWorkflow(doc: Workflow): CodedError[] {
Expand Down Expand Up @@ -317,17 +316,23 @@ export function validateWorkflow(doc: Workflow): CodedError[] {
return errors;
}

export async function getWorkflowErrors(): Promise<CodedError[]> {
export async function getWorkflowErrors(): Promise<undefined | string> {
try {
const workflow = await getWorkflow();

if (workflow === undefined) {
return [];
}
try {
const workflowErrors = validateWorkflow(workflow);

if (workflowErrors.length > 0) {
core.warning(formatWorkflowErrors(workflowErrors));
}

return validateWorkflow(workflow);
return formatWorkflowCause(workflowErrors);
} catch (e) {
return `getWorkflowErrors() failed: ${e.toString()}`;
}
} catch (e) {
return [WorkflowErrors.LintFailed];
return `getWorkflow() failed: ${e.toString()}`;
}
}

Expand All @@ -346,18 +351,14 @@ export function formatWorkflowCause(errors: CodedError[]): undefined | string {
return errors.map((e) => e.code).join(",");
}

export async function getWorkflow(): Promise<Workflow | undefined> {
export async function getWorkflow(): Promise<Workflow> {
const relativePath = await getWorkflowPath();
const absolutePath = path.join(
getRequiredEnvParam("GITHUB_WORKSPACE"),
relativePath
);

try {
return yaml.safeLoad(fs.readFileSync(absolutePath, "utf-8"));
} catch (e) {
return undefined;
}
return yaml.safeLoad(fs.readFileSync(absolutePath, "utf-8"));
}

/**
Expand Down
13 changes: 1 addition & 12 deletions src/init-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,13 @@ async function run() {

const workflowErrors = await actionsUtil.getWorkflowErrors();

// we do not want to worry users if linting is failing
// but we do want to send a status report containing this error code
// below
const userWorkflowErrors = workflowErrors.filter(
(o) => o.code !== "LintFailed"
);

if (userWorkflowErrors.length > 0) {
core.warning(actionsUtil.formatWorkflowErrors(userWorkflowErrors));
}

if (
!(await actionsUtil.sendStatusReport(
await actionsUtil.createStatusReportBase(
"init",
"starting",
startedAt,
actionsUtil.formatWorkflowCause(workflowErrors)
workflowErrors
)
))
) {
Expand Down

0 comments on commit 28e2860

Please sign in to comment.