Refactoring: Convert ActionName to enum

This commit is contained in:
Henry Mercer 2024-02-29 11:29:32 +00:00
parent e12a8cc5eb
commit c0917251d4
27 changed files with 123 additions and 111 deletions

View file

@ -7,7 +7,6 @@ import * as safeWhich from "@chrisgavin/safe-which";
import { JSONSchemaForNPMPackageJsonFiles } from "@schemastore/package";
import type { Config } from "./config-utils";
import { EnvVar } from "./environment";
import {
doesDirectoryExist,
getCodeQLDatabasePath,
@ -15,14 +14,6 @@ import {
ConfigurationError,
} from "./util";
export type ActionName =
| "autobuild"
| "finish"
| "init"
| "init-post"
| "resolve-environment"
| "upload-sarif";
// eslint-disable-next-line import/no-commonjs
const pkg = require("../package.json") as JSONSchemaForNPMPackageJsonFiles;
@ -268,21 +259,6 @@ export function getActionVersion(): string {
return pkg.version!;
}
/**
* @returns a boolean indicating whether the analysis is considered to be first party.
*
* This is based on whether the init action has been used, which is only used for first party analysis.
* When a SARIF file has been generated by other means and submitted using the upload action, this is
* considered to be a third party analysis and is treated differently when calculating SLOs. To ensure
* misconfigured workflows are not treated as third party, only the upload-sarif action can return false.
*/
export function isFirstPartyAnalysis(actionName: ActionName): boolean {
if (actionName !== "upload-sarif") {
return true;
}
return process.env[EnvVar.INIT_ACTION_HAS_RUN] === "true";
}
/**
* Returns the name of the event that triggered this workflow.
*

View file

@ -26,6 +26,7 @@ import { getActionsLogger, Logger } from "./logging";
import { parseRepositoryNwo } from "./repository";
import * as statusReport from "./status-report";
import {
ActionName,
createStatusReportBase,
DatabaseCreationTimings,
getActionsStatus,
@ -64,7 +65,7 @@ async function sendStatusReport(
) {
const status = getActionsStatus(error, stats?.analyze_failure_language);
const statusReportBase = await createStatusReportBase(
"finish",
ActionName.Analyze,
status,
startedAt,
config,
@ -191,7 +192,7 @@ async function run() {
try {
await statusReport.sendStatusReport(
await createStatusReportBase(
"finish",
ActionName.Analyze,
"starting",
startedAt,
undefined,

View file

@ -17,6 +17,7 @@ import {
getActionsStatus,
createStatusReportBase,
sendStatusReport,
ActionName,
} from "./status-report";
import {
checkActionVersion,
@ -45,7 +46,7 @@ async function sendCompletedStatusReport(
const status = getActionsStatus(cause, failingLanguage);
const statusReportBase = await createStatusReportBase(
"autobuild",
ActionName.Autobuild,
status,
startedAt,
config,
@ -71,7 +72,7 @@ async function run() {
try {
await sendStatusReport(
await createStatusReportBase(
"autobuild",
ActionName.Autobuild,
"starting",
startedAt,
undefined,

View file

@ -19,6 +19,7 @@ import {
sendStatusReport,
createStatusReportBase,
getActionsStatus,
ActionName,
} from "./status-report";
import {
checkDiskUsage,
@ -76,7 +77,7 @@ async function runWrapper() {
await sendStatusReport(
await createStatusReportBase(
"init-post",
ActionName.InitPost,
getActionsStatus(error),
startedAt,
config,
@ -89,7 +90,7 @@ async function runWrapper() {
return;
}
const statusReportBase = await createStatusReportBase(
"init-post",
ActionName.InitPost,
"success",
startedAt,
config,

View file

@ -33,6 +33,7 @@ import { getActionsLogger, Logger } from "./logging";
import { parseRepositoryNwo } from "./repository";
import { ToolsSource } from "./setup-codeql";
import {
ActionName,
StatusReportBase,
createStatusReportBase,
getActionsStatus,
@ -109,7 +110,7 @@ async function sendCompletedStatusReport(
error?: Error,
) {
const statusReportBase = await createStatusReportBase(
"init",
ActionName.Init,
getActionsStatus(error),
startedAt,
undefined,
@ -227,7 +228,7 @@ async function run() {
try {
await sendStatusReport(
await createStatusReportBase(
"init",
ActionName.Init,
"starting",
startedAt,
undefined,
@ -316,7 +317,7 @@ async function run() {
core.setFailed(error.message);
await sendStatusReport(
await createStatusReportBase(
"init",
ActionName.Init,
error instanceof ConfigurationError ? "user-error" : "aborted",
startedAt,
config,

View file

@ -15,6 +15,7 @@ import {
sendStatusReport,
createStatusReportBase,
getActionsStatus,
ActionName,
} from "./status-report";
import {
checkActionVersion,
@ -24,7 +25,6 @@ import {
wrapError,
} from "./util";
const ACTION_NAME = "resolve-environment";
const ENVIRONMENT_OUTPUT_NAME = "environment";
async function run() {
@ -36,7 +36,7 @@ async function run() {
try {
await sendStatusReport(
await createStatusReportBase(
ACTION_NAME,
ActionName.ResolveEnvironment,
"starting",
startedAt,
undefined,
@ -82,7 +82,7 @@ async function run() {
await sendStatusReport(
await createStatusReportBase(
ACTION_NAME,
ActionName.ResolveEnvironment,
getActionsStatus(error),
startedAt,
config,
@ -99,7 +99,7 @@ async function run() {
await sendStatusReport(
await createStatusReportBase(
ACTION_NAME,
ActionName.ResolveEnvironment,
"success",
startedAt,
config,
@ -113,7 +113,11 @@ async function runWrapper() {
try {
await run();
} catch (error) {
core.setFailed(`${ACTION_NAME} action failed: ${wrapError(error).message}`);
core.setFailed(
`${ActionName.ResolveEnvironment} action failed: ${
wrapError(error).message
}`,
);
}
await checkForTimeout();
}

View file

@ -6,7 +6,7 @@ import { BuildMode } from "./config-utils";
import { EnvVar } from "./environment";
import { Language } from "./languages";
import { getRunnerLogger } from "./logging";
import { createStatusReportBase } from "./status-report";
import { ActionName, createStatusReportBase } from "./status-report";
import {
setupTests,
setupActionsVars,
@ -39,7 +39,7 @@ test("createStatusReportBase", async (t) => {
setupEnvironmentAndStub(tmpDir);
const statusReport = await createStatusReportBase(
"init",
ActionName.Init,
"failure",
new Date("May 19, 2023 05:19:00"),
createTestConfig({
@ -52,7 +52,7 @@ test("createStatusReportBase", async (t) => {
"exception stack trace",
);
t.is(statusReport.action_name, "init");
t.is(statusReport.action_name, ActionName.Init);
t.is(statusReport.action_oid, "unknown");
t.is(typeof statusReport.action_version, "string");
t.is(
@ -87,7 +87,7 @@ test("createStatusReportBase_firstParty", async (t) => {
t.is(
(
await createStatusReportBase(
"upload-sarif",
ActionName.UploadSarif,
"failure",
new Date("May 19, 2023 05:19:00"),
createTestConfig({}),
@ -103,7 +103,7 @@ test("createStatusReportBase_firstParty", async (t) => {
t.is(
(
await createStatusReportBase(
"autobuild",
ActionName.Autobuild,
"failure",
new Date("May 19, 2023 05:19:00"),
createTestConfig({}),
@ -120,7 +120,7 @@ test("createStatusReportBase_firstParty", async (t) => {
t.is(
(
await createStatusReportBase(
"upload-sarif",
ActionName.UploadSarif,
"failure",
new Date("May 19, 2023 05:19:00"),
createTestConfig({}),
@ -136,7 +136,7 @@ test("createStatusReportBase_firstParty", async (t) => {
t.is(
(
await createStatusReportBase(
"init",
ActionName.Init,
"failure",
new Date("May 19, 2023 05:19:00"),
createTestConfig({}),
@ -153,7 +153,7 @@ test("createStatusReportBase_firstParty", async (t) => {
t.is(
(
await createStatusReportBase(
"upload-sarif",
ActionName.UploadSarif,
"failure",
new Date("May 19, 2023 05:19:00"),
createTestConfig({}),
@ -169,7 +169,7 @@ test("createStatusReportBase_firstParty", async (t) => {
t.is(
(
await createStatusReportBase(
"finish",
ActionName.Analyze,
"failure",
new Date("May 19, 2023 05:19:00"),
createTestConfig({}),

View file

@ -10,8 +10,6 @@ import {
getWorkflowRunAttempt,
getActionVersion,
getRequiredInput,
isFirstPartyAnalysis,
ActionName,
} from "./actions-util";
import { getAnalysisKey, getApiClient } from "./api-client";
import { BuildMode, Config } from "./config-utils";
@ -27,6 +25,30 @@ import {
DiskUsage,
} from "./util";
export enum ActionName {
Autobuild = "autobuild",
Analyze = "finish",
Init = "init",
InitPost = "init-post",
ResolveEnvironment = "resolve-environment",
UploadSarif = "upload-sarif",
}
/**
* @returns a boolean indicating whether the analysis is considered to be first party.
*
* This is based on whether the init action has been used, which is only used for first party analysis.
* When a SARIF file has been generated by other means and submitted using the upload action, this is
* considered to be a third party analysis and is treated differently when calculating SLOs. To ensure
* misconfigured workflows are not treated as third party, only the upload-sarif action can return false.
*/
export function isFirstPartyAnalysis(actionName: ActionName): boolean {
if (actionName !== ActionName.UploadSarif) {
return true;
}
return process.env[EnvVar.INIT_ACTION_HAS_RUN] === "true";
}
export type ActionStatus =
| "aborted" // Only used in the init Action, if init failed before initializing the tracer due to something other than a configuration error.
| "failure"

View file

@ -1,7 +1,7 @@
import * as core from "@actions/core";
import * as actionsUtil from "./actions-util";
import { getActionVersion, isFirstPartyAnalysis } from "./actions-util";
import { getActionVersion } from "./actions-util";
import { getGitHubVersion } from "./api-client";
import { Logger, getActionsLogger } from "./logging";
import { parseRepositoryNwo } from "./repository";
@ -10,6 +10,8 @@ import {
sendStatusReport,
StatusReportBase,
getActionsStatus,
ActionName,
isFirstPartyAnalysis,
} from "./status-report";
import * as upload_lib from "./upload-lib";
import {
@ -22,8 +24,6 @@ import {
wrapError,
} from "./util";
const ACTION_NAME = "upload-sarif";
interface UploadSarifStatusReport
extends StatusReportBase,
upload_lib.UploadStatusReport {}
@ -34,7 +34,7 @@ async function sendSuccessStatusReport(
logger: Logger,
) {
const statusReportBase = await createStatusReportBase(
ACTION_NAME,
ActionName.UploadSarif,
"success",
startedAt,
undefined,
@ -58,7 +58,7 @@ async function run() {
await sendStatusReport(
await createStatusReportBase(
ACTION_NAME,
ActionName.UploadSarif,
"starting",
startedAt,
undefined,
@ -89,7 +89,7 @@ async function run() {
await sendSuccessStatusReport(startedAt, uploadResult.statusReport, logger);
} catch (unwrappedError) {
const error =
!isFirstPartyAnalysis(ACTION_NAME) &&
!isFirstPartyAnalysis(ActionName.UploadSarif) &&
unwrappedError instanceof upload_lib.InvalidSarifUploadError
? new ConfigurationError(unwrappedError.message)
: wrapError(unwrappedError);
@ -98,7 +98,7 @@ async function run() {
console.log(error);
await sendStatusReport(
await createStatusReportBase(
ACTION_NAME,
ActionName.UploadSarif,
getActionsStatus(error),
startedAt,
undefined,