Merge branch 'main' into remove-uploadFromActions-params

This commit is contained in:
Sam Partington 2021-02-02 11:34:02 +00:00 committed by GitHub
commit 10a2f1b1aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 84 deletions

View file

@ -232,7 +232,7 @@ test("getWorkflowErrors() for a range of malformed workflows", (t) => {
actionsutil.getWorkflowErrors({
on: 1,
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
[]
)
);
@ -242,7 +242,7 @@ test("getWorkflowErrors() for a range of malformed workflows", (t) => {
on: 1,
jobs: 1,
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
[]
)
);
@ -252,7 +252,7 @@ test("getWorkflowErrors() for a range of malformed workflows", (t) => {
on: 1,
jobs: [1],
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
[]
)
);
@ -262,7 +262,7 @@ test("getWorkflowErrors() for a range of malformed workflows", (t) => {
on: 1,
jobs: { 1: 1 },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
[]
)
);
@ -272,7 +272,7 @@ test("getWorkflowErrors() for a range of malformed workflows", (t) => {
on: 1,
jobs: { test: 1 },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
[]
)
);
@ -282,7 +282,7 @@ test("getWorkflowErrors() for a range of malformed workflows", (t) => {
on: 1,
jobs: { test: [1] },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
[]
)
);
@ -292,7 +292,7 @@ test("getWorkflowErrors() for a range of malformed workflows", (t) => {
on: 1,
jobs: { test: { steps: 1 } },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
[]
)
);
@ -302,7 +302,7 @@ test("getWorkflowErrors() for a range of malformed workflows", (t) => {
on: 1,
jobs: { test: { steps: [{ notrun: "git checkout HEAD^2" }] } },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
[]
)
);
@ -312,7 +312,7 @@ test("getWorkflowErrors() for a range of malformed workflows", (t) => {
on: 1,
jobs: { test: [undefined] },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
[]
)
);
@ -403,6 +403,12 @@ test("formatWorkflowErrors() when there are multiple errors", (t) => {
t.true(message.startsWith("2 issues were detected with this workflow:"));
});
test("formatWorkflowCause() with no errors", (t) => {
const message = actionsutil.formatWorkflowCause([]);
t.deepEqual(message, undefined);
});
test("formatWorkflowCause()", (t) => {
const message = actionsutil.formatWorkflowCause([
actionsutil.WorkflowErrors.CheckoutWrongHead,

View file

@ -177,17 +177,11 @@ function branchesToArray(branches?: string | null | string[]): string[] | "**" {
}
return "**";
}
enum MissingTriggers {
None = 0,
Push = 1,
PullRequest = 2,
}
export interface CodedError {
message: string;
code: string;
}
function toCodedErrors<T>(errors: T): Record<keyof T, CodedError> {
return Object.entries(errors).reduce((acc, [key, value]) => {
acc[key] = { message: value, code: key };
@ -199,8 +193,6 @@ function toCodedErrors<T>(errors: T): Record<keyof T, CodedError> {
// message to add as a warning annotation 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.`,
MissingPullRequestHook: `Please specify an on.pull_request hook so that Code Scanning is explicitly run against pull requests. This will be required to see results on pull requests from January 31 2021.`,
MissingPushHook: `Please specify an on.push hook so that Code Scanning can compare pull requests against the state of the base branch.`,
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.`,
@ -232,19 +224,19 @@ export function getWorkflowErrors(doc: Workflow): CodedError[] {
}
}
let missing = MissingTriggers.None;
let missingPush = false;
if (doc.on === undefined) {
// this is not a valid config
} else if (typeof doc.on === "string") {
if (doc.on === "pull_request") {
missing = MissingTriggers.Push;
missingPush = true;
}
} else if (Array.isArray(doc.on)) {
const hasPush = doc.on.includes("push");
const hasPullRequest = doc.on.includes("pull_request");
if (hasPullRequest && !hasPush) {
missing = missing | MissingTriggers.Push;
missingPush = true;
}
} else if (isObject(doc.on)) {
const hasPush = Object.prototype.hasOwnProperty.call(doc.on, "push");
@ -254,7 +246,7 @@ export function getWorkflowErrors(doc: Workflow): CodedError[] {
);
if (!hasPush && hasPullRequest) {
missing = missing | MissingTriggers.Push;
missingPush = true;
}
if (hasPush && hasPullRequest) {
const paths = doc.on.push?.paths;
@ -295,22 +287,10 @@ export function getWorkflowErrors(doc: Workflow): CodedError[] {
}
}
}
} else {
// on is not a known type
// this workflow is likely malformed
missing = MissingTriggers.Push | MissingTriggers.PullRequest;
}
switch (missing) {
case MissingTriggers.PullRequest | MissingTriggers.Push:
errors.push(WorkflowErrors.MissingHooks);
break;
case MissingTriggers.PullRequest:
errors.push(WorkflowErrors.MissingPullRequestHook);
break;
case MissingTriggers.Push:
errors.push(WorkflowErrors.MissingPushHook);
break;
if (missingPush) {
errors.push(WorkflowErrors.MissingPushHook);
}
return errors;
@ -340,7 +320,7 @@ export async function validateWorkflow(): Promise<undefined | string> {
core.warning(message);
}
return `warning: ${formatWorkflowCause(workflowErrors)}`;
return formatWorkflowCause(workflowErrors);
}
export function formatWorkflowErrors(errors: CodedError[]): string {