Skip to content

Commit b296f26

Browse files
committed
Refactor: upload all available debug artifacts in init-post
Previously, we uploaded SARIF artifacts in the `analyze-post` step and database and log artifacts in the `init-post` step. As we migrate to the updated `artifact` dependencies, we want to switch to uploading all artifacts in one step. In order to upload all artifacts in one go and maintain the artifacts at the root of the debug directory, we first move SARIF artifacts to the database directory. This should not affect any other consumers of the SARIF file as this occurs in the `init-post` step.
1 parent e817992 commit b296f26

9 files changed

+78
-167
lines changed

src/analyze-action-post-helper.test.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/analyze-action-post-helper.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import * as core from "@actions/core";
2-
31
import * as actionsUtil from "./actions-util";
4-
import { Config, getConfig } from "./config-utils";
2+
import { getConfig } from "./config-utils";
53
import { getActionsLogger } from "./logging";
64

7-
export async function run(
8-
uploadSarifDebugArtifact: (
9-
config: Config,
10-
outputDir: string,
11-
) => Promise<void>,
12-
) {
5+
export async function run() {
136
const logger = getActionsLogger();
147

158
const config = await getConfig(actionsUtil.getTemporaryDirectory(), logger);
@@ -18,13 +11,4 @@ export async function run(
1811
"Config file could not be found at expected location. Did the 'init' action fail to start?",
1912
);
2013
}
21-
22-
// Upload Actions SARIF artifacts for debugging
23-
if (config?.debugMode) {
24-
core.info(
25-
"Debug mode is on. Uploading available SARIF files as Actions debugging artifact...",
26-
);
27-
const outputDir = actionsUtil.getRequiredInput("output");
28-
await uploadSarifDebugArtifact(config, outputDir);
29-
}
3014
}

src/analyze-action-post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { wrapError } from "./util";
1212

1313
async function runWrapper() {
1414
try {
15-
await analyzeActionPostHelper.run(debugArtifacts.uploadSarifDebugArtifact);
15+
await analyzeActionPostHelper.run();
1616

1717
// Also run the upload-sarif post action since we're potentially running
1818
// the same steps in the analyze action.

src/analyze-action.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ async function run() {
230230

231231
const apiDetails = getApiDetails();
232232
const outputDir = actionsUtil.getRequiredInput("output");
233+
core.exportVariable(EnvVar.SARIF_RESULTS_OUTPUT_DIR, outputDir);
233234
const threads = util.getThreadsFlag(
234235
actionsUtil.getOptionalInput("threads") || process.env["CODEQL_THREADS"],
235236
logger,

src/debug-artifacts.ts

Lines changed: 62 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getRequiredInput } from "./actions-util";
1010
import { dbIsFinalized } from "./analyze";
1111
import { getCodeQL } from "./codeql";
1212
import { Config } from "./config-utils";
13+
import { EnvVar } from "./environment";
1314
import { Language } from "./languages";
1415
import { Logger } from "./logging";
1516
import {
@@ -23,6 +24,67 @@ export function sanitizeArifactName(name: string): string {
2324
return name.replace(/[^a-zA-Z0-9_\\-]+/g, "");
2425
}
2526

27+
export async function uploadAllAvailableDebugArtifacts(
28+
config: Config,
29+
logger: Logger,
30+
) {
31+
let filesToUpload: string[] = [];
32+
33+
const analyzeActionOutputDir = process.env[EnvVar.SARIF_RESULTS_OUTPUT_DIR];
34+
for (const lang of config.languages) {
35+
// Add any SARIF files, if they exist
36+
if (
37+
analyzeActionOutputDir !== undefined &&
38+
fs.existsSync(analyzeActionOutputDir) &&
39+
fs.lstatSync(analyzeActionOutputDir).isDirectory()
40+
) {
41+
const sarifFile = path.resolve(analyzeActionOutputDir, `${lang}.sarif`);
42+
// Move SARIF to DB location so that they can be uploaded with the same root directory as the other artifacts.
43+
if (fs.existsSync(sarifFile)) {
44+
const sarifInDbLocation = path.resolve(
45+
config.dbLocation,
46+
`${lang}.sarif`,
47+
);
48+
fs.renameSync(sarifFile, sarifInDbLocation);
49+
filesToUpload = filesToUpload.concat(sarifInDbLocation);
50+
}
51+
}
52+
53+
// Add any log files
54+
const databaseDirectory = getCodeQLDatabasePath(config, lang);
55+
const logsDirectory = path.resolve(databaseDirectory, "log");
56+
if (doesDirectoryExist(logsDirectory)) {
57+
filesToUpload = filesToUpload.concat(listFolder(logsDirectory));
58+
}
59+
60+
// Multilanguage tracing: there are additional logs in the root of the cluster
61+
const multiLanguageTracingLogsDirectory = path.resolve(
62+
config.dbLocation,
63+
"log",
64+
);
65+
if (doesDirectoryExist(multiLanguageTracingLogsDirectory)) {
66+
filesToUpload = filesToUpload.concat(
67+
listFolder(multiLanguageTracingLogsDirectory),
68+
);
69+
}
70+
71+
// Add database bundle
72+
let databaseBundlePath: string;
73+
if (!dbIsFinalized(config, lang, logger)) {
74+
databaseBundlePath = await createPartialDatabaseBundle(config, lang);
75+
} else {
76+
databaseBundlePath = await createDatabaseBundleCli(config, lang);
77+
}
78+
filesToUpload = filesToUpload.concat(databaseBundlePath);
79+
}
80+
81+
await uploadDebugArtifacts(
82+
filesToUpload,
83+
config.dbLocation,
84+
config.debugArtifactName,
85+
);
86+
}
87+
2688
export async function uploadDebugArtifacts(
2789
toUpload: string[],
2890
rootDir: string,
@@ -63,50 +125,6 @@ export async function uploadDebugArtifacts(
63125
}
64126
}
65127

66-
export async function uploadSarifDebugArtifact(
67-
config: Config,
68-
outputDir: string,
69-
) {
70-
if (!doesDirectoryExist(outputDir)) {
71-
return;
72-
}
73-
74-
let toUpload: string[] = [];
75-
for (const lang of config.languages) {
76-
const sarifFile = path.resolve(outputDir, `${lang}.sarif`);
77-
if (fs.existsSync(sarifFile)) {
78-
toUpload = toUpload.concat(sarifFile);
79-
}
80-
}
81-
await uploadDebugArtifacts(toUpload, outputDir, config.debugArtifactName);
82-
}
83-
84-
export async function uploadLogsDebugArtifact(config: Config) {
85-
let toUpload: string[] = [];
86-
for (const language of config.languages) {
87-
const databaseDirectory = getCodeQLDatabasePath(config, language);
88-
const logsDirectory = path.resolve(databaseDirectory, "log");
89-
if (doesDirectoryExist(logsDirectory)) {
90-
toUpload = toUpload.concat(listFolder(logsDirectory));
91-
}
92-
}
93-
94-
// Multilanguage tracing: there are additional logs in the root of the cluster
95-
const multiLanguageTracingLogsDirectory = path.resolve(
96-
config.dbLocation,
97-
"log",
98-
);
99-
if (doesDirectoryExist(multiLanguageTracingLogsDirectory)) {
100-
toUpload = toUpload.concat(listFolder(multiLanguageTracingLogsDirectory));
101-
}
102-
103-
await uploadDebugArtifacts(
104-
toUpload,
105-
config.dbLocation,
106-
config.debugArtifactName,
107-
);
108-
}
109-
110128
/**
111129
* If a database has not been finalized, we cannot run the `codeql database bundle`
112130
* command in the CLI because it will return an error. Instead we directly zip
@@ -150,31 +168,3 @@ async function createDatabaseBundleCli(
150168
);
151169
return databaseBundlePath;
152170
}
153-
154-
export async function uploadDatabaseBundleDebugArtifact(
155-
config: Config,
156-
logger: Logger,
157-
) {
158-
for (const language of config.languages) {
159-
try {
160-
let databaseBundlePath: string;
161-
if (!dbIsFinalized(config, language, logger)) {
162-
databaseBundlePath = await createPartialDatabaseBundle(
163-
config,
164-
language,
165-
);
166-
} else {
167-
databaseBundlePath = await createDatabaseBundleCli(config, language);
168-
}
169-
await uploadDebugArtifacts(
170-
[databaseBundlePath],
171-
config.dbLocation,
172-
config.debugArtifactName,
173-
);
174-
} catch (error) {
175-
core.info(
176-
`Failed to upload database debug bundle for ${config.debugDatabaseName}-${language}: ${error}`,
177-
);
178-
}
179-
}
180-
}

src/environment.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ export enum EnvVar {
6464

6565
ODASA_TRACER_CONFIGURATION = "ODASA_TRACER_CONFIGURATION",
6666

67+
/** The value of the `output` input for the analyze action. */
68+
SARIF_RESULTS_OUTPUT_DIR = "CODEQL_ACTION_SARIF_RESULTS_OUTPUT_DIR",
69+
6770
/**
6871
* What percentage of the total amount of RAM over 8 GB that the Action should reserve for the
6972
* system.

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,19 @@ test("post: init action with debug mode off", async (t) => {
3535
packs: [],
3636
} as unknown as configUtils.Config);
3737

38-
const uploadDatabaseBundleSpy = sinon.spy();
39-
const uploadLogsSpy = sinon.spy();
38+
const uploadAllAvailableDebugArtifactsSpy = sinon.spy();
4039
const printDebugLogsSpy = sinon.spy();
4140

4241
await initActionPostHelper.run(
43-
uploadDatabaseBundleSpy,
44-
uploadLogsSpy,
42+
uploadAllAvailableDebugArtifactsSpy,
4543
printDebugLogsSpy,
4644
createTestConfig({ debugMode: false }),
4745
parseRepositoryNwo("github/codeql-action"),
4846
createFeatures([]),
4947
getRunnerLogger(true),
5048
);
5149

52-
t.assert(uploadDatabaseBundleSpy.notCalled);
53-
t.assert(uploadLogsSpy.notCalled);
50+
t.assert(uploadAllAvailableDebugArtifactsSpy.notCalled);
5451
t.assert(printDebugLogsSpy.notCalled);
5552
});
5653
});
@@ -60,22 +57,19 @@ test("post: init action with debug mode on", async (t) => {
6057
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
6158
process.env["RUNNER_TEMP"] = tmpDir;
6259

63-
const uploadDatabaseBundleSpy = sinon.spy();
64-
const uploadLogsSpy = sinon.spy();
60+
const uploadAllAvailableDebugArtifactsSpy = sinon.spy();
6561
const printDebugLogsSpy = sinon.spy();
6662

6763
await initActionPostHelper.run(
68-
uploadDatabaseBundleSpy,
69-
uploadLogsSpy,
64+
uploadAllAvailableDebugArtifactsSpy,
7065
printDebugLogsSpy,
7166
createTestConfig({ debugMode: true }),
7267
parseRepositoryNwo("github/codeql-action"),
7368
createFeatures([]),
7469
getRunnerLogger(true),
7570
);
7671

77-
t.assert(uploadDatabaseBundleSpy.called);
78-
t.assert(uploadLogsSpy.called);
72+
t.assert(uploadAllAvailableDebugArtifactsSpy.called);
7973
t.assert(printDebugLogsSpy.called);
8074
});
8175
});

src/init-action-post-helper.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,10 @@ export async function tryUploadSarifIfRunFailed(
158158
}
159159

160160
export async function run(
161-
uploadDatabaseBundleDebugArtifact: (
161+
uploadAllAvailableDebugArtifacts: (
162162
config: Config,
163163
logger: Logger,
164164
) => Promise<void>,
165-
uploadLogsDebugArtifact: (config: Config) => Promise<void>,
166165
printDebugLogs: (config: Config) => Promise<void>,
167166
config: Config,
168167
repositoryNwo: RepositoryNwo,
@@ -211,9 +210,7 @@ export async function run(
211210
logger.info(
212211
"Debug mode is on. Uploading available database bundles and logs as Actions debugging artifacts...",
213212
);
214-
await uploadDatabaseBundleDebugArtifact(config, logger);
215-
await uploadLogsDebugArtifact(config);
216-
213+
await uploadAllAvailableDebugArtifacts(config, logger);
217214
await printDebugLogs(config);
218215
}
219216

src/init-action-post.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ async function runWrapper() {
6464
}
6565

6666
uploadFailedSarifResult = await initActionPostHelper.run(
67-
debugArtifacts.uploadDatabaseBundleDebugArtifact,
68-
debugArtifacts.uploadLogsDebugArtifact,
67+
debugArtifacts.uploadAllAvailableDebugArtifacts,
6968
printDebugLogs,
7069
config,
7170
repositoryNwo,

0 commit comments

Comments
 (0)