Skip to content

Commit 483255c

Browse files
author
Simon Engledew
committed
Correctly report WorkflowMissing
1 parent fbacfbf commit 483255c

6 files changed

Lines changed: 35 additions & 50 deletions

File tree

lib/actions-util.js

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions-util.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,15 @@ export interface CodedError {
188188
message: string;
189189
code: string;
190190
}
191-
function toCodedErrors(errors: {
192-
[key: string]: string;
193-
}): { [key: string]: CodedError } {
191+
function toCodedErrors<T>(errors: T): Record<keyof T, CodedError> {
194192
return Object.entries(errors).reduce((acc, [key, value]) => {
195193
acc[key] = { message: value, code: key };
196194
return acc;
197-
}, {} as ReturnType<typeof toCodedErrors>);
195+
}, {} as Record<keyof T, CodedError>);
198196
}
199197

198+
// codes to send back via status report
199+
// if message is set to a string a warning annotation will also be added to the run
200200
export const WorkflowErrors = toCodedErrors({
201201
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.`,
202202
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({
205205
PathsSpecified: `Using on.push.paths can prevent Code Scanning annotating new alerts in your pull requests.`,
206206
PathsIgnoreSpecified: `Using on.push.paths-ignore can prevent Code Scanning annotating new alerts in your pull requests.`,
207207
CheckoutWrongHead: `git checkout HEAD^2 is no longer necessary. Please remove this step as Code Scanning recommends analyzing the merge commit for best results.`,
208-
LintFailed: `Unable to lint workflow for CodeQL.`,
209208
});
210209

211210
export function validateWorkflow(doc: Workflow): CodedError[] {
@@ -317,17 +316,23 @@ export function validateWorkflow(doc: Workflow): CodedError[] {
317316
return errors;
318317
}
319318

320-
export async function getWorkflowErrors(): Promise<CodedError[]> {
319+
export async function getWorkflowErrors(): Promise<undefined | string> {
321320
try {
322321
const workflow = await getWorkflow();
323322

324-
if (workflow === undefined) {
325-
return [];
326-
}
323+
try {
324+
const workflowErrors = validateWorkflow(workflow);
325+
326+
if (workflowErrors.length > 0) {
327+
core.warning(formatWorkflowErrors(workflowErrors));
328+
}
327329

328-
return validateWorkflow(workflow);
330+
return formatWorkflowCause(workflowErrors);
331+
} catch (e) {
332+
return `getWorkflowErrors() failed: ${e.toString()}`;
333+
}
329334
} catch (e) {
330-
return [WorkflowErrors.LintFailed];
335+
return `getWorkflow() failed: ${e.toString()}`;
331336
}
332337
}
333338

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

349-
export async function getWorkflow(): Promise<Workflow | undefined> {
354+
export async function getWorkflow(): Promise<Workflow> {
350355
const relativePath = await getWorkflowPath();
351356
const absolutePath = path.join(
352357
getRequiredEnvParam("GITHUB_WORKSPACE"),
353358
relativePath
354359
);
355360

356-
try {
357-
return yaml.safeLoad(fs.readFileSync(absolutePath, "utf-8"));
358-
} catch (e) {
359-
return undefined;
360-
}
361+
return yaml.safeLoad(fs.readFileSync(absolutePath, "utf-8"));
361362
}
362363

363364
/**

src/init-action.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,13 @@ async function run() {
112112

113113
const workflowErrors = await actionsUtil.getWorkflowErrors();
114114

115-
// we do not want to worry users if linting is failing
116-
// but we do want to send a status report containing this error code
117-
// below
118-
const userWorkflowErrors = workflowErrors.filter(
119-
(o) => o.code !== "LintFailed"
120-
);
121-
122-
if (userWorkflowErrors.length > 0) {
123-
core.warning(actionsUtil.formatWorkflowErrors(userWorkflowErrors));
124-
}
125-
126115
if (
127116
!(await actionsUtil.sendStatusReport(
128117
await actionsUtil.createStatusReportBase(
129118
"init",
130119
"starting",
131120
startedAt,
132-
actionsUtil.formatWorkflowCause(workflowErrors)
121+
workflowErrors
133122
)
134123
))
135124
) {

0 commit comments

Comments
 (0)