Improve method naming
This commit is contained in:
parent
e09fbf5b4a
commit
3224214d91
6 changed files with 104 additions and 47 deletions
37
lib/init-action-post-helper.js
generated
37
lib/init-action-post-helper.js
generated
|
|
@ -19,7 +19,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.run = exports.uploadSarifIfRunFailed = exports.uploadFailedSarif = void 0;
|
||||
exports.run = exports.tryUploadSarifIfRunFailed = void 0;
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const actionsUtil = __importStar(require("./actions-util"));
|
||||
const codeql_1 = require("./codeql");
|
||||
|
|
@ -35,7 +35,11 @@ function createFailedUploadFailedSarifResult(error) {
|
|||
upload_failed_run_stack_trace: error instanceof Error ? error.stack : undefined,
|
||||
};
|
||||
}
|
||||
async function uploadFailedSarif(config, repositoryNwo, featureEnablement, logger) {
|
||||
/**
|
||||
* Upload a failed SARIF file if we can verify that SARIF upload is enabled and determine the SARIF
|
||||
* category for the workflow.
|
||||
*/
|
||||
async function maybeUploadFailedSarif(config, repositoryNwo, featureEnablement, logger) {
|
||||
var _a;
|
||||
if (!config.codeQLCmd) {
|
||||
logger.warning("CodeQL command not found. Unable to upload failed SARIF file.");
|
||||
|
|
@ -63,40 +67,41 @@ async function uploadFailedSarif(config, repositoryNwo, featureEnablement, logge
|
|||
await uploadLib.waitForProcessing(repositoryNwo, uploadResult.sarifID, logger, { isUnsuccessfulExecution: true });
|
||||
return (_a = uploadResult === null || uploadResult === void 0 ? void 0 : uploadResult.statusReport) !== null && _a !== void 0 ? _a : {};
|
||||
}
|
||||
exports.uploadFailedSarif = uploadFailedSarif;
|
||||
async function uploadSarifIfRunFailed(config, repositoryNwo, featureEnablement, logger) {
|
||||
// This environment variable is used to integration test uploading a SARIF file for failed runs
|
||||
const expectFailedSarifUpload = process.env["CODEQL_ACTION_EXPECT_UPLOAD_FAILED_SARIF"] === "true";
|
||||
async function tryUploadSarifIfRunFailed(config, repositoryNwo, featureEnablement, logger) {
|
||||
if (process.env[shared_environment_1.CODEQL_ACTION_ANALYZE_DID_COMPLETE_SUCCESSFULLY] !== "true") {
|
||||
try {
|
||||
return await uploadFailedSarif(config, repositoryNwo, featureEnablement, logger);
|
||||
return await maybeUploadFailedSarif(config, repositoryNwo, featureEnablement, logger);
|
||||
}
|
||||
catch (e) {
|
||||
if (expectFailedSarifUpload) {
|
||||
throw new Error("Expected to upload a SARIF file for this failed CodeQL code scanning run, but encountered " +
|
||||
`the following error: ${e}`);
|
||||
}
|
||||
logger.debug(`Failed to upload a SARIF file for this failed CodeQL code scanning run. ${e}`);
|
||||
return createFailedUploadFailedSarifResult(e);
|
||||
}
|
||||
}
|
||||
else if (expectFailedSarifUpload) {
|
||||
throw new Error("Expected to upload a SARIF file for this failed CodeQL code scanning run, but didn't.");
|
||||
}
|
||||
else {
|
||||
return {
|
||||
upload_failed_run_skipped_because: "Analyze Action completed successfully",
|
||||
};
|
||||
}
|
||||
}
|
||||
exports.uploadSarifIfRunFailed = uploadSarifIfRunFailed;
|
||||
exports.tryUploadSarifIfRunFailed = tryUploadSarifIfRunFailed;
|
||||
async function run(uploadDatabaseBundleDebugArtifact, uploadLogsDebugArtifact, printDebugLogs, repositoryNwo, featureEnablement, logger) {
|
||||
const config = await (0, config_utils_1.getConfig)(actionsUtil.getTemporaryDirectory(), logger);
|
||||
if (config === undefined) {
|
||||
logger.warning("Debugging artifacts are unavailable since the 'init' Action failed before it could produce any.");
|
||||
return;
|
||||
}
|
||||
const uploadFailedSarifResult = await uploadSarifIfRunFailed(config, repositoryNwo, featureEnablement, logger);
|
||||
const uploadFailedSarifResult = await tryUploadSarifIfRunFailed(config, repositoryNwo, featureEnablement, logger);
|
||||
if (uploadFailedSarifResult.upload_failed_run_skipped_because) {
|
||||
logger.debug("Skipped uploading a failed SARIF file for this CodeQL code scanning run because: " +
|
||||
`${uploadFailedSarifResult.upload_failed_run_skipped_because}.`);
|
||||
}
|
||||
// Throw an error if in integration tests, we expected to upload a SARIF file for a failed run
|
||||
// but we didn't upload anything.
|
||||
if (process.env["CODEQL_ACTION_EXPECT_UPLOAD_FAILED_SARIF"] === "true" &&
|
||||
!uploadFailedSarifResult.raw_upload_size_bytes) {
|
||||
throw new Error("Expected to upload a failed SARIF file for this CodeQL code scanning run, " +
|
||||
`but the result was instead ${uploadFailedSarifResult}.`);
|
||||
}
|
||||
// Upload appropriate Actions artifacts for debugging
|
||||
if (config.debugMode) {
|
||||
core.info("Debug mode is on. Uploading available database bundles and logs as Actions debugging artifacts...");
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"init-action-post-helper.js","sourceRoot":"","sources":["../src/init-action-post-helper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAsC;AAEtC,4DAA8C;AAC9C,qCAAqC;AACrC,iDAAmD;AACnD,mDAA6D;AAG7D,6DAAuF;AACvF,wDAA0C;AAC1C,iCAA6E;AAC7E,yCAKoB;AAWpB,SAAS,mCAAmC,CAC1C,KAAc;IAEd,OAAO;QACL,uBAAuB,EACrB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;QACxD,6BAA6B,EAC3B,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;KACnD,CAAC;AACJ,CAAC;AAEM,KAAK,UAAU,iBAAiB,CACrC,MAAc,EACd,aAA4B,EAC5B,iBAAoC,EACpC,MAAc;;IAEd,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;QACrB,MAAM,CAAC,OAAO,CACZ,+DAA+D,CAChE,CAAC;QACF,OAAO,EAAE,iCAAiC,EAAE,0BAA0B,EAAE,CAAC;KAC1E;IACD,MAAM,MAAM,GAAG,MAAM,IAAA,kBAAS,EAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACjD,IACE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C1D,MAAM,EACN,aAAa,EACb,iBAAiB,EACjB,MAAM,CACP,CAAC;IAEF,qDAAqD;IACrD,IAAI,MAAM,CAAC,SAAS,EAAE;QACpB,IAAI,CAAC,IAAI,CACP,mGAAmG,CACpG,CAAC;QACF,MAAM,iCAAiC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACxD,MAAM,uBAAuB,CAAC,MAAM,CAAC,CAAC;QAEtC,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;KAC9B;IAED,OAAO,uBAAuB,CAAC;AACjC,CAAC;AAnCD,kBAmCC"}
|
||||
{"version":3,"file":"init-action-post-helper.js","sourceRoot":"","sources":["../src/init-action-post-helper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAsC;AAEtC,4DAA8C;AAC9C,qCAAqC;AACrC,iDAAmD;AACnD,mDAA6D;AAG7D,6DAAuF;AACvF,wDAA0C;AAC1C,iCAA6E;AAC7E,yCAKoB;AAWpB,SAAS,mCAAmC,CAC1C,KAAc;IAEd,OAAO;QACL,uBAAuB,EACrB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;QACxD,6BAA6B,EAC3B,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;KACnD,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,KAAK,UAAU,sBAAsB,CACnC,MAAc,EACd,aAA4B,EAC5B,iBAAoC,EACpC,MAAc;;IAEd,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;QACrB,MAAM,CAAC,OAAO,CACZ,+DAA+D,CAChE,CAAC;QACF,OAAO,EAAE,iCAAiC,EAAE,0BAA0B,EAAE,CAAC;KAC1E;IACD,MAAM,MAAM,GAAG,MAAM,IAAA,kBAAS,EAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACjD,IACE,CAAC,CAA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AC9C;QACA,MAAM,IAAI,KAAK,CACb,4EAA4E;YAC1E,8BAA8B,uBAAuB,GAAG,CAC3D,CAAC;KACH;IAED,qDAAqD;IACrD,IAAI,MAAM,CAAC,SAAS,EAAE;QACpB,IAAI,CAAC,IAAI,CACP,mGAAmG,CACpG,CAAC;QACF,MAAM,iCAAiC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACxD,MAAM,uBAAuB,CAAC,MAAM,CAAC,CAAC;QAEtC,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;KAC9B;IAED,OAAO,uBAAuB,CAAC;AACjC,CAAC;AApDD,kBAoDC"}
|
||||
31
lib/init-action-post-helper.test.js
generated
31
lib/init-action-post-helper.test.js
generated
|
|
@ -125,7 +125,7 @@ const workflow = __importStar(require("./workflow"));
|
|||
},
|
||||
},
|
||||
]);
|
||||
await testFailedSarifUpload(t, actionsWorkflow, {
|
||||
const result = await testFailedSarifUpload(t, actionsWorkflow, {
|
||||
expectedLogs: [
|
||||
{
|
||||
message: "Won't upload a failed SARIF file since SARIF upload is disabled.",
|
||||
|
|
@ -134,6 +134,7 @@ const workflow = __importStar(require("./workflow"));
|
|||
],
|
||||
expectUpload: false,
|
||||
});
|
||||
t.is(result.upload_failed_run_skipped_because, "SARIF upload is disabled");
|
||||
});
|
||||
(0, ava_1.default)("uploading failed SARIF run fails when workflow does not reference github/codeql-action", async (t) => {
|
||||
const actionsWorkflow = createTestWorkflow([
|
||||
|
|
@ -142,7 +143,19 @@ const workflow = __importStar(require("./workflow"));
|
|||
uses: "actions/checkout@v3",
|
||||
},
|
||||
]);
|
||||
await t.throwsAsync(async () => await testFailedSarifUpload(t, actionsWorkflow));
|
||||
const expectedError = "Could not get upload input to github/codeql-action/analyze since the analyze job does not " +
|
||||
"call github/codeql-action/analyze.";
|
||||
const result = await testFailedSarifUpload(t, actionsWorkflow, {
|
||||
expectedLogs: [
|
||||
{
|
||||
message: `Failed to upload a SARIF file for this failed CodeQL code scanning run. Error: ${expectedError}`,
|
||||
type: "debug",
|
||||
},
|
||||
],
|
||||
expectUpload: false,
|
||||
});
|
||||
t.is(result.upload_failed_run_error, expectedError);
|
||||
t.truthy(result.upload_failed_run_stack_trace);
|
||||
});
|
||||
function createTestWorkflow(steps) {
|
||||
return {
|
||||
|
|
@ -182,9 +195,18 @@ async function testFailedSarifUpload(t, actionsWorkflow, { category, expectedLog
|
|||
const diagnosticsExportStub = sinon.stub(codeqlObject, "diagnosticsExport");
|
||||
sinon.stub(workflow, "getWorkflow").resolves(actionsWorkflow);
|
||||
const uploadFromActions = sinon.stub(uploadLib, "uploadFromActions");
|
||||
uploadFromActions.resolves({ sarifID: "42" });
|
||||
uploadFromActions.resolves({
|
||||
sarifID: "42",
|
||||
statusReport: { raw_upload_size_bytes: 20, zipped_upload_size_bytes: 10 },
|
||||
});
|
||||
const waitForProcessing = sinon.stub(uploadLib, "waitForProcessing");
|
||||
await initActionPostHelper.uploadFailedSarif(config, (0, repository_1.parseRepositoryNwo)("github/codeql-action"), (0, testing_utils_1.createFeatures)([feature_flags_1.Feature.UploadFailedSarifEnabled]), (0, testing_utils_1.getRecordingLogger)(messages));
|
||||
const result = await initActionPostHelper.tryUploadSarifIfRunFailed(config, (0, repository_1.parseRepositoryNwo)("github/codeql-action"), (0, testing_utils_1.createFeatures)([feature_flags_1.Feature.UploadFailedSarifEnabled]), (0, testing_utils_1.getRecordingLogger)(messages));
|
||||
if (expectUpload) {
|
||||
t.deepEqual(result, {
|
||||
raw_upload_size_bytes: 20,
|
||||
zipped_upload_size_bytes: 10,
|
||||
});
|
||||
}
|
||||
t.deepEqual(messages, expectedLogs);
|
||||
if (expectUpload) {
|
||||
t.true(diagnosticsExportStub.calledOnceWith(sinon.match.string, category), `Actual args were: ${diagnosticsExportStub.args}`);
|
||||
|
|
@ -198,5 +220,6 @@ async function testFailedSarifUpload(t, actionsWorkflow, { category, expectedLog
|
|||
t.true(uploadFromActions.notCalled);
|
||||
t.true(waitForProcessing.notCalled);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//# sourceMappingURL=init-action-post-helper.test.js.map
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -134,7 +134,7 @@ test("doesn't upload failed SARIF for workflow with upload: false", async (t) =>
|
|||
},
|
||||
},
|
||||
]);
|
||||
await testFailedSarifUpload(t, actionsWorkflow, {
|
||||
const result = await testFailedSarifUpload(t, actionsWorkflow, {
|
||||
expectedLogs: [
|
||||
{
|
||||
message:
|
||||
|
|
@ -144,6 +144,7 @@ test("doesn't upload failed SARIF for workflow with upload: false", async (t) =>
|
|||
],
|
||||
expectUpload: false,
|
||||
});
|
||||
t.is(result.upload_failed_run_skipped_because, "SARIF upload is disabled");
|
||||
});
|
||||
|
||||
test("uploading failed SARIF run fails when workflow does not reference github/codeql-action", async (t) => {
|
||||
|
|
@ -153,9 +154,20 @@ test("uploading failed SARIF run fails when workflow does not reference github/c
|
|||
uses: "actions/checkout@v3",
|
||||
},
|
||||
]);
|
||||
await t.throwsAsync(
|
||||
async () => await testFailedSarifUpload(t, actionsWorkflow)
|
||||
);
|
||||
const expectedError =
|
||||
"Could not get upload input to github/codeql-action/analyze since the analyze job does not " +
|
||||
"call github/codeql-action/analyze.";
|
||||
const result = await testFailedSarifUpload(t, actionsWorkflow, {
|
||||
expectedLogs: [
|
||||
{
|
||||
message: `Failed to upload a SARIF file for this failed CodeQL code scanning run. Error: ${expectedError}`,
|
||||
type: "debug",
|
||||
},
|
||||
],
|
||||
expectUpload: false,
|
||||
});
|
||||
t.is(result.upload_failed_run_error, expectedError);
|
||||
t.truthy(result.upload_failed_run_stack_trace);
|
||||
});
|
||||
|
||||
function createTestWorkflow(
|
||||
|
|
@ -193,7 +205,7 @@ async function testFailedSarifUpload(
|
|||
expectedLogs?: LoggedMessage[];
|
||||
expectUpload?: boolean;
|
||||
} = {}
|
||||
): Promise<void> {
|
||||
): Promise<initActionPostHelper.UploadFailedSarifResult> {
|
||||
const config = {
|
||||
codeQLCmd: "codeql",
|
||||
debugMode: true,
|
||||
|
|
@ -214,15 +226,24 @@ async function testFailedSarifUpload(
|
|||
sinon.stub(workflow, "getWorkflow").resolves(actionsWorkflow);
|
||||
|
||||
const uploadFromActions = sinon.stub(uploadLib, "uploadFromActions");
|
||||
uploadFromActions.resolves({ sarifID: "42" } as uploadLib.UploadResult);
|
||||
uploadFromActions.resolves({
|
||||
sarifID: "42",
|
||||
statusReport: { raw_upload_size_bytes: 20, zipped_upload_size_bytes: 10 },
|
||||
} as uploadLib.UploadResult);
|
||||
const waitForProcessing = sinon.stub(uploadLib, "waitForProcessing");
|
||||
|
||||
await initActionPostHelper.uploadFailedSarif(
|
||||
const result = await initActionPostHelper.tryUploadSarifIfRunFailed(
|
||||
config,
|
||||
parseRepositoryNwo("github/codeql-action"),
|
||||
createFeatures([Feature.UploadFailedSarifEnabled]),
|
||||
getRecordingLogger(messages)
|
||||
);
|
||||
if (expectUpload) {
|
||||
t.deepEqual(result, {
|
||||
raw_upload_size_bytes: 20,
|
||||
zipped_upload_size_bytes: 10,
|
||||
});
|
||||
}
|
||||
t.deepEqual(messages, expectedLogs);
|
||||
if (expectUpload) {
|
||||
t.true(
|
||||
|
|
@ -248,4 +269,5 @@ async function testFailedSarifUpload(
|
|||
t.true(uploadFromActions.notCalled);
|
||||
t.true(waitForProcessing.notCalled);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,11 @@ function createFailedUploadFailedSarifResult(
|
|||
};
|
||||
}
|
||||
|
||||
export async function uploadFailedSarif(
|
||||
/**
|
||||
* Upload a failed SARIF file if we can verify that SARIF upload is enabled and determine the SARIF
|
||||
* category for the workflow.
|
||||
*/
|
||||
async function maybeUploadFailedSarif(
|
||||
config: Config,
|
||||
repositoryNwo: RepositoryNwo,
|
||||
featureEnablement: FeatureEnablement,
|
||||
|
|
@ -92,40 +96,26 @@ export async function uploadFailedSarif(
|
|||
return uploadResult?.statusReport ?? {};
|
||||
}
|
||||
|
||||
export async function uploadSarifIfRunFailed(
|
||||
export async function tryUploadSarifIfRunFailed(
|
||||
config: Config,
|
||||
repositoryNwo: RepositoryNwo,
|
||||
featureEnablement: FeatureEnablement,
|
||||
logger: Logger
|
||||
): Promise<UploadFailedSarifResult> {
|
||||
// This environment variable is used to integration test uploading a SARIF file for failed runs
|
||||
const expectFailedSarifUpload =
|
||||
process.env["CODEQL_ACTION_EXPECT_UPLOAD_FAILED_SARIF"] === "true";
|
||||
|
||||
if (process.env[CODEQL_ACTION_ANALYZE_DID_COMPLETE_SUCCESSFULLY] !== "true") {
|
||||
try {
|
||||
return await uploadFailedSarif(
|
||||
return await maybeUploadFailedSarif(
|
||||
config,
|
||||
repositoryNwo,
|
||||
featureEnablement,
|
||||
logger
|
||||
);
|
||||
} catch (e) {
|
||||
if (expectFailedSarifUpload) {
|
||||
throw new Error(
|
||||
"Expected to upload a SARIF file for this failed CodeQL code scanning run, but encountered " +
|
||||
`the following error: ${e}`
|
||||
);
|
||||
}
|
||||
logger.debug(
|
||||
`Failed to upload a SARIF file for this failed CodeQL code scanning run. ${e}`
|
||||
);
|
||||
return createFailedUploadFailedSarifResult(e);
|
||||
}
|
||||
} else if (expectFailedSarifUpload) {
|
||||
throw new Error(
|
||||
"Expected to upload a SARIF file for this failed CodeQL code scanning run, but didn't."
|
||||
);
|
||||
} else {
|
||||
return {
|
||||
upload_failed_run_skipped_because:
|
||||
|
|
@ -150,12 +140,29 @@ export async function run(
|
|||
return;
|
||||
}
|
||||
|
||||
const uploadFailedSarifResult = await uploadSarifIfRunFailed(
|
||||
const uploadFailedSarifResult = await tryUploadSarifIfRunFailed(
|
||||
config,
|
||||
repositoryNwo,
|
||||
featureEnablement,
|
||||
logger
|
||||
);
|
||||
if (uploadFailedSarifResult.upload_failed_run_skipped_because) {
|
||||
logger.debug(
|
||||
"Skipped uploading a failed SARIF file for this CodeQL code scanning run because: " +
|
||||
`${uploadFailedSarifResult.upload_failed_run_skipped_because}.`
|
||||
);
|
||||
}
|
||||
// Throw an error if in integration tests, we expected to upload a SARIF file for a failed run
|
||||
// but we didn't upload anything.
|
||||
if (
|
||||
process.env["CODEQL_ACTION_EXPECT_UPLOAD_FAILED_SARIF"] === "true" &&
|
||||
!uploadFailedSarifResult.raw_upload_size_bytes
|
||||
) {
|
||||
throw new Error(
|
||||
"Expected to upload a failed SARIF file for this CodeQL code scanning run, " +
|
||||
`but the result was instead ${uploadFailedSarifResult}.`
|
||||
);
|
||||
}
|
||||
|
||||
// Upload appropriate Actions artifacts for debugging
|
||||
if (config.debugMode) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue