Tidy up ready for review
This commit is contained in:
parent
1020df5700
commit
92ffb08081
6 changed files with 92 additions and 106 deletions
|
|
@ -85,7 +85,7 @@ test("prepareEnvironment() when a local run", (t) => {
|
|||
test("validateWorkflow() when on is missing", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({});
|
||||
|
||||
t.deepEqual(errors, [actionsutil.ErrMissingHooks]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MissingHooks]);
|
||||
});
|
||||
|
||||
test("validateWorkflow() when on.push is missing", (t) => {
|
||||
|
|
@ -93,19 +93,19 @@ test("validateWorkflow() when on.push is missing", (t) => {
|
|||
|
||||
console.log(errors);
|
||||
|
||||
t.deepEqual(errors, [actionsutil.ErrMissingHooks]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MissingHooks]);
|
||||
});
|
||||
|
||||
test("validateWorkflow() when on.push is an array missing pull_request", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({ on: ["push"] });
|
||||
|
||||
t.deepEqual(errors, [actionsutil.ErrMissingPullRequestHook]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MissingPullRequestHook]);
|
||||
});
|
||||
|
||||
test("validateWorkflow() when on.push is an array missing push", (t) => {
|
||||
const errors = actionsutil.validateWorkflow({ on: ["pull_request"] });
|
||||
|
||||
t.deepEqual(errors, [actionsutil.ErrMissingPushHook]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MissingPushHook]);
|
||||
});
|
||||
|
||||
test("validateWorkflow() when on.push is valid", (t) => {
|
||||
|
|
@ -132,7 +132,7 @@ test("validateWorkflow() when on.push should not have a path", (t) => {
|
|||
},
|
||||
});
|
||||
|
||||
t.deepEqual(errors, [actionsutil.ErrPathsSpecified]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.PathsSpecified]);
|
||||
});
|
||||
|
||||
test("validateWorkflow() when on.push is a correct object", (t) => {
|
||||
|
|
@ -161,7 +161,7 @@ test("validateWorkflow() when on.push is mismatched", (t) => {
|
|||
},
|
||||
});
|
||||
|
||||
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MismatchedBranches]);
|
||||
});
|
||||
|
||||
test("validateWorkflow() when on.push is not mismatched", (t) => {
|
||||
|
|
@ -183,7 +183,7 @@ test("validateWorkflow() when on.push is mismatched for pull_request", (t) => {
|
|||
},
|
||||
});
|
||||
|
||||
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MismatchedBranches]);
|
||||
});
|
||||
|
||||
test("validateWorkflow() when on.pull_request for every branch but push specifies branches", (t) => {
|
||||
|
|
@ -194,7 +194,7 @@ test("validateWorkflow() when on.pull_request for every branch but push specifie
|
|||
},
|
||||
});
|
||||
|
||||
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.MismatchedBranches]);
|
||||
});
|
||||
|
||||
test("validateWorkflow() when HEAD^2 is checked out", (t) => {
|
||||
|
|
@ -203,28 +203,28 @@ test("validateWorkflow() when HEAD^2 is checked out", (t) => {
|
|||
jobs: { test: { steps: [{ run: "git checkout HEAD^2" }] } },
|
||||
});
|
||||
|
||||
t.deepEqual(errors, [actionsutil.ErrCheckoutWrongHead]);
|
||||
t.deepEqual(errors, [actionsutil.WorkflowErrors.CheckoutWrongHead]);
|
||||
});
|
||||
|
||||
test("formatWorkflowErrors() when there is one error", (t) => {
|
||||
const message = actionsutil.formatWorkflowErrors([
|
||||
actionsutil.ErrCheckoutWrongHead,
|
||||
actionsutil.WorkflowErrors.CheckoutWrongHead,
|
||||
]);
|
||||
t.true(message.startsWith("1 issue was detected with this workflow:"));
|
||||
});
|
||||
|
||||
test("formatWorkflowErrors() when there are multiple errors", (t) => {
|
||||
const message = actionsutil.formatWorkflowErrors([
|
||||
actionsutil.ErrCheckoutWrongHead,
|
||||
actionsutil.ErrPathsSpecified,
|
||||
actionsutil.WorkflowErrors.CheckoutWrongHead,
|
||||
actionsutil.WorkflowErrors.PathsSpecified,
|
||||
]);
|
||||
t.true(message.startsWith("2 issues were detected with this workflow:"));
|
||||
});
|
||||
|
||||
test("formatWorkflowCause()", (t) => {
|
||||
const message = actionsutil.formatWorkflowCause([
|
||||
actionsutil.ErrCheckoutWrongHead,
|
||||
actionsutil.ErrPathsSpecified,
|
||||
actionsutil.WorkflowErrors.CheckoutWrongHead,
|
||||
actionsutil.WorkflowErrors.PathsSpecified,
|
||||
]);
|
||||
|
||||
t.deepEqual(message, "CheckoutWrongHead,PathsSpecified");
|
||||
|
|
|
|||
|
|
@ -135,44 +135,37 @@ enum MissingTriggers {
|
|||
PullRequest = 2,
|
||||
}
|
||||
|
||||
interface WorkflowError {
|
||||
interface CodedError {
|
||||
message: string;
|
||||
code: string;
|
||||
}
|
||||
|
||||
export const ErrCheckoutWrongHead = {
|
||||
message: `Git checkout HEAD^2 is no longer necessary. Please remove this line.`,
|
||||
code: "CheckoutWrongHead",
|
||||
};
|
||||
export const ErrMismatchedBranches = {
|
||||
message: `Please make sure that every branch in on.pull_request is also in on.push so that CodeQL can establish a baseline.`,
|
||||
code: "MismatchedBranches",
|
||||
};
|
||||
export const ErrMissingHooks = {
|
||||
message: `Please specify on.push and on.pull_request hooks.`,
|
||||
code: "MissingHooks",
|
||||
};
|
||||
export const ErrMissingPushHook = {
|
||||
message: `Please specify an on.push hook so CodeQL can establish a baseline.`,
|
||||
code: "MissingPushHook",
|
||||
};
|
||||
export const ErrMissingPullRequestHook = {
|
||||
message: `Please specify an on.pull_request hook so CodeQL is run against new pull requests.`,
|
||||
code: "MissingPullRequestHook",
|
||||
};
|
||||
export const ErrPathsSpecified = {
|
||||
message: `Please do not specify paths at on.pull.`,
|
||||
code: "PathsSpecified",
|
||||
};
|
||||
function toCodedErrors(errors: {
|
||||
[key: string]: string;
|
||||
}): { [key: string]: CodedError } {
|
||||
return Object.entries(errors).reduce((acc, [key, value]) => {
|
||||
acc[key] = { message: value, code: key };
|
||||
return acc;
|
||||
}, {} as ReturnType<typeof toCodedErrors>);
|
||||
}
|
||||
|
||||
export function validateWorkflow(doc: Workflow): WorkflowError[] {
|
||||
const errors: WorkflowError[] = [];
|
||||
export const WorkflowErrors = toCodedErrors({
|
||||
MismatchedBranches: `Please make sure that every branch in on.pull_request is also in on.push so that CodeQL can establish a baseline.`,
|
||||
MissingHooks: `Please specify on.push and on.pull_request hooks.`,
|
||||
MissingPullRequestHook: `Please specify an on.pull_request hook so CodeQL is run against new pull requests.`,
|
||||
MissingPushHook: `Please specify an on.push hook so CodeQL can establish a baseline.`,
|
||||
PathsSpecified: `Please do not specify paths at on.pull.`,
|
||||
CheckoutWrongHead: `Git checkout HEAD^2 is no longer necessary. Please remove this line.`,
|
||||
});
|
||||
|
||||
export function validateWorkflow(doc: Workflow): CodedError[] {
|
||||
const errors: CodedError[] = [];
|
||||
|
||||
// .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(ErrCheckoutWrongHead);
|
||||
errors.push(WorkflowErrors.CheckoutWrongHead);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -209,7 +202,7 @@ export function validateWorkflow(doc: Workflow): WorkflowError[] {
|
|||
} else {
|
||||
const paths = doc.on.push?.paths;
|
||||
if (Array.isArray(paths) && paths.length > 0) {
|
||||
errors.push(ErrPathsSpecified);
|
||||
errors.push(WorkflowErrors.PathsSpecified);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -222,32 +215,30 @@ export function validateWorkflow(doc: Workflow): WorkflowError[] {
|
|||
(value) => !push.includes(value)
|
||||
);
|
||||
if (intersects.length > 0) {
|
||||
errors.push(ErrMismatchedBranches);
|
||||
errors.push(WorkflowErrors.MismatchedBranches);
|
||||
}
|
||||
} else if (push.length > 0) {
|
||||
errors.push(ErrMismatchedBranches);
|
||||
errors.push(WorkflowErrors.MismatchedBranches);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch (missing) {
|
||||
case MissingTriggers.PullRequest | MissingTriggers.Push:
|
||||
errors.push(ErrMissingHooks);
|
||||
errors.push(WorkflowErrors.MissingHooks);
|
||||
break;
|
||||
case MissingTriggers.PullRequest:
|
||||
errors.push(ErrMissingPullRequestHook);
|
||||
errors.push(WorkflowErrors.MissingPullRequestHook);
|
||||
break;
|
||||
case MissingTriggers.Push:
|
||||
errors.push(ErrMissingPushHook);
|
||||
errors.push(WorkflowErrors.MissingPushHook);
|
||||
break;
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
export async function getWorkflowErrors(): Promise<
|
||||
WorkflowError[] | undefined
|
||||
> {
|
||||
export async function getWorkflowErrors(): Promise<CodedError[] | undefined> {
|
||||
const workflow = await getWorkflow();
|
||||
|
||||
if (workflow === undefined) {
|
||||
|
|
@ -263,15 +254,17 @@ export async function getWorkflowErrors(): Promise<
|
|||
return workflowErrors;
|
||||
}
|
||||
|
||||
export function formatWorkflowErrors(errors: WorkflowError[]): string {
|
||||
return `${errors.length} issue${
|
||||
errors.length === 1 ? " was" : "s were"
|
||||
} detected with this workflow: ${errors.map((e) => e.message).join(", ")}`;
|
||||
export function formatWorkflowErrors(errors: CodedError[]): string {
|
||||
const issuesWere = errors.length === 1 ? "issue was" : "issues were";
|
||||
|
||||
return `${
|
||||
errors.length
|
||||
} ${issuesWere} detected with this workflow: ${errors
|
||||
.map((e) => e.message)
|
||||
.join(", ")}`;
|
||||
}
|
||||
|
||||
export function formatWorkflowCause(
|
||||
errors?: WorkflowError[]
|
||||
): undefined | string {
|
||||
export function formatWorkflowCause(errors?: CodedError[]): undefined | string {
|
||||
if (errors === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue