Address comments from review
- Change error messages. - Use logger instead of core - throw Error instead of write error message
This commit is contained in:
parent
04451e072f
commit
df9b50ee5f
6 changed files with 68 additions and 52 deletions
41
lib/init-action-post-helper.js
generated
41
lib/init-action-post-helper.js
generated
|
|
@ -24,7 +24,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.run = exports.tryUploadSarifIfRunFailed = void 0;
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const actionsUtil = __importStar(require("./actions-util"));
|
||||
const api_client_1 = require("./api-client");
|
||||
const codeql_1 = require("./codeql");
|
||||
|
|
@ -75,7 +74,7 @@ async function maybeUploadFailedSarif(config, repositoryNwo, features, logger) {
|
|||
// We call 'database export-diagnostics' to find any per-database diagnostics.
|
||||
await codeql.databaseExportDiagnostics(databasePath, sarifFile, category, config.tempDir, logger);
|
||||
}
|
||||
core.info(`Uploading failed SARIF file ${sarifFile}`);
|
||||
logger.info(`Uploading failed SARIF file ${sarifFile}`);
|
||||
const uploadResult = await uploadLib.uploadFromActions(sarifFile, checkoutPath, category, logger, { considerInvalidRequestUserError: false });
|
||||
await uploadLib.waitForProcessing(repositoryNwo, uploadResult.sarifID, logger, { isUnsuccessfulExecution: true });
|
||||
return uploadResult
|
||||
|
|
@ -119,11 +118,11 @@ async function run(uploadDatabaseBundleDebugArtifact, uploadLogsDebugArtifact, p
|
|||
`but the result was instead ${error}.`);
|
||||
}
|
||||
if (process.env["CODEQL_ACTION_EXPECT_UPLOAD_FAILED_SARIF"] === "true") {
|
||||
await removeUploadedSarif(uploadFailedSarifResult);
|
||||
await removeUploadedSarif(uploadFailedSarifResult, logger);
|
||||
}
|
||||
// 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...");
|
||||
logger.info("Debug mode is on. Uploading available database bundles and logs as Actions debugging artifacts...");
|
||||
await uploadDatabaseBundleDebugArtifact(config, logger);
|
||||
await uploadLogsDebugArtifact(config);
|
||||
await printDebugLogs(config);
|
||||
|
|
@ -131,14 +130,16 @@ async function run(uploadDatabaseBundleDebugArtifact, uploadLogsDebugArtifact, p
|
|||
return uploadFailedSarifResult;
|
||||
}
|
||||
exports.run = run;
|
||||
async function removeUploadedSarif(uploadFailedSarifResult) {
|
||||
async function removeUploadedSarif(uploadFailedSarifResult, logger) {
|
||||
const sarifID = uploadFailedSarifResult.sarifID;
|
||||
if (sarifID) {
|
||||
core.startGroup("Deleting failed SARIF upload");
|
||||
core.info(`Uploaded failed SARIF file with ID ${sarifID}. Because this is a test, the analysis associated with it will now be deleted.`);
|
||||
logger.startGroup("Deleting failed SARIF upload");
|
||||
logger.info(`In test mode, therefore deleting the failed analysis to avoid impacting tool status for the Action repository. SARIF ID to delete: ${sarifID}.`);
|
||||
const client = (0, api_client_1.getApiClient)();
|
||||
try {
|
||||
const repositoryNwo = (0, repository_1.parseRepositoryNwo)((0, util_1.getRequiredEnvParam)("GITHUB_REPOSITORY"));
|
||||
// Wait to make sure the analysis is ready for download before requesting it.
|
||||
await (0, util_1.wait)(5000);
|
||||
// Get the analysis associated with the uploaded sarif
|
||||
const analysisInfo = await client.request("GET /repos/:owner/:repo/code-scanning/analyses?sarif_id=:sarif_id", {
|
||||
owner: repositoryNwo.owner,
|
||||
|
|
@ -148,40 +149,36 @@ async function removeUploadedSarif(uploadFailedSarifResult) {
|
|||
// Delete the analysis.
|
||||
if (analysisInfo.data.length === 1) {
|
||||
const analysis = analysisInfo.data[0];
|
||||
core.info(`Deleting analysis ${analysis.id}`);
|
||||
logger.info(`Analysis ID to delete: ${analysis.id}.`);
|
||||
try {
|
||||
await client.request("DELETE /repos/:owner/:repo/code-scanning/analyses/:analysis_id?confirm_delete", {
|
||||
owner: repositoryNwo.owner,
|
||||
repo: repositoryNwo.repo,
|
||||
analysis_id: analysis.id,
|
||||
});
|
||||
logger.info(`Analysis deleted.`);
|
||||
}
|
||||
catch (e) {
|
||||
if (e instanceof Error &&
|
||||
e.message.includes("No analysis found for analysis ID")) {
|
||||
core.info(`Analysis ${analysis.id} does not exist. It was likely already deleted.`);
|
||||
}
|
||||
else {
|
||||
throw e;
|
||||
}
|
||||
const origMessage = (0, util_1.getErrorMessage)(e);
|
||||
const newMessage = origMessage.includes("No analysis found for analysis ID")
|
||||
? `Analysis ${analysis.id} does not exist. It was likely already deleted.`
|
||||
: origMessage;
|
||||
throw new Error(newMessage);
|
||||
}
|
||||
}
|
||||
else {
|
||||
core.warning(`Expected to find exactly one analysis with sarif_id ${sarifID}. Found ${analysisInfo.data.length}.`);
|
||||
throw new Error(`Expected to find exactly one analysis with sarif_id ${sarifID}. Found ${analysisInfo.data.length}.`);
|
||||
}
|
||||
core.endGroup();
|
||||
}
|
||||
catch (e) {
|
||||
// Fail the test if we can't delete the analysis.
|
||||
core.error("Failed to delete uploaded SARIF analysis.");
|
||||
throw e;
|
||||
throw new Error(`Failed to delete uploaded SARIF analysis. Reason: ${(0, util_1.getErrorMessage)(e)}`);
|
||||
}
|
||||
finally {
|
||||
core.endGroup();
|
||||
logger.endGroup();
|
||||
}
|
||||
}
|
||||
else {
|
||||
core.warning("Could not delete uploaded SARIF analysis because no sarifID was returned.");
|
||||
logger.warning("Could not delete the uploaded SARIF analysis because a SARIF ID wasn't provided by the API when uploading the SARIF file.");
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=init-action-post-helper.js.map
|
||||
Loading…
Add table
Add a link
Reference in a new issue