Skip to content

Commit 0a9e57a

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 653a997 commit 0a9e57a

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";
@@ -95,7 +95,7 @@ export async function uploadCombinedSarifArtifacts(
9595
* @return The path to that debug artifact, or undefined if an error occurs.
9696
*/
9797
function tryPrepareSarifDebugArtifact(
98-
config: Config,
98+
config: ActionConfig<"dbLocation">,
9999
language: Language,
100100
logger: Logger,
101101
): string | undefined {
@@ -137,7 +137,7 @@ function tryPrepareSarifDebugArtifact(
137137
*/
138138
async function tryBundleDatabase(
139139
codeql: CodeQL,
140-
config: Config,
140+
config: ActionConfig<"dbLocation">,
141141
language: Language,
142142
logger: Logger,
143143
): Promise<string | undefined> {
@@ -163,14 +163,19 @@ async function tryBundleDatabase(
163163
}
164164
}
165165

166+
/** The minimal configuration needed for `tryUploadAllAvailableDebugArtifacts`. */
167+
export type UploadDebugArtifactsConfig = ActionConfig<
168+
"languages" | "gitHubVersion" | "dbLocation" | "debugArtifactName"
169+
>;
170+
166171
/**
167172
* Attempt to upload all available debug artifacts.
168173
*
169174
* Logs and suppresses any errors that occur.
170175
*/
171176
export async function tryUploadAllAvailableDebugArtifacts(
172177
codeql: CodeQL,
173-
config: Config,
178+
config: UploadDebugArtifactsConfig,
174179
logger: Logger,
175180
codeQlVersion: string | undefined,
176181
) {
@@ -381,7 +386,7 @@ export async function getArtifactUploaderClient(
381386
* all files in the database folder and return the path.
382387
*/
383388
async function createPartialDatabaseBundle(
384-
config: Config,
389+
config: ActionConfig<"dbLocation">,
385390
language: Language,
386391
): Promise<string> {
387392
const databasePath = getCodeQLDatabasePath(config, language);
@@ -422,7 +427,7 @@ async function createPartialDatabaseBundle(
422427
*/
423428
async function createDatabaseBundleCli(
424429
codeql: CodeQL,
425-
config: Config,
430+
config: ActionConfig<"dbLocation">,
426431
language: Language,
427432
): Promise<string> {
428433
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 { getTempDiagnosticPath } from "./diagnostics";
@@ -348,13 +353,13 @@ export async function tryUploadSarifIfRunFailed(
348353
export async function uploadFailureInfo(
349354
uploadAllAvailableDebugArtifacts: (
350355
codeql: CodeQL,
351-
config: Config,
356+
config: UploadDebugArtifactsConfig,
352357
logger: Logger,
353358
codeQlVersion: string,
354359
) => Promise<void>,
355-
printDebugLogs: (config: Config) => Promise<void>,
360+
printDebugLogs: (config: Partial<Config>) => Promise<void>,
356361
codeql: CodeQL,
357-
config: Config,
362+
config: Config | AbortedInitConfig,
358363
repositoryNwo: RepositoryNwo,
359364
features: FeatureEnablement,
360365
jobStatus: string | undefined,
@@ -401,7 +406,7 @@ export async function uploadFailureInfo(
401406
}
402407

403408
// Upload appropriate Actions artifacts for debugging
404-
if (config.debugMode) {
409+
if (config.debugMode && hasConfigKey(config, "dbLocation")) {
405410
logger.info(
406411
"Debug mode is on. Uploading available database bundles and logs as Actions debugging artifacts...",
407412
);
@@ -415,7 +420,7 @@ export async function uploadFailureInfo(
415420
await printDebugLogs(config);
416421
}
417422

418-
if (actionsUtil.isSelfHostedRunner()) {
423+
if (actionsUtil.isSelfHostedRunner() && config.dbLocation) {
419424
try {
420425
fs.rmSync(config.dbLocation, {
421426
recursive: true,
@@ -478,7 +483,7 @@ function isConclusiveJobStatus(jobStatus: string | undefined): boolean {
478483
*/
479484
async function recordOverlayStatus(
480485
codeql: CodeQL,
481-
config: Config,
486+
config: Partial<Config>,
482487
features: FeatureEnablement,
483488
jobStatus: string | undefined,
484489
env: ReadOnlyEnv,
@@ -487,7 +492,8 @@ async function recordOverlayStatus(
487492
if (
488493
config.overlayDatabaseMode !== OverlayDatabaseMode.OverlayBase ||
489494
env.getOptional(EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY) === "true" ||
490-
!(await features.getValue(Feature.OverlayAnalysisStatusSave))
495+
!(await features.getValue(Feature.OverlayAnalysisStatusSave)) ||
496+
config.languages === undefined
491497
) {
492498
return;
493499
}

‎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)