Skip to content

Commit f4a15a1

Browse files
committed
Try to load failed config if normal config is unavailable
Adapt relevant functions by narrowing their types or skipping steps that would otherwise have been skipped as well
1 parent 4fdbb71 commit f4a15a1

8 files changed

Lines changed: 92 additions & 29 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 22 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/actions-util.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as toolrunner from "@actions/exec/lib/toolrunner";
66
import * as github from "@actions/github";
77
import * as io from "@actions/io";
88

9-
import type { Config } from "./config-utils";
9+
import { hasConfigKey, type Config } from "./config/action-config";
1010
import { Env, EnvVar, ActionsEnvVars, ReadOnlyEnv } from "./environment";
1111
import { Logger } from "./logging";
1212
import {
@@ -131,7 +131,14 @@ export function getWorkflowEvent(env: Env = getEnv()): any {
131131
}
132132
}
133133

134-
export async function printDebugLogs(config: Config) {
134+
export async function printDebugLogs(config: Partial<Config>) {
135+
if (
136+
!hasConfigKey(config, "languages") ||
137+
!hasConfigKey(config, "dbLocation")
138+
) {
139+
return;
140+
}
141+
135142
for (const language of config.languages) {
136143
const databaseDirectory = getCodeQLDatabasePath(config, language);
137144
const logsDirectory = path.join(databaseDirectory, "log");

‎src/analyze.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getTemporaryDirectory } from "./actions-util";
99
import * as analyses from "./analyses";
1010
import { setupCppAutobuild } from "./autobuild";
1111
import { type CodeQL } from "./codeql";
12+
import type { ActionConfig } from "./config/action-config";
1213
import { defaultSuites } from "./config/db-config";
1314
import * as configUtils from "./config-utils";
1415
import {
@@ -171,7 +172,7 @@ async function shouldExtractLanguage(
171172
}
172173

173174
export function dbIsFinalized(
174-
config: configUtils.Config,
175+
config: ActionConfig<"dbLocation">,
175176
language: Language,
176177
logger: Logger,
177178
) {

‎src/config/action-config.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ export type ActionConfig<
143143
T extends ActionConfigProperties = ActionConfigProperties,
144144
> = Partial<Config> & Pick<Config, T>;
145145

146+
/** Helper function to check whether `config` has `key` to narrow the type of `config`. */
147+
export function hasConfigKey<Key extends ActionConfigProperties>(
148+
config: Partial<Config>,
149+
key: Key,
150+
): config is ActionConfig<Key> {
151+
return config[key] !== undefined;
152+
}
153+
146154
/** A minimal configuration that we can make available even when `init` aborts. */
147155
export type AbortedInitConfig = ActionConfig<
148156
| "version"

‎src/debug-artifacts.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getOptionalInput, getTemporaryDirectory } from "./actions-util";
1010
import { dbIsFinalized } from "./analyze";
1111
import { scanArtifactsForTokens } from "./artifact-scanner";
1212
import { type CodeQL } from "./codeql";
13-
import { Config } from "./config-utils";
13+
import type { ActionConfig } from "./config/action-config";
1414
import { EnvVar, exportEnvVar } from "./environment";
1515
import * as json from "./json";
1616
import { Language } from "./languages";
@@ -89,7 +89,7 @@ export async function uploadCombinedSarifArtifacts(
8989
* @return The path to that debug artifact, or undefined if an error occurs.
9090
*/
9191
function tryPrepareSarifDebugArtifact(
92-
config: Config,
92+
config: ActionConfig<"dbLocation">,
9393
language: Language,
9494
logger: Logger,
9595
): string | undefined {
@@ -131,7 +131,7 @@ function tryPrepareSarifDebugArtifact(
131131
*/
132132
async function tryBundleDatabase(
133133
codeql: CodeQL,
134-
config: Config,
134+
config: ActionConfig<"dbLocation">,
135135
language: Language,
136136
logger: Logger,
137137
): Promise<string | undefined> {
@@ -157,14 +157,19 @@ async function tryBundleDatabase(
157157
}
158158
}
159159

160+
/** The minimal configuration needed for `tryUploadAllAvailableDebugArtifacts`. */
161+
export type UploadDebugArtifactsConfig = ActionConfig<
162+
"languages" | "gitHubVersion" | "dbLocation" | "debugArtifactName"
163+
>;
164+
160165
/**
161166
* Attempt to upload all available debug artifacts.
162167
*
163168
* Logs and suppresses any errors that occur.
164169
*/
165170
export async function tryUploadAllAvailableDebugArtifacts(
166171
codeql: CodeQL,
167-
config: Config,
172+
config: UploadDebugArtifactsConfig,
168173
logger: Logger,
169174
) {
170175
const filesToUpload: string[] = [];
@@ -353,7 +358,7 @@ export async function getArtifactUploaderClient(
353358
* all files in the database folder and return the path.
354359
*/
355360
async function createPartialDatabaseBundle(
356-
config: Config,
361+
config: ActionConfig<"dbLocation">,
357362
language: Language,
358363
): Promise<string> {
359364
const databasePath = getCodeQLDatabasePath(config, language);
@@ -394,7 +399,7 @@ async function createPartialDatabaseBundle(
394399
*/
395400
async function createDatabaseBundleCli(
396401
codeql: CodeQL,
397-
config: Config,
402+
config: ActionConfig<"dbLocation">,
398403
language: Language,
399404
): Promise<string> {
400405
const databaseBundlePath = await bundleDb(

‎src/init-action-post-helper.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import * as actionsUtil from "./actions-util";
77
import { CodeScanning, RiskAssessment } from "./analyses";
88
import { getApiClient, getGitHubVersion } from "./api-client";
99
import { CodeQL, getCodeQL } from "./codeql";
10-
import type { ActionConfig } from "./config/action-config";
10+
import {
11+
hasConfigKey,
12+
type AbortedInitConfig,
13+
type ActionConfig,
14+
} from "./config/action-config";
1115
import {
1216
Config,
1317
isCodeScanningEnabled,
@@ -17,6 +21,7 @@ import {
1721
getArtifactSuffix,
1822
getArtifactUploaderClient,
1923
sanitizeArtifactName,
24+
type UploadDebugArtifactsConfig,
2025
} from "./debug-artifacts";
2126
import * as dependencyCaching from "./dependency-caching";
2227
import { EnvVar, ReadOnlyEnv } from "./environment";
@@ -330,12 +335,12 @@ export async function tryUploadSarifIfRunFailed(
330335
export async function uploadFailureInfo(
331336
uploadAllAvailableDebugArtifacts: (
332337
codeql: CodeQL,
333-
config: Config,
338+
config: UploadDebugArtifactsConfig,
334339
logger: Logger,
335340
) => Promise<void>,
336-
printDebugLogs: (config: Config) => Promise<void>,
341+
printDebugLogs: (config: Partial<Config>) => Promise<void>,
337342
codeql: CodeQL,
338-
config: Config,
343+
config: Config | AbortedInitConfig,
339344
repositoryNwo: RepositoryNwo,
340345
features: FeatureEnablement,
341346
jobStatus: string | undefined,
@@ -382,15 +387,15 @@ export async function uploadFailureInfo(
382387
}
383388

384389
// Upload appropriate Actions artifacts for debugging
385-
if (config.debugMode) {
390+
if (config.debugMode && hasConfigKey(config, "dbLocation")) {
386391
logger.info(
387392
"Debug mode is on. Uploading available database bundles and logs as Actions debugging artifacts...",
388393
);
389394
await uploadAllAvailableDebugArtifacts(codeql, config, logger);
390395
await printDebugLogs(config);
391396
}
392397

393-
if (actionsUtil.isSelfHostedRunner()) {
398+
if (actionsUtil.isSelfHostedRunner() && config.dbLocation) {
394399
try {
395400
fs.rmSync(config.dbLocation, {
396401
recursive: true,
@@ -453,7 +458,7 @@ function isConclusiveJobStatus(jobStatus: string | undefined): boolean {
453458
*/
454459
async function recordOverlayStatus(
455460
codeql: CodeQL,
456-
config: Config,
461+
config: Partial<Config>,
457462
features: FeatureEnablement,
458463
jobStatus: string | undefined,
459464
env: ReadOnlyEnv,
@@ -462,7 +467,8 @@ async function recordOverlayStatus(
462467
if (
463468
config.overlayDatabaseMode !== OverlayDatabaseMode.OverlayBase ||
464469
env.getOptional(EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY) === "true" ||
465-
!(await features.getValue(Feature.OverlayAnalysisStatusSave))
470+
!(await features.getValue(Feature.OverlayAnalysisStatusSave)) ||
471+
config.languages === undefined
466472
) {
467473
return;
468474
}

‎src/init-action-post.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ import {
1616
import { getGitHubVersion, wrapApiConfigurationError } from "./api-client";
1717
import { CachingKind } from "./caching-utils";
1818
import { getCodeQL } from "./codeql";
19-
import { type Config, getConfig } from "./config-utils";
19+
import {
20+
hasConfigKey,
21+
type AbortedInitConfig,
22+
type Config,
23+
} from "./config/action-config";
24+
import { getAbortedInitConfig, getConfig } from "./config-utils";
2025
import * as debugArtifacts from "./debug-artifacts";
2126
import {
2227
DependencyCachingUsageReport,
@@ -49,7 +54,7 @@ async function run(action: ActionState<["Base", "Logger"]>) {
4954
// possible, and only use safe functions outside.
5055
const logger = action.logger;
5156

52-
let config: Config | undefined;
57+
let config: Config | AbortedInitConfig | undefined;
5358
let uploadFailedSarifResult:
5459
| initActionPostHelper.UploadFailedSarifResult
5560
| undefined;
@@ -67,6 +72,13 @@ async function run(action: ActionState<["Base", "Logger"]>) {
6772
const tmpDir = getTemporaryDirectory();
6873
config = await getConfig(tmpDir, logger);
6974

75+
// If we weren't able to read the normal configuration file, the `init` step failed before it could write it.
76+
// Try to load the "failed" configuration file instead which may have been written by the `init` step
77+
// with a minimal configuration for this post-action.
78+
if (config === undefined) {
79+
config = getAbortedInitConfig(action, tmpDir);
80+
}
81+
7082
const gitHubVersion = config?.gitHubVersion ?? (await getGitHubVersion());
7183
checkGitHubVersionInRange(gitHubVersion, logger);
7284

@@ -78,7 +90,7 @@ async function run(action: ActionState<["Base", "Logger"]>) {
7890
logger,
7991
);
8092

81-
if (config === undefined) {
93+
if (config?.codeQLCmd === undefined) {
8294
logger.warning(
8395
"Debugging artifacts are unavailable since the 'init' Action failed before it could produce any.",
8496
);
@@ -103,6 +115,7 @@ async function run(action: ActionState<["Base", "Logger"]>) {
103115
// and where caching may not be enabled.
104116
if (
105117
(await gitUtils.isAnalyzingDefaultBranch()) &&
118+
hasConfigKey(config, "dependencyCachingEnabled") &&
106119
config.dependencyCachingEnabled !== CachingKind.None
107120
) {
108121
dependencyCachingUsage = await getDependencyCacheUsage(logger);
@@ -158,7 +171,7 @@ async function run(action: ActionState<["Base", "Logger"]>) {
158171
* Otherwise, we determine the job status based on whether the analyze step
159172
* completed successfully and whether we have a valid CodeQL config.
160173
*/
161-
function getFinalJobStatus(config: Config | undefined): JobStatus {
174+
function getFinalJobStatus(config: Partial<Config> | undefined): JobStatus {
162175
const existingJobStatus = getJobStatusFromEnvironment();
163176
if (existingJobStatus !== undefined) {
164177
return existingJobStatus;

‎src/util.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as semver from "semver";
1212

1313
import * as apiCompatibility from "./api-compatibility.json";
1414
import type { CodeQL } from "./codeql";
15+
import type { ActionConfig } from "./config/action-config";
1516
import type { Pack } from "./config/db-config";
1617
import type { Config } from "./config-utils";
1718
import {
@@ -445,7 +446,10 @@ export function getThreadsFlag(
445446
/**
446447
* Get the path where the CodeQL database for the given language lives.
447448
*/
448-
export function getCodeQLDatabasePath(config: Config, language: Language) {
449+
export function getCodeQLDatabasePath(
450+
config: ActionConfig<"dbLocation">,
451+
language: Language,
452+
) {
449453
return path.resolve(config.dbLocation, language);
450454
}
451455

@@ -639,7 +643,9 @@ export async function codeQlVersionAtLeast(
639643
return semver.gte((await codeql.getVersion()).version, requiredVersion);
640644
}
641645

642-
export function getBaseDatabaseOidsFilePath(config: Config): string {
646+
export function getBaseDatabaseOidsFilePath(
647+
config: ActionConfig<"dbLocation">,
648+
): string {
643649
return path.join(config.dbLocation, BASE_DATABASE_OIDS_FILE_NAME);
644650
}
645651

@@ -651,7 +657,7 @@ export function getBaseDatabaseOidsFilePath(config: Config): string {
651657
* given `includeDiagnostics` value.
652658
*/
653659
export async function bundleDb(
654-
config: Config,
660+
config: ActionConfig<"dbLocation">,
655661
language: Language,
656662
codeql: CodeQL,
657663
dbName: string,

0 commit comments

Comments
 (0)