Correctly report WorkflowMissing

This commit is contained in:
Simon Engledew 2021-01-22 09:04:58 +00:00
parent 4547749a2f
commit 28e2860afb
No known key found for this signature in database
GPG key ID: 84302E7B02FE8BCE
6 changed files with 35 additions and 50 deletions

24
lib/actions-util.js generated
View file

@ -153,6 +153,8 @@ function toCodedErrors(errors) {
return acc;
}, {});
}
// codes to send back via status report
// if message is set to a string a warning annotation will also be added 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.`,
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.`,
@ -161,7 +163,6 @@ exports.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.`,
});
function validateWorkflow(doc) {
var _a, _b, _c, _d, _e, _f, _g, _h;
@ -264,13 +265,19 @@ exports.validateWorkflow = validateWorkflow;
async function getWorkflowErrors() {
try {
const workflow = await getWorkflow();
if (workflow === undefined) {
return [];
try {
const workflowErrors = validateWorkflow(workflow);
if (workflowErrors.length > 0) {
core.warning(formatWorkflowErrors(workflowErrors));
}
return formatWorkflowCause(workflowErrors);
}
catch (e) {
return `getWorkflowErrors() failed: ${e.toString()}`;
}
return validateWorkflow(workflow);
}
catch (e) {
return [exports.WorkflowErrors.LintFailed];
return `getWorkflow() failed: ${e.toString()}`;
}
}
exports.getWorkflowErrors = getWorkflowErrors;
@ -290,12 +297,7 @@ exports.formatWorkflowCause = formatWorkflowCause;
async function getWorkflow() {
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"));
}
exports.getWorkflow = getWorkflow;
/**

File diff suppressed because one or more lines are too long

9
lib/init-action.js generated
View file

@ -66,14 +66,7 @@ async function run() {
try {
actionsUtil.prepareLocalRunEnvironment();
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))))) {
if (!(await actionsUtil.sendStatusReport(await actionsUtil.createStatusReportBase("init", "starting", startedAt, workflowErrors)))) {
return;
}
const initCodeQLResult = await init_1.initCodeQL(actionsUtil.getOptionalInput("tools"), apiDetails, actionsUtil.getRequiredEnvParam("RUNNER_TEMP"), actionsUtil.getRequiredEnvParam("RUNNER_TOOL_CACHE"), "actions", logger);

File diff suppressed because one or more lines are too long

View file

@ -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.`,
@ -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[] {
@ -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);
return validateWorkflow(workflow);
if (workflowErrors.length > 0) {
core.warning(formatWorkflowErrors(workflowErrors));
}
return formatWorkflowCause(workflowErrors);
} catch (e) {
return `getWorkflowErrors() failed: ${e.toString()}`;
}
} catch (e) {
return [WorkflowErrors.LintFailed];
return `getWorkflow() failed: ${e.toString()}`;
}
}
@ -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"));
}
/**

View file

@ -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
)
))
) {