Skip to content

Commit

Permalink
Merge branch 'master' into autobuild_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Veytsman authored and GitHub committed May 1, 2020
2 parents 1fe0932 + d46c1c7 commit 984552a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 30 deletions.
5 changes: 4 additions & 1 deletion lib/finalize-db.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 18 additions & 9 deletions lib/upload-lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions lib/upload-sarif.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/finalize-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
35 changes: 23 additions & 12 deletions src/upload-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
core.info('Uploading results');

const githubToken = core.getInput('token');
Expand All @@ -71,15 +71,15 @@ 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
Expand All @@ -97,35 +97,44 @@ 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<boolean> {
if (fs.lstatSync(input).isDirectory()) {
const sarifFiles = fs.readdirSync(input)
.filter(f => f.endsWith(".sarif"))
.map(f => path.resolve(input, f));
await uploadFiles(sarifFiles);
if (sarifFiles.length === 0) {
core.setFailed("No SARIF files found to upload in \"" + input + "\".");
return false;
}
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<boolean> {
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
// are used together, and then the upload-sarif action is invoked twice.
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');
Expand All @@ -134,7 +143,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);

Expand All @@ -145,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');
Expand All @@ -168,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, '');
Expand All @@ -177,4 +186,6 @@ async function uploadFiles(sarifFiles: string[]) {
core.setFailed(error.message);
}
core.endGroup();

return succeeded;
}
10 changes: 6 additions & 4 deletions src/upload-sarif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 984552a

Please sign in to comment.