Skip to content

Commit ee753b4

Browse files
authored
Merge pull request #3209 from github/mbg/code-quality/skip-failed-upload
Skip failed SARIF upload if Code Quality is the only analysis kind
2 parents 17783bf + db6938a commit ee753b4

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

lib/init-action-post.js

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

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test, { ExecutionContext } from "ava";
22
import * as sinon from "sinon";
33

44
import * as actionsUtil from "./actions-util";
5+
import { AnalysisKind } from "./analyses";
56
import * as codeql from "./codeql";
67
import * as configUtils from "./config-utils";
78
import { Feature } from "./feature-flags";
@@ -28,12 +29,13 @@ test("post: init action with debug mode off", async (t) => {
2829
const gitHubVersion: util.GitHubVersion = {
2930
type: util.GitHubVariant.DOTCOM,
3031
};
31-
sinon.stub(configUtils, "getConfig").resolves({
32-
debugMode: false,
33-
gitHubVersion,
34-
languages: [],
35-
packs: [],
36-
} as unknown as configUtils.Config);
32+
sinon.stub(configUtils, "getConfig").resolves(
33+
createTestConfig({
34+
debugMode: false,
35+
gitHubVersion,
36+
languages: [],
37+
}),
38+
);
3739

3840
const uploadAllAvailableDebugArtifactsSpy = sinon.spy();
3941
const printDebugLogsSpy = sinon.spy();
@@ -295,6 +297,17 @@ test("uploading failed SARIF run fails when workflow does not reference github/c
295297
t.truthy(result.upload_failed_run_stack_trace);
296298
});
297299

300+
test("not uploading failed SARIF when `code-scanning` is not an enabled analysis kind", async (t) => {
301+
const result = await testFailedSarifUpload(t, createTestWorkflow([]), {
302+
analysisKinds: [AnalysisKind.CodeQuality],
303+
expectUpload: false,
304+
});
305+
t.is(
306+
result.upload_failed_run_skipped_because,
307+
"Code Scanning is not enabled.",
308+
);
309+
});
310+
298311
function createTestWorkflow(
299312
steps: workflow.WorkflowJobStep[],
300313
): workflow.Workflow {
@@ -327,20 +340,22 @@ async function testFailedSarifUpload(
327340
expectUpload = true,
328341
exportDiagnosticsEnabled = false,
329342
matrix = {},
343+
analysisKinds = [AnalysisKind.CodeScanning],
330344
}: {
331345
category?: string;
332346
databaseExists?: boolean;
333347
expectUpload?: boolean;
334348
exportDiagnosticsEnabled?: boolean;
335349
matrix?: { [key: string]: string };
350+
analysisKinds?: AnalysisKind[];
336351
} = {},
337352
): Promise<initActionPostHelper.UploadFailedSarifResult> {
338-
const config = {
353+
const config = createTestConfig({
354+
analysisKinds,
339355
codeQLCmd: "codeql",
340356
debugMode: true,
341357
languages: [],
342-
packs: [],
343-
} as unknown as configUtils.Config;
358+
});
344359
if (databaseExists) {
345360
config.dbLocation = "path/to/database";
346361
}

src/init-action-post-helper.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as actionsUtil from "./actions-util";
77
import { CodeScanning } from "./analyses";
88
import { getApiClient } from "./api-client";
99
import { CodeQL, getCodeQL } from "./codeql";
10-
import { Config } from "./config-utils";
10+
import { Config, isCodeScanningEnabled } from "./config-utils";
1111
import * as dependencyCaching from "./dependency-caching";
1212
import { EnvVar } from "./environment";
1313
import { Feature, FeatureEnablement } from "./feature-flags";
@@ -139,6 +139,15 @@ export async function tryUploadSarifIfRunFailed(
139139
EnvVar.JOB_STATUS,
140140
process.env[EnvVar.JOB_STATUS] ?? JobStatus.ConfigErrorStatus,
141141
);
142+
143+
// If the only enabled analysis kind is `code-quality`, then we shouldn't
144+
// upload the failed SARIF to Code Scanning.
145+
if (!isCodeScanningEnabled(config)) {
146+
return {
147+
upload_failed_run_skipped_because: "Code Scanning is not enabled.",
148+
};
149+
}
150+
142151
try {
143152
return await maybeUploadFailedSarif(
144153
config,

0 commit comments

Comments
 (0)