mirror of
https://github.com/github/codeql-action.git
synced 2026-01-06 22:50:17 +08:00
Add telemetry for uploading failed runs
This commit is contained in:
@@ -295,7 +295,12 @@ function getRefFromEnv(): string {
|
||||
return refEnv;
|
||||
}
|
||||
|
||||
type ActionName = "init" | "autobuild" | "finish" | "upload-sarif";
|
||||
type ActionName =
|
||||
| "init"
|
||||
| "autobuild"
|
||||
| "finish"
|
||||
| "upload-sarif"
|
||||
| "init-post";
|
||||
type ActionStatus =
|
||||
| "starting"
|
||||
| "aborted"
|
||||
|
||||
@@ -16,17 +16,29 @@ import {
|
||||
getWorkflow,
|
||||
} from "./workflow";
|
||||
|
||||
export interface UploadFailedSarifResult {
|
||||
/** Size in bytes of the unzipped SARIF payload uploaded for the failed run. */
|
||||
upload_failed_run_raw_upload_size_bytes?: number;
|
||||
/** Size in bytes of actual SARIF payload uploaded for the failed run. */
|
||||
upload_failed_run_zipped_upload_size_bytes?: number;
|
||||
|
||||
/** Error encountered during uploading the failed run. */
|
||||
upload_failed_run_error?: string;
|
||||
/** Reason why we did not upload a SARIF payload with `executionSuccessful: false`. */
|
||||
upload_failed_run_skipped_because?: string;
|
||||
}
|
||||
|
||||
export async function uploadFailedSarif(
|
||||
config: Config,
|
||||
repositoryNwo: RepositoryNwo,
|
||||
featureEnablement: FeatureEnablement,
|
||||
logger: Logger
|
||||
) {
|
||||
): Promise<UploadFailedSarifResult> {
|
||||
if (!config.codeQLCmd) {
|
||||
logger.warning(
|
||||
"CodeQL command not found. Unable to upload failed SARIF file."
|
||||
);
|
||||
return;
|
||||
return { upload_failed_run_skipped_because: "CodeQL command not found" };
|
||||
}
|
||||
const codeql = await getCodeQL(config.codeQLCmd);
|
||||
if (
|
||||
@@ -36,7 +48,7 @@ export async function uploadFailedSarif(
|
||||
))
|
||||
) {
|
||||
logger.debug("Uploading failed SARIF is disabled.");
|
||||
return;
|
||||
return { upload_failed_run_skipped_because: "Feature disabled" };
|
||||
}
|
||||
const workflow = await getWorkflow();
|
||||
const jobName = getRequiredEnvParam("GITHUB_JOB");
|
||||
@@ -48,7 +60,7 @@ export async function uploadFailedSarif(
|
||||
logger.debug(
|
||||
"Won't upload a failed SARIF file since SARIF upload is disabled."
|
||||
);
|
||||
return;
|
||||
return { upload_failed_run_skipped_because: "SARIF upload is disabled" };
|
||||
}
|
||||
const category = getCategoryInputOrThrow(workflow, jobName, matrix);
|
||||
const checkoutPath = getCheckoutPathInputOrThrow(workflow, jobName, matrix);
|
||||
@@ -69,6 +81,12 @@ export async function uploadFailedSarif(
|
||||
logger,
|
||||
{ isUnsuccessfulExecution: true }
|
||||
);
|
||||
return {
|
||||
upload_failed_run_raw_upload_size_bytes:
|
||||
uploadResult?.statusReport?.raw_upload_size_bytes,
|
||||
upload_failed_run_zipped_upload_size_bytes:
|
||||
uploadResult?.statusReport?.zipped_upload_size_bytes,
|
||||
};
|
||||
}
|
||||
|
||||
export async function run(
|
||||
@@ -91,9 +109,16 @@ export async function run(
|
||||
const expectFailedSarifUpload =
|
||||
process.env["CODEQL_ACTION_EXPECT_UPLOAD_FAILED_SARIF"] === "true";
|
||||
|
||||
let uploadFailedSarifResult: UploadFailedSarifResult;
|
||||
|
||||
if (process.env[CODEQL_ACTION_ANALYZE_DID_UPLOAD_SARIF] !== "true") {
|
||||
try {
|
||||
await uploadFailedSarif(config, repositoryNwo, featureEnablement, logger);
|
||||
uploadFailedSarifResult = await uploadFailedSarif(
|
||||
config,
|
||||
repositoryNwo,
|
||||
featureEnablement,
|
||||
logger
|
||||
);
|
||||
} catch (e) {
|
||||
if (expectFailedSarifUpload) {
|
||||
throw new Error(
|
||||
@@ -104,11 +129,18 @@ export async function run(
|
||||
logger.info(
|
||||
`Failed to upload a SARIF file for the failed run. Error: ${e}`
|
||||
);
|
||||
uploadFailedSarifResult = {
|
||||
upload_failed_run_error: e instanceof Error ? e.message : String(e),
|
||||
};
|
||||
}
|
||||
} else if (expectFailedSarifUpload) {
|
||||
throw new Error(
|
||||
"Expected to upload a SARIF file for the failed run, but didn't."
|
||||
);
|
||||
} else {
|
||||
uploadFailedSarifResult = {
|
||||
upload_failed_run_skipped_because: "SARIF file already uploaded",
|
||||
};
|
||||
}
|
||||
|
||||
// Upload appropriate Actions artifacts for debugging
|
||||
@@ -121,4 +153,6 @@ export async function run(
|
||||
|
||||
await printDebugLogs(config);
|
||||
}
|
||||
|
||||
return uploadFailedSarifResult;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,14 @@
|
||||
|
||||
import * as core from "@actions/core";
|
||||
|
||||
import * as actionsUtil from "./actions-util";
|
||||
import {
|
||||
createStatusReportBase,
|
||||
getActionsStatus,
|
||||
getTemporaryDirectory,
|
||||
printDebugLogs,
|
||||
sendStatusReport,
|
||||
StatusReportBase,
|
||||
} from "./actions-util";
|
||||
import { getGitHubVersion } from "./api-client";
|
||||
import * as debugArtifacts from "./debug-artifacts";
|
||||
import { Features } from "./feature-flags";
|
||||
@@ -15,7 +22,15 @@ import { getActionsLogger } from "./logging";
|
||||
import { parseRepositoryNwo } from "./repository";
|
||||
import { checkGitHubVersionInRange, getRequiredEnvParam } from "./util";
|
||||
|
||||
interface InitPostStatusReport
|
||||
extends StatusReportBase,
|
||||
initActionPostHelper.UploadFailedSarifResult {}
|
||||
|
||||
async function runWrapper() {
|
||||
const startedAt = new Date();
|
||||
let uploadFailedSarifResult:
|
||||
| initActionPostHelper.UploadFailedSarifResult
|
||||
| undefined;
|
||||
try {
|
||||
const logger = getActionsLogger();
|
||||
const gitHubVersion = await getGitHubVersion();
|
||||
@@ -27,22 +42,45 @@ async function runWrapper() {
|
||||
const features = new Features(
|
||||
gitHubVersion,
|
||||
repositoryNwo,
|
||||
actionsUtil.getTemporaryDirectory(),
|
||||
getTemporaryDirectory(),
|
||||
logger
|
||||
);
|
||||
|
||||
await initActionPostHelper.run(
|
||||
uploadFailedSarifResult = await initActionPostHelper.run(
|
||||
debugArtifacts.uploadDatabaseBundleDebugArtifact,
|
||||
debugArtifacts.uploadLogsDebugArtifact,
|
||||
actionsUtil.printDebugLogs,
|
||||
printDebugLogs,
|
||||
repositoryNwo,
|
||||
features,
|
||||
logger
|
||||
);
|
||||
} catch (error) {
|
||||
core.setFailed(`init post-action step failed: ${error}`);
|
||||
console.log(error);
|
||||
} catch (e) {
|
||||
core.setFailed(e instanceof Error ? e.message : String(e));
|
||||
|
||||
console.log(e);
|
||||
await sendStatusReport(
|
||||
await createStatusReportBase(
|
||||
"init-post",
|
||||
getActionsStatus(e),
|
||||
startedAt,
|
||||
String(e),
|
||||
e instanceof Error ? e.stack : undefined
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
const statusReportBase = await createStatusReportBase(
|
||||
"init-post",
|
||||
"success",
|
||||
startedAt
|
||||
);
|
||||
const statusReport: InitPostStatusReport = {
|
||||
...statusReportBase,
|
||||
...uploadFailedSarifResult,
|
||||
};
|
||||
console.log("status report");
|
||||
console.log(statusReport);
|
||||
await sendStatusReport(statusReport);
|
||||
}
|
||||
|
||||
void runWrapper();
|
||||
|
||||
Reference in New Issue
Block a user