Skip to content

Commit

Permalink
Add an option to allow waiting until an analysis has been processed b…
Browse files Browse the repository at this point in the history
…efore finishing the Action.
  • Loading branch information
Chris Gavin committed Nov 17, 2021
1 parent 2ecc17d commit 316ad9d
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 8 deletions.
3 changes: 3 additions & 0 deletions analyze/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ inputs:
description: Whether to upload the resulting CodeQL database
required: false
default: "true"
wait-for-processing:
description: If true, the Action will wait for the uploaded SARIF to be processed before completing.
required: false
token:
default: ${{ github.token }}
matrix:
Expand Down
55 changes: 51 additions & 4 deletions lib/upload-lib.js

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

2 changes: 1 addition & 1 deletion lib/upload-lib.js.map

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion lib/util.js

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

2 changes: 1 addition & 1 deletion lib/util.js.map

Large diffs are not rendered by default.

66 changes: 65 additions & 1 deletion src/upload-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ async function uploadPayload(

logger.debug(`response status: ${response.status}`);
logger.info("Successfully uploaded results");

return response.data.id;
}

export interface UploadStatusReport {
Expand Down Expand Up @@ -159,6 +161,7 @@ export async function uploadFromActions(
actionsUtil.getWorkflowRunID(),
actionsUtil.getRequiredInput("checkout_path"),
actionsUtil.getRequiredInput("matrix"),
actionsUtil.getOptionalInput("wait-for-processing") === "true",
gitHubVersion,
apiDetails,
logger
Expand Down Expand Up @@ -190,6 +193,7 @@ export async function uploadFromRunner(
undefined,
sourceRoot,
undefined,
false,
gitHubVersion,
apiDetails,
logger
Expand Down Expand Up @@ -323,6 +327,9 @@ export function buildPayload(
}
}

const STATUS_CHECK_FREQUENCY_MILLISECONDS = 5 * 1000;
const STATUS_CHECK_TIMEOUT_MILLISECONDS = 2 * 60 * 1000;

// Uploads the given set of sarif files.
// Returns true iff the upload occurred and succeeded
async function uploadFiles(
Expand All @@ -336,6 +343,7 @@ async function uploadFiles(
workflowRunID: number | undefined,
sourceRoot: string,
environment: string | undefined,
waitForProcessing: boolean,
gitHubVersion: util.GitHubVersion,
apiDetails: api.GitHubApiDetails,
logger: Logger
Expand Down Expand Up @@ -390,10 +398,66 @@ async function uploadFiles(
logger.debug(`Number of results in upload: ${numResultInSarif}`);

// Make the upload
await uploadPayload(payload, repositoryNwo, apiDetails, logger);
const sarifID = await uploadPayload(
payload,
repositoryNwo,
apiDetails,
logger
);

logger.endGroup();

if (waitForProcessing) {
logger.startGroup("Waiting for processing to finish");
const client = api.getApiClient(apiDetails);

const statusCheckingStarted = Date.now();
// eslint-disable-next-line no-constant-condition
while (true) {
if (
Date.now() >
statusCheckingStarted + STATUS_CHECK_TIMEOUT_MILLISECONDS
) {
// If the analysis hasn't finished processing in the allotted time, we continue anyway rather than failing.
// It's possible the analysis will eventually finish processing, but it's not worth spending more Actions time waiting.
logger.warning(
"Timed out waiting for analysis to finish processing. Continuing."
);
break;
}
// We put the delay at the start of the loop, since it's unlikely that the analysis will have succeeded immediately. We might as well wait preemptively.
await util.delay(STATUS_CHECK_FREQUENCY_MILLISECONDS);
try {
const response = await client.request(
"GET /repos/:owner/:repo/code-scanning/sarifs/:sarif_id",
{
owner: repositoryNwo.owner,
repo: repositoryNwo.repo,
sarif_id: sarifID,
}
);
const status = response.data.processing_status;
logger.info(`Status is ${status}.`);
if (status === "complete") {
break;
}
} catch (e) {
if (util.isHTTPError(e)) {
switch (e.status) {
case 404:
logger.info("Analysis is not found yet...");
break;
default:
throw e;
}
} else {
throw e;
}
}
}
logger.endGroup();
}

return {
raw_upload_size_bytes: rawUploadSizeBytes,
zipped_upload_size_bytes: zippedUploadSizeBytes,
Expand Down
4 changes: 4 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,7 @@ export async function bundleDb(
}
return databaseBundlePath;
}

export async function delay(milliseconds: number) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
3 changes: 3 additions & 0 deletions upload-sarif/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ inputs:
category:
description: String used by Code Scanning for matching the analyses
required: false
wait-for-processing:
description: If true, the Action will wait for the uploaded SARIF to be processed before completing.
required: false
runs:
using: 'node12'
main: '../lib/upload-sarif-action.js'

0 comments on commit 316ad9d

Please sign in to comment.