Send short codes that do not need URL encoding for better splunk tracing

This commit is contained in:
Simon Engledew 2020-11-24 12:43:08 +00:00
parent 6df1fc5e38
commit 2ac22e8935
No known key found for this signature in database
GPG key ID: 84302E7B02FE8BCE
10 changed files with 215 additions and 72 deletions

90
lib/actions-util.js generated
View file

@ -103,16 +103,34 @@ function isObject(o) {
}
var MissingTriggers;
(function (MissingTriggers) {
MissingTriggers[MissingTriggers["NONE"] = 0] = "NONE";
MissingTriggers[MissingTriggers["PUSH"] = 1] = "PUSH";
MissingTriggers[MissingTriggers["PULL_REQUEST"] = 2] = "PULL_REQUEST";
MissingTriggers[MissingTriggers["None"] = 0] = "None";
MissingTriggers[MissingTriggers["Push"] = 1] = "Push";
MissingTriggers[MissingTriggers["PullRequest"] = 2] = "PullRequest";
})(MissingTriggers || (MissingTriggers = {}));
exports.ErrCheckoutWrongHead = `Git checkout HEAD^2 is no longer necessary. Please remove this line.`;
exports.ErrMismatchedBranches = `Please make sure that every branch in on.pull_request is also in on.push so that CodeQL can establish a baseline.`;
exports.ErrMissingHooks = `Please specify on.push and on.pull_request hooks.`;
exports.ErrMissingPushHook = `Please specify an on.push hook so CodeQL can establish a baseline.`;
exports.ErrMissingPullRequestHook = `Please specify an on.pull_request hook so CodeQL is run against new pull requests.`;
exports.ErrPathsSpecified = `Please do not specify paths at on.pull.`;
exports.ErrCheckoutWrongHead = {
message: `Git checkout HEAD^2 is no longer necessary. Please remove this line.`,
code: "CheckoutWrongHead",
};
exports.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",
};
exports.ErrMissingHooks = {
message: `Please specify on.push and on.pull_request hooks.`,
code: "MissingHooks",
};
exports.ErrMissingPushHook = {
message: `Please specify an on.push hook so CodeQL can establish a baseline.`,
code: "MissingPushHook",
};
exports.ErrMissingPullRequestHook = {
message: `Please specify an on.pull_request hook so CodeQL is run against new pull requests.`,
code: "MissingPullRequestHook",
};
exports.ErrPathsSpecified = {
message: `Please do not specify paths at on.pull.`,
code: "PathsSpecified",
};
function validateWorkflow(doc) {
var _a, _b, _c, _d;
const errors = [];
@ -124,37 +142,37 @@ function validateWorkflow(doc) {
}
}
}
let missing = MissingTriggers.NONE;
let missing = MissingTriggers.None;
if (doc.on === undefined) {
missing = MissingTriggers.PUSH | MissingTriggers.PULL_REQUEST;
missing = MissingTriggers.Push | MissingTriggers.PullRequest;
}
else if (typeof doc.on === "string") {
switch (doc.on) {
case "push":
missing = MissingTriggers.PULL_REQUEST;
missing = MissingTriggers.PullRequest;
break;
case "pull_request":
missing = MissingTriggers.PUSH;
missing = MissingTriggers.Push;
break;
default:
missing = MissingTriggers.PUSH | MissingTriggers.PULL_REQUEST;
missing = MissingTriggers.Push | MissingTriggers.PullRequest;
break;
}
}
else if (Array.isArray(doc.on)) {
if (!doc.on.includes("push")) {
missing = missing | MissingTriggers.PUSH;
missing = missing | MissingTriggers.Push;
}
if (!doc.on.includes("pull_request")) {
missing = missing | MissingTriggers.PULL_REQUEST;
missing = missing | MissingTriggers.PullRequest;
}
}
else if (isObject(doc.on)) {
if (!Object.prototype.hasOwnProperty.call(doc.on, "pull_request")) {
missing = missing | MissingTriggers.PULL_REQUEST;
missing = missing | MissingTriggers.PullRequest;
}
if (!Object.prototype.hasOwnProperty.call(doc.on, "push")) {
missing = missing | MissingTriggers.PUSH;
missing = missing | MissingTriggers.Push;
}
else {
const paths = (_d = doc.on.push) === null || _d === void 0 ? void 0 : _d.paths;
@ -162,30 +180,35 @@ function validateWorkflow(doc) {
errors.push(exports.ErrPathsSpecified);
}
}
if (doc.on.push && doc.on.pull_request) {
if (doc.on.push) {
const push = doc.on.push.branches || [];
const pull_request = doc.on.pull_request.branches || [];
const intersects = pull_request.filter((value) => !push.includes(value));
if (intersects.length > 0) {
if (doc.on.pull_request) {
const pull_request = doc.on.pull_request.branches || [];
const intersects = pull_request.filter((value) => !push.includes(value));
if (intersects.length > 0) {
errors.push(exports.ErrMismatchedBranches);
}
}
else if (push.length > 0) {
errors.push(exports.ErrMismatchedBranches);
}
}
}
switch (missing) {
case MissingTriggers.PULL_REQUEST | MissingTriggers.PUSH:
case MissingTriggers.PullRequest | MissingTriggers.Push:
errors.push(exports.ErrMissingHooks);
break;
case MissingTriggers.PULL_REQUEST:
case MissingTriggers.PullRequest:
errors.push(exports.ErrMissingPullRequestHook);
break;
case MissingTriggers.PUSH:
case MissingTriggers.Push:
errors.push(exports.ErrMissingPushHook);
break;
}
return errors;
}
exports.validateWorkflow = validateWorkflow;
async function getWorkflowError() {
async function getWorkflowErrors() {
const workflow = await getWorkflow();
if (workflow === undefined) {
return undefined;
@ -194,9 +217,20 @@ async function getWorkflowError() {
if (workflowErrors.length === 0) {
return undefined;
}
return `${workflowErrors.length} issue${workflowErrors.length === 1 ? " was" : "s were"} detected with this workflow: ${workflowErrors.join(", ")}`;
return workflowErrors;
}
exports.getWorkflowError = getWorkflowError;
exports.getWorkflowErrors = getWorkflowErrors;
function formatWorkflowErrors(errors) {
return `${errors.length} issue${errors.length === 1 ? " was" : "s were"} detected with this workflow: ${errors.map((e) => e.message).join(", ")}`;
}
exports.formatWorkflowErrors = formatWorkflowErrors;
function formatWorkflowCause(errors) {
if (errors === undefined) {
return undefined;
}
return errors.map((e) => e.code).join(",");
}
exports.formatWorkflowCause = formatWorkflowCause;
async function getWorkflow() {
const relativePath = await getWorkflowPath();
const absolutePath = path.join(getRequiredEnvParam("GITHUB_WORKSPACE"), relativePath);

File diff suppressed because one or more lines are too long

View file

@ -146,6 +146,15 @@ ava_1.default("validateWorkflow() when on.push is mismatched for pull_request",
});
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
});
ava_1.default("validateWorkflow() when on.pull_request for every branch but push specifies branches", (t) => {
const errors = actionsutil.validateWorkflow({
on: {
push: { branches: ["main"] },
pull_request: null,
},
});
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
});
ava_1.default("validateWorkflow() when HEAD^2 is checked out", (t) => {
const errors = actionsutil.validateWorkflow({
on: ["push", "pull_request"],
@ -153,4 +162,25 @@ ava_1.default("validateWorkflow() when HEAD^2 is checked out", (t) => {
});
t.deepEqual(errors, [actionsutil.ErrCheckoutWrongHead]);
});
ava_1.default("formatWorkflowErrors() when there is one error", (t) => {
const message = actionsutil.formatWorkflowErrors([
actionsutil.ErrCheckoutWrongHead,
]);
t.true(message.startsWith("1 issue was detected with this workflow:"));
});
ava_1.default("formatWorkflowErrors() when there are multiple errors", (t) => {
const message = actionsutil.formatWorkflowErrors([
actionsutil.ErrCheckoutWrongHead,
actionsutil.ErrPathsSpecified,
]);
t.true(message.startsWith("2 issues were detected with this workflow:"));
});
ava_1.default("formatWorkflowCause()", (t) => {
const message = actionsutil.formatWorkflowCause([
actionsutil.ErrCheckoutWrongHead,
actionsutil.ErrPathsSpecified,
]);
t.deepEqual(message, "CheckoutWrongHead,PathsSpecified");
t.deepEqual(actionsutil.formatWorkflowCause(undefined), undefined);
});
//# sourceMappingURL=actions-util.test.js.map

File diff suppressed because one or more lines are too long

8
lib/init-action.js generated
View file

@ -55,11 +55,11 @@ async function run() {
let toolsVersion;
try {
actionsUtil.prepareLocalRunEnvironment();
const workflowError = await actionsUtil.getWorkflowError();
if (workflowError !== undefined) {
core.warning(workflowError);
const workflowErrors = await actionsUtil.getWorkflowErrors();
if (workflowErrors !== undefined) {
core.warning(actionsUtil.formatWorkflowErrors(workflowErrors));
}
if (!(await actionsUtil.sendStatusReport(await actionsUtil.createStatusReportBase("init", "starting", startedAt, workflowError)))) {
if (!(await actionsUtil.sendStatusReport(await actionsUtil.createStatusReportBase("init", "starting", startedAt, actionsUtil.formatWorkflowCause(workflowErrors))))) {
return;
}
const initCodeQLResult = await init_1.initCodeQL(actionsUtil.getOptionalInput("tools"), actionsUtil.getRequiredInput("token"), actionsUtil.getRequiredEnvParam("GITHUB_SERVER_URL"), actionsUtil.getRequiredEnvParam("RUNNER_TEMP"), actionsUtil.getRequiredEnvParam("RUNNER_TOOL_CACHE"), "actions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CAAC,CAAC;QAE7C,MAAM,YAAY,GAAG,MAAM,cAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACnD,IAAI,YAAY,KAAK,SAAS,EAAE;YAC9B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE;gBAC3D,IAAI,CAAC,cAAc,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;aACjC;YAED,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE;gBAChC,MAAM,0BAAmB,CACvB,mBAAmB,EACnB,SAAS,EACT,MAAM,EACN,MAAM,EACN,YAAY,CACb,CAAC;aACH;SACF;QAED,IAAI,CAAC,SAAS,CAAC,aAAa,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC;KACjD;IAAC,OAAO,KAAK,EAAE;QACd,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC9B,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACnB,MAAM,WAAW,CAAC,gBAAgB,CAChC,MAAM,WAAW,CAAC,sBAAsB,CACtC,MAAM,EACN,SAAS,EACT,SAAS,EACT,KAAK,CAAC,OAAO,EACb,KAAK,CAAC,KAAK,CACZ,CACF,CAAC;QACF,OAAO;KACR;IACD,MAAM,uBAAuB,CAAC,SAAS,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;AACjE,CAAC;AAED,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE;IAChB,IAAI,CAAC,SAAS,CAAC,uBAAuB,CAAC,EAAE,CAAC,CAAC;IAC3C,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACjB,CAAC,CAAC,CAAC"}

View file

@ -5,7 +5,7 @@
"description": "CodeQL action",
"scripts": {
"build": "tsc",
"test": "ava src/** --serial --verbose",
"test": "ava src/actions-util.test.ts --serial --verbose",
"lint": "eslint --report-unused-disable-directives --max-warnings=0 . --ext .js,.ts",
"lint-fix": "eslint --report-unused-disable-directives --max-warnings=0 . --ext .js,.ts --fix",
"removeNPMAbsolutePaths": "removeNPMAbsolutePaths . --force"

View file

@ -186,6 +186,17 @@ test("validateWorkflow() when on.push is mismatched for pull_request", (t) => {
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
});
test("validateWorkflow() when on.pull_request for every branch but push specifies branches", (t) => {
const errors = actionsutil.validateWorkflow({
on: {
push: { branches: ["main"] },
pull_request: null,
},
});
t.deepEqual(errors, [actionsutil.ErrMismatchedBranches]);
});
test("validateWorkflow() when HEAD^2 is checked out", (t) => {
const errors = actionsutil.validateWorkflow({
on: ["push", "pull_request"],
@ -194,3 +205,28 @@ test("validateWorkflow() when HEAD^2 is checked out", (t) => {
t.deepEqual(errors, [actionsutil.ErrCheckoutWrongHead]);
});
test("formatWorkflowErrors() when there is one error", (t) => {
const message = actionsutil.formatWorkflowErrors([
actionsutil.ErrCheckoutWrongHead,
]);
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,
]);
t.true(message.startsWith("2 issues were detected with this workflow:"));
});
test("formatWorkflowCause()", (t) => {
const message = actionsutil.formatWorkflowCause([
actionsutil.ErrCheckoutWrongHead,
actionsutil.ErrPathsSpecified,
]);
t.deepEqual(message, "CheckoutWrongHead,PathsSpecified");
t.deepEqual(actionsutil.formatWorkflowCause(undefined), undefined);
});

View file

@ -130,20 +130,43 @@ function isObject(o): o is object {
}
enum MissingTriggers {
NONE = 0,
PUSH = 1,
PULL_REQUEST = 2,
None = 0,
Push = 1,
PullRequest = 2,
}
export const ErrCheckoutWrongHead = `Git checkout HEAD^2 is no longer necessary. Please remove this line.`;
export const ErrMismatchedBranches = `Please make sure that every branch in on.pull_request is also in on.push so that CodeQL can establish a baseline.`;
export const ErrMissingHooks = `Please specify on.push and on.pull_request hooks.`;
export const ErrMissingPushHook = `Please specify an on.push hook so CodeQL can establish a baseline.`;
export const ErrMissingPullRequestHook = `Please specify an on.pull_request hook so CodeQL is run against new pull requests.`;
export const ErrPathsSpecified = `Please do not specify paths at on.pull.`;
interface WorkflowError {
message: string;
code: string;
}
export function validateWorkflow(doc: Workflow): string[] {
const errors: 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",
};
export function validateWorkflow(doc: Workflow): WorkflowError[] {
const errors: WorkflowError[] = [];
// .jobs[key].steps[].run
for (const job of Object.values(doc?.jobs || {})) {
@ -154,35 +177,35 @@ export function validateWorkflow(doc: Workflow): string[] {
}
}
let missing = MissingTriggers.NONE;
let missing = MissingTriggers.None;
if (doc.on === undefined) {
missing = MissingTriggers.PUSH | MissingTriggers.PULL_REQUEST;
missing = MissingTriggers.Push | MissingTriggers.PullRequest;
} else if (typeof doc.on === "string") {
switch (doc.on) {
case "push":
missing = MissingTriggers.PULL_REQUEST;
missing = MissingTriggers.PullRequest;
break;
case "pull_request":
missing = MissingTriggers.PUSH;
missing = MissingTriggers.Push;
break;
default:
missing = MissingTriggers.PUSH | MissingTriggers.PULL_REQUEST;
missing = MissingTriggers.Push | MissingTriggers.PullRequest;
break;
}
} else if (Array.isArray(doc.on)) {
if (!doc.on.includes("push")) {
missing = missing | MissingTriggers.PUSH;
missing = missing | MissingTriggers.Push;
}
if (!doc.on.includes("pull_request")) {
missing = missing | MissingTriggers.PULL_REQUEST;
missing = missing | MissingTriggers.PullRequest;
}
} else if (isObject(doc.on)) {
if (!Object.prototype.hasOwnProperty.call(doc.on, "pull_request")) {
missing = missing | MissingTriggers.PULL_REQUEST;
missing = missing | MissingTriggers.PullRequest;
}
if (!Object.prototype.hasOwnProperty.call(doc.on, "push")) {
missing = missing | MissingTriggers.PUSH;
missing = missing | MissingTriggers.Push;
} else {
const paths = doc.on.push?.paths;
if (Array.isArray(paths) && paths.length > 0) {
@ -190,26 +213,31 @@ export function validateWorkflow(doc: Workflow): string[] {
}
}
if (doc.on.push && doc.on.pull_request) {
if (doc.on.push) {
const push = doc.on.push.branches || [];
const pull_request = doc.on.pull_request.branches || [];
const intersects = pull_request.filter((value) => !push.includes(value));
if (intersects.length > 0) {
if (doc.on.pull_request) {
const pull_request = doc.on.pull_request.branches || [];
const intersects = pull_request.filter(
(value) => !push.includes(value)
);
if (intersects.length > 0) {
errors.push(ErrMismatchedBranches);
}
} else if (push.length > 0) {
errors.push(ErrMismatchedBranches);
}
}
}
switch (missing) {
case MissingTriggers.PULL_REQUEST | MissingTriggers.PUSH:
case MissingTriggers.PullRequest | MissingTriggers.Push:
errors.push(ErrMissingHooks);
break;
case MissingTriggers.PULL_REQUEST:
case MissingTriggers.PullRequest:
errors.push(ErrMissingPullRequestHook);
break;
case MissingTriggers.PUSH:
case MissingTriggers.Push:
errors.push(ErrMissingPushHook);
break;
}
@ -217,7 +245,9 @@ export function validateWorkflow(doc: Workflow): string[] {
return errors;
}
export async function getWorkflowError(): Promise<string | undefined> {
export async function getWorkflowErrors(): Promise<
WorkflowError[] | undefined
> {
const workflow = await getWorkflow();
if (workflow === undefined) {
@ -230,9 +260,22 @@ export async function getWorkflowError(): Promise<string | undefined> {
return undefined;
}
return `${workflowErrors.length} issue${
workflowErrors.length === 1 ? " was" : "s were"
} detected with this workflow: ${workflowErrors.join(", ")}`;
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 formatWorkflowCause(
errors?: WorkflowError[]
): undefined | string {
if (errors === undefined) {
return undefined;
}
return errors.map((e) => e.code).join(",");
}
export async function getWorkflow(): Promise<Workflow | undefined> {

View file

@ -96,10 +96,10 @@ async function run() {
try {
actionsUtil.prepareLocalRunEnvironment();
const workflowError = await actionsUtil.getWorkflowError();
const workflowErrors = await actionsUtil.getWorkflowErrors();
if (workflowError !== undefined) {
core.warning(workflowError);
if (workflowErrors !== undefined) {
core.warning(actionsUtil.formatWorkflowErrors(workflowErrors));
}
if (
@ -108,7 +108,7 @@ async function run() {
"init",
"starting",
startedAt,
workflowError
actionsUtil.formatWorkflowCause(workflowErrors)
)
))
) {