@@ -21,7 +21,11 @@ import {
2121import * as api from "./api-client" ;
2222import { getCachingKind } from "./caching-utils" ;
2323import { 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" ;
2529import {
2630 calculateAugmentation ,
2731 ExcludeQueryFilter ,
@@ -71,6 +75,7 @@ import {
7175import { OverlayDatabaseMode } from "./overlay/overlay-database-mode" ;
7276import { shouldSkipOverlayAnalysis } from "./overlay/status" ;
7377import { RepositoryNwo } from "./repository" ;
78+ import { ActionName } from "./status-report" ;
7479import { ToolsFeature } from "./tools-features" ;
7580import { downloadTrapCaches } from "./trap-caching" ;
7681import {
@@ -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
0 commit comments