From 26e955cfa321f8ee3150ca31cf77d693930ed1bb Mon Sep 17 00:00:00 2001 From: Joshua Hale Date: Thu, 30 Apr 2020 16:28:53 +0100 Subject: [PATCH 1/4] report status as failure if upload fails --- lib/finalize-db.js | 5 ++++- lib/upload-lib.js | 13 +++++++++---- lib/upload-sarif.js | 10 +++++++--- src/finalize-db.ts | 5 ++++- src/upload-lib.ts | 18 ++++++++++++------ src/upload-sarif.ts | 10 ++++++---- 6 files changed, 42 insertions(+), 19 deletions(-) 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 f28f085a4..74e8a300a 100644 --- a/lib/upload-lib.js +++ b/lib/upload-lib.js @@ -56,21 +56,24 @@ function combineSarifFiles(sarifFiles) { exports.combineSarifFiles = combineSarifFiles; // 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) .filter(f => f.endsWith(".sarif")) .map(f => path.resolve(input, f)); - 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 @@ -78,7 +81,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'); @@ -94,7 +97,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 === "") { @@ -131,6 +134,7 @@ async function uploadFiles(sarifFiles) { } else { core.info("Successfully uploaded results"); + succeeded = true; } // Mark that we have made an upload fs.writeFileSync(sentinelFile, ''); @@ -139,4 +143,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 8c6a31e4e..c7965555d 100644 --- a/src/upload-lib.ts +++ b/src/upload-lib.ts @@ -49,20 +49,23 @@ export function combineSarifFiles(sarifFiles: string[]): string { // 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)); - 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 @@ -70,7 +73,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'); @@ -90,7 +93,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'); @@ -130,6 +133,7 @@ async function uploadFiles(sarifFiles: string[]) { core.setFailed('Upload failed (' + requestID + '): ' + await res.readBody()); } else { core.info("Successfully uploaded results"); + succeeded = true; } // Mark that we have made an upload @@ -139,4 +143,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); }); From 5d2700f9cbcdfd67a172405ed36c95850ccf35bf Mon Sep 17 00:00:00 2001 From: Chris Gavin Date: Fri, 1 May 2020 11:07:58 +0100 Subject: [PATCH 2/4] Increase the log level of the message showing what SARIF files were uploaded. --- lib/upload-lib.js | 2 +- src/upload-lib.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/upload-lib.js b/lib/upload-lib.js index f28f085a4..2136fee71 100644 --- a/lib/upload-lib.js +++ b/lib/upload-lib.js @@ -85,7 +85,7 @@ async function uploadFiles(sarifFiles) { const ref = util.getRequiredEnvParam('GITHUB_REF'); // it's in the form "refs/heads/master" const analysisName = util.getRequiredEnvParam('GITHUB_WORKFLOW'); const startedAt = process.env[sharedEnv.CODEQL_ACTION_STARTED_AT]; - core.debug("Uploading sarif files: " + JSON.stringify(sarifFiles)); + core.info("Uploading sarif files: " + JSON.stringify(sarifFiles)); let sarifPayload = combineSarifFiles(sarifFiles); sarifPayload = fingerprints.addFingerprints(sarifPayload); const zipped_sarif = zlib_1.default.gzipSync(sarifPayload).toString('base64'); diff --git a/src/upload-lib.ts b/src/upload-lib.ts index 8c6a31e4e..105e440b1 100644 --- a/src/upload-lib.ts +++ b/src/upload-lib.ts @@ -79,7 +79,7 @@ async function uploadFiles(sarifFiles: string[]) { const analysisName = util.getRequiredEnvParam('GITHUB_WORKFLOW'); const startedAt = process.env[sharedEnv.CODEQL_ACTION_STARTED_AT]; - core.debug("Uploading sarif files: " + JSON.stringify(sarifFiles)); + core.info("Uploading sarif files: " + JSON.stringify(sarifFiles)); let sarifPayload = combineSarifFiles(sarifFiles); sarifPayload = fingerprints.addFingerprints(sarifPayload); From b6a0306228047c89bc6638afbf4606fca113562b Mon Sep 17 00:00:00 2001 From: Chris Gavin Date: Fri, 1 May 2020 11:12:15 +0100 Subject: [PATCH 3/4] Fail the upload action if uploading a folder with no SARIF files in. --- lib/upload-lib.js | 3 +++ src/upload-lib.ts | 3 +++ 2 files changed, 6 insertions(+) diff --git a/lib/upload-lib.js b/lib/upload-lib.js index 2136fee71..568d2934f 100644 --- a/lib/upload-lib.js +++ b/lib/upload-lib.js @@ -61,6 +61,9 @@ async function upload(input) { 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 + "\"."); + } await uploadFiles(sarifFiles); } else { diff --git a/src/upload-lib.ts b/src/upload-lib.ts index 105e440b1..a4959ae36 100644 --- a/src/upload-lib.ts +++ b/src/upload-lib.ts @@ -54,6 +54,9 @@ export async function upload(input: string) { 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 + "\"."); + } await uploadFiles(sarifFiles); } else { await uploadFiles([input]); From 4e9886ad2bf3b89c655a3c6c66a849ba06ae5b1e Mon Sep 17 00:00:00 2001 From: Chris Gavin Date: Fri, 1 May 2020 15:27:32 +0100 Subject: [PATCH 4/4] Stop the upload action early if no files will be uploaded. --- lib/upload-lib.js | 1 + src/upload-lib.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/upload-lib.js b/lib/upload-lib.js index 568d2934f..a9abfb900 100644 --- a/lib/upload-lib.js +++ b/lib/upload-lib.js @@ -63,6 +63,7 @@ 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; } await uploadFiles(sarifFiles); } diff --git a/src/upload-lib.ts b/src/upload-lib.ts index a4959ae36..443a95e86 100644 --- a/src/upload-lib.ts +++ b/src/upload-lib.ts @@ -56,6 +56,7 @@ export async function upload(input: string) { .map(f => path.resolve(input, f)); if (sarifFiles.length === 0) { core.setFailed("No SARIF files found to upload in \"" + input + "\"."); + return; } await uploadFiles(sarifFiles); } else {