Skip to content

Commit a60169d

Browse files
committed
Store partial configuration when init is aborted
1 parent d4e0565 commit a60169d

4 files changed

Lines changed: 171 additions & 22 deletions

File tree

‎lib/entry-points.js‎

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

‎src/config-utils.ts‎

Lines changed: 102 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import {
2121
import * as api from "./api-client";
2222
import { getCachingKind } from "./caching-utils";
2323
import { type CodeQL } from "./codeql";
24-
import { type ActionConfig, type Config } from "./config/action-config";
24+
import {
25+
type AbortedInitConfig,
26+
type ActionConfig,
27+
type Config,
28+
} from "./config/action-config";
2529
import {
2630
calculateAugmentation,
2731
ExcludeQueryFilter,
@@ -71,6 +75,7 @@ import {
7175
import { OverlayDatabaseMode } from "./overlay/overlay-database-mode";
7276
import { shouldSkipOverlayAnalysis } from "./overlay/status";
7377
import { RepositoryNwo } from "./repository";
78+
import { ActionName } from "./status-report";
7479
import { ToolsFeature } from "./tools-features";
7580
import { downloadTrapCaches } from "./trap-caching";
7681
import {
@@ -1379,31 +1384,84 @@ export function getPathToParsedConfigFile(tempDir: string): string {
13791384
return path.join(tempDir, "config");
13801385
}
13811386

1387+
function getPathToFailedConfigFile(tempDir: string): string {
1388+
return path.join(tempDir, "failed-config");
1389+
}
1390+
13821391
/**
1383-
* Store the given config to the path returned from getPathToParsedConfigFile.
1392+
* Writes {@link config} to {@link configFilePath}.
1393+
*
1394+
* This function is not intended to be called, other than by
1395+
* {@link saveConfig} and {@link saveFailedConfig} below.
13841396
*/
1385-
export async function saveConfig(config: Config, logger: Logger) {
1397+
function saveConfigFile(
1398+
logger: Logger,
1399+
configFilePath: string,
1400+
config: Partial<Config>,
1401+
) {
13861402
const configString = JSON.stringify(config);
1387-
const configFile = getPathToParsedConfigFile(config.tempDir);
1388-
fs.mkdirSync(path.dirname(configFile), { recursive: true });
1389-
fs.writeFileSync(configFile, configString, "utf8");
1403+
fs.mkdirSync(path.dirname(configFilePath), { recursive: true });
1404+
fs.writeFileSync(configFilePath, configString, "utf8");
13901405
logger.debug("Saved config:");
13911406
logger.debug(configString);
13921407
}
13931408

13941409
/**
1395-
* Get the config that has been saved to the given temp dir.
1396-
* If the config could not be found then returns undefined.
1410+
* Store the given config to the path returned from getPathToParsedConfigFile.
13971411
*/
1398-
export async function getConfig(
1399-
tempDir: string,
1412+
export async function saveConfig(config: Config, logger: Logger) {
1413+
const configFile = getPathToParsedConfigFile(config.tempDir);
1414+
saveConfigFile(logger, configFile, config);
1415+
}
1416+
1417+
/**
1418+
* Store parts of {@link config} as a failed configuration file.
1419+
*
1420+
* The file will only contain parts of {@link config} that are relevant to
1421+
* the `init-post` step in the workflow.
1422+
*/
1423+
export function saveAbortedInitConfig(
1424+
action: ActionState<["Base", "Logger"]>,
1425+
config: Config,
1426+
) {
1427+
// Should only be called by init.
1428+
if (action.name !== ActionName.Init) {
1429+
return;
1430+
}
1431+
1432+
// Create a copy of the input `Config` that only contains the keys we want.
1433+
const failedConfig: AbortedInitConfig = {
1434+
version: config.version,
1435+
tempDir: config.tempDir,
1436+
codeQLCmd: config.codeQLCmd,
1437+
debugMode: config.debugMode,
1438+
gitHubVersion: config.gitHubVersion,
1439+
languages: config.languages,
1440+
analysisKinds: config.analysisKinds,
1441+
};
1442+
1443+
action.logger.debug(`Saving config for failed init step.`);
1444+
saveConfigFile(
1445+
action.logger,
1446+
getPathToFailedConfigFile(config.tempDir),
1447+
failedConfig,
1448+
);
1449+
}
1450+
1451+
/**
1452+
* Attempts to read a {@link Config} from {@link configFilePath}.
1453+
*
1454+
* This function is not intended to be called, other than by
1455+
* {@link getConfig} and {@link getAbortedInitConfig} below.
1456+
*/
1457+
function readConfigFile(
14001458
logger: Logger,
1401-
): Promise<Config | undefined> {
1402-
const configFile = getPathToParsedConfigFile(tempDir);
1403-
if (!fs.existsSync(configFile)) {
1459+
configFilePath: string,
1460+
): Config | undefined {
1461+
if (!fs.existsSync(configFilePath)) {
14041462
return undefined;
14051463
}
1406-
const configString = fs.readFileSync(configFile, "utf8");
1464+
const configString = fs.readFileSync(configFilePath, "utf8");
14071465
logger.debug("Loaded config:");
14081466
logger.debug(configString);
14091467

@@ -1428,6 +1486,36 @@ export async function getConfig(
14281486
return config as Config;
14291487
}
14301488

1489+
/**
1490+
* Get the config that has been saved to the given temp dir.
1491+
* If the config could not be found then returns undefined.
1492+
*/
1493+
export async function getConfig(
1494+
tempDir: string,
1495+
logger: Logger,
1496+
): Promise<Config | undefined> {
1497+
const configFile = getPathToParsedConfigFile(tempDir);
1498+
return readConfigFile(logger, configFile);
1499+
}
1500+
1501+
/**
1502+
* Loads a {@link AbortedInitConfig} from a failed `init` step if it exists.
1503+
*
1504+
* The result will only contain parts of a {@link Config} that are relevant to
1505+
* the `init-post` step in the workflow.
1506+
*/
1507+
export function getAbortedInitConfig(
1508+
action: ActionState<["Base", "Logger"]>,
1509+
tempDir: string,
1510+
): AbortedInitConfig | undefined {
1511+
// Only intended to be called by the `init-post` action.
1512+
if (action.name !== ActionName.InitPost) {
1513+
return undefined;
1514+
}
1515+
1516+
return readConfigFile(action.logger, getPathToFailedConfigFile(tempDir));
1517+
}
1518+
14311519
/**
14321520
* Generate a `qlconfig.yml` file from the `registries` input.
14331521
* This file is used by the CodeQL CLI to list the registries to use for each

‎src/config/action-config.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,14 @@ export type ActionConfigProperties = keyof Config;
142142
export type ActionConfig<
143143
T extends ActionConfigProperties = ActionConfigProperties,
144144
> = Partial<Config> & Pick<Config, T>;
145+
146+
/** A minimal configuration that we can make available even when `init` aborts. */
147+
export type AbortedInitConfig = ActionConfig<
148+
| "version"
149+
| "tempDir"
150+
| "gitHubVersion"
151+
| "debugMode"
152+
| "languages"
153+
| "analysisKinds"
154+
| "codeQLCmd"
155+
>;

‎src/init-action.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,18 @@ async function run(
457457

458458
await checkInstallPython311(config.languages, codeql);
459459
} catch (unwrappedError) {
460+
// If we have initialised the Action state, then try to save a subset of it for use by the `init-post` action
461+
// so that it can try to create and upload additional diagnostic information.
462+
if (config !== undefined) {
463+
try {
464+
configUtils.saveAbortedInitConfig(actionState, config);
465+
} catch (configSaveError) {
466+
logger.warning(
467+
`Unable to save configuration for '${ActionName.InitPost}' step: ${getErrorMessage(configSaveError)}`,
468+
);
469+
}
470+
}
471+
460472
const error = wrapError(unwrappedError);
461473
core.setFailed(error.message);
462474
const statusReportBase = await createStatusReportBase(

0 commit comments

Comments
 (0)