diff --git a/lib/finalize-db.js b/lib/finalize-db.js index ec7e6f440..caaa4ce83 100644 --- a/lib/finalize-db.js +++ b/lib/finalize-db.js @@ -125,7 +125,10 @@ async function run() { core.info('Analyzing database'); await runQueries(codeqlCmd, databaseFolder, sarifFolder, config); if ('true' === core.getInput('upload')) { - await upload_lib.upload(sarifFolder); + if (!await upload_lib.upload(sarifFolder)) { + await util.reportActionFailed('failed', 'upload'); + return; + } } } catch (error) { diff --git a/lib/upload-lib.js b/lib/upload-lib.js index 8942f36b1..6c8100d58 100644 --- a/lib/upload-lib.js +++ b/lib/upload-lib.js @@ -73,13 +73,13 @@ async function uploadPayload(payload) { const statusCode = res.message.statusCode; if (statusCode === 202) { core.info("Successfully uploaded results"); - return; + return true; } const requestID = res.message.headers["x-github-request-id"]; // On any other status code that's not 5xx mark the upload as failed if (!statusCode || statusCode < 500 || statusCode >= 600) { core.setFailed('Upload failed (' + requestID + '): (' + statusCode + ') ' + await res.readBody()); - return; + return false; } // On a 5xx status code we may retry the request if (attempt < backoffPeriods.length) { @@ -96,12 +96,14 @@ async function uploadPayload(payload) { // and not an error that the user has caused or can fix. // We avoid marking the job as failed to avoid breaking CI workflows. core.error('Upload failed (' + requestID + '): (' + statusCode + ') ' + await res.readBody()); - return; + return false; } } + return false; } // Uploads a single sarif file or a directory of sarif files // depending on what the path happens to refer to. +// Returns true iff the upload occurred and succeeded async function upload(input) { if (fs.lstatSync(input).isDirectory()) { const sarifFiles = fs.readdirSync(input) @@ -109,18 +111,20 @@ async function upload(input) { .map(f => path.resolve(input, f)); if (sarifFiles.length === 0) { core.setFailed("No SARIF files found to upload in \"" + input + "\"."); - return; + return false; } - await uploadFiles(sarifFiles); + return await uploadFiles(sarifFiles); } else { - await uploadFiles([input]); + return await uploadFiles([input]); } } exports.upload = upload; // Uploads the given set of sarif files. +// Returns true iff the upload occurred and succeeded async function uploadFiles(sarifFiles) { core.startGroup("Uploading results"); + let succeeded = false; try { // Check if an upload has happened before. If so then abort. // This is intended to catch when the finish and upload-sarif actions @@ -128,7 +132,7 @@ async function uploadFiles(sarifFiles) { const sentinelFile = await getSentinelFilePath(); if (fs.existsSync(sentinelFile)) { core.info("Aborting as an upload has already happened from this job"); - return; + return false; } const commitOid = util.getRequiredEnvParam('GITHUB_SHA'); const workflowRunIDStr = util.getRequiredEnvParam('GITHUB_RUN_ID'); @@ -144,7 +148,7 @@ async function uploadFiles(sarifFiles) { const workflowRunID = parseInt(workflowRunIDStr, 10); if (Number.isNaN(workflowRunID)) { core.setFailed('GITHUB_RUN_ID must define a non NaN workflow run ID'); - return; + return false; } let matrix = core.getInput('matrix'); if (matrix === "null" || matrix === "") { @@ -163,7 +167,7 @@ async function uploadFiles(sarifFiles) { "tool_names": toolNames, }); // Make the upload - await uploadPayload(payload); + succeeded = await uploadPayload(payload); // Mark that we have made an upload fs.writeFileSync(sentinelFile, ''); } @@ -171,4 +175,5 @@ async function uploadFiles(sarifFiles) { core.setFailed(error.message); } core.endGroup(); + return succeeded; } diff --git a/lib/upload-sarif.js b/lib/upload-sarif.js index 5bd8e593d..b22d2ac62 100644 --- a/lib/upload-sarif.js +++ b/lib/upload-sarif.js @@ -15,16 +15,20 @@ async function run() { return; } try { - await upload_lib.upload(core.getInput('sarif_file')); + if (await upload_lib.upload(core.getInput('sarif_file'))) { + await util.reportActionSucceeded('upload-sarif'); + } + else { + await util.reportActionFailed('upload-sarif', 'upload'); + } } catch (error) { core.setFailed(error.message); await util.reportActionFailed('upload-sarif', error.message, error.stack); return; } - await util.reportActionSucceeded('upload-sarif'); } run().catch(e => { - core.setFailed("upload-sarif action failed: " + e); + core.setFailed("codeql/upload-sarif action failed: " + e); console.log(e); }); diff --git a/src/finalize-db.ts b/src/finalize-db.ts index a4165e97e..f58ab6c1c 100644 --- a/src/finalize-db.ts +++ b/src/finalize-db.ts @@ -150,7 +150,10 @@ async function run() { await runQueries(codeqlCmd, databaseFolder, sarifFolder, config); if ('true' === core.getInput('upload')) { - await upload_lib.upload(sarifFolder); + if (!await upload_lib.upload(sarifFolder)) { + await util.reportActionFailed('failed', 'upload'); + return; + } } } catch (error) { diff --git a/src/upload-lib.ts b/src/upload-lib.ts index 32d5bed0c..7eb44a635 100644 --- a/src/upload-lib.ts +++ b/src/upload-lib.ts @@ -49,7 +49,7 @@ export function combineSarifFiles(sarifFiles: string[]): string { // Upload the given payload. // If the request fails then this will retry a small number of times. -async function uploadPayload(payload) { +async function uploadPayload(payload): Promise { core.info('Uploading results'); const githubToken = core.getInput('token'); @@ -71,7 +71,7 @@ async function uploadPayload(payload) { const statusCode = res.message.statusCode; if (statusCode === 202) { core.info("Successfully uploaded results"); - return; + return true; } const requestID = res.message.headers["x-github-request-id"]; @@ -79,7 +79,7 @@ async function uploadPayload(payload) { // On any other status code that's not 5xx mark the upload as failed if (!statusCode || statusCode < 500 || statusCode >= 600) { core.setFailed('Upload failed (' + requestID + '): (' + statusCode + ') ' + await res.readBody()); - return; + return false; } // On a 5xx status code we may retry the request @@ -97,31 +97,36 @@ async function uploadPayload(payload) { // and not an error that the user has caused or can fix. // We avoid marking the job as failed to avoid breaking CI workflows. core.error('Upload failed (' + requestID + '): (' + statusCode + ') ' + await res.readBody()); - return; + return false; } } + + return false; } // Uploads a single sarif file or a directory of sarif files // depending on what the path happens to refer to. -export async function upload(input: string) { +// Returns true iff the upload occurred and succeeded +export async function upload(input: string): Promise { if (fs.lstatSync(input).isDirectory()) { const sarifFiles = fs.readdirSync(input) .filter(f => f.endsWith(".sarif")) .map(f => path.resolve(input, f)); if (sarifFiles.length === 0) { core.setFailed("No SARIF files found to upload in \"" + input + "\"."); - return; + return false; } - await uploadFiles(sarifFiles); + return await uploadFiles(sarifFiles); } else { - await uploadFiles([input]); + return await uploadFiles([input]); } } // Uploads the given set of sarif files. -async function uploadFiles(sarifFiles: string[]) { +// Returns true iff the upload occurred and succeeded +async function uploadFiles(sarifFiles: string[]): Promise { core.startGroup("Uploading results"); + let succeeded = false; try { // Check if an upload has happened before. If so then abort. // This is intended to catch when the finish and upload-sarif actions @@ -129,7 +134,7 @@ async function uploadFiles(sarifFiles: string[]) { const sentinelFile = await getSentinelFilePath(); if (fs.existsSync(sentinelFile)) { core.info("Aborting as an upload has already happened from this job"); - return; + return false; } const commitOid = util.getRequiredEnvParam('GITHUB_SHA'); @@ -149,7 +154,7 @@ async function uploadFiles(sarifFiles: string[]) { if (Number.isNaN(workflowRunID)) { core.setFailed('GITHUB_RUN_ID must define a non NaN workflow run ID'); - return; + return false; } let matrix: string | undefined = core.getInput('matrix'); @@ -172,7 +177,7 @@ async function uploadFiles(sarifFiles: string[]) { }); // Make the upload - await uploadPayload(payload); + succeeded = await uploadPayload(payload); // Mark that we have made an upload fs.writeFileSync(sentinelFile, ''); @@ -181,4 +186,6 @@ async function uploadFiles(sarifFiles: string[]) { core.setFailed(error.message); } core.endGroup(); + + return succeeded; } diff --git a/src/upload-sarif.ts b/src/upload-sarif.ts index b0aedce5a..418769c27 100644 --- a/src/upload-sarif.ts +++ b/src/upload-sarif.ts @@ -9,17 +9,19 @@ async function run() { } try { - await upload_lib.upload(core.getInput('sarif_file')); + if (await upload_lib.upload(core.getInput('sarif_file'))) { + await util.reportActionSucceeded('upload-sarif'); + } else { + await util.reportActionFailed('upload-sarif', 'upload'); + } } catch (error) { core.setFailed(error.message); await util.reportActionFailed('upload-sarif', error.message, error.stack); return; } - - await util.reportActionSucceeded('upload-sarif'); } run().catch(e => { - core.setFailed("upload-sarif action failed: " + e); + core.setFailed("codeql/upload-sarif action failed: " + e); console.log(e); });