Skip to content

Commit 06fbd89

Browse files
committed
Move workflow check to a function in init.ts and add tests
1 parent 127851b commit 06fbd89

File tree

9 files changed

+1653
-1495
lines changed

9 files changed

+1653
-1495
lines changed

‎lib/analyze-action.js‎

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

‎lib/init-action-post.js‎

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

‎lib/init-action.js‎

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

‎lib/setup-codeql-action.js‎

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

‎lib/upload-lib.js‎

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

‎lib/upload-sarif-action.js‎

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

‎src/init-action.ts‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { loadPropertiesFromApi } from "./feature-flags/properties";
4040
import {
4141
checkInstallPython311,
4242
checkPacksForOverlayCompatibility,
43+
checkWorkflow,
4344
cleanupDatabaseClusterDirectory,
4445
initCodeQL,
4546
initConfig,
@@ -86,7 +87,6 @@ import {
8687
getErrorMessage,
8788
BuildMode,
8889
} from "./util";
89-
import { validateWorkflow } from "./workflow";
9090

9191
/**
9292
* Sends a status report indicating that the `init` Action is starting.
@@ -288,19 +288,9 @@ async function run() {
288288
toolsSource = initCodeQLResult.toolsSource;
289289
zstdAvailability = initCodeQLResult.zstdAvailability;
290290

291-
// Check the workflow for problems, unless `SKIP_WORKFLOW_VALIDATION` is `true`.
292-
if (process.env[EnvVar.SKIP_WORKFLOW_VALIDATION] !== "true") {
293-
core.startGroup("Validating workflow");
294-
const validateWorkflowResult = await validateWorkflow(codeql, logger);
295-
if (validateWorkflowResult === undefined) {
296-
logger.info("Detected no issues with the code scanning workflow.");
297-
} else {
298-
logger.warning(
299-
`Unable to validate code scanning workflow: ${validateWorkflowResult}`,
300-
);
301-
}
302-
core.endGroup();
303-
}
291+
// Check the workflow for problems. If there are any problems, they are reported
292+
// to the workflow log. No exceptions are thrown.
293+
await checkWorkflow(logger, codeql);
304294

305295
// Set CODEQL_ENABLE_EXPERIMENTAL_FEATURES for Rust if between 2.19.3 (included) and 2.22.1 (excluded)
306296
// We need to set this environment variable before initializing the config, otherwise Rust

‎src/init.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,81 @@ import * as fs from "fs";
22
import path from "path";
33

44
import test, { ExecutionContext } from "ava";
5+
import * as sinon from "sinon";
56

67
import { createStubCodeQL } from "./codeql";
8+
import { EnvVar } from "./environment";
79
import {
810
checkPacksForOverlayCompatibility,
11+
checkWorkflow,
912
cleanupDatabaseClusterDirectory,
1013
} from "./init";
1114
import { KnownLanguage } from "./languages";
1215
import {
1316
LoggedMessage,
17+
checkExpectedLogMessages,
1418
createTestConfig,
1519
getRecordingLogger,
1620
setupTests,
1721
} from "./testing-utils";
1822
import { ConfigurationError, withTmpDir } from "./util";
23+
import * as workflow from "./workflow";
1924

2025
setupTests(test);
2126

27+
test("checkWorkflow - validates workflow if `SKIP_WORKFLOW_VALIDATION` is not set", async (t) => {
28+
const messages: LoggedMessage[] = [];
29+
const codeql = createStubCodeQL({});
30+
31+
const validateWorkflow = sinon.stub(workflow, "validateWorkflow");
32+
validateWorkflow.resolves(undefined);
33+
34+
await checkWorkflow(getRecordingLogger(messages), codeql);
35+
36+
t.assert(
37+
validateWorkflow.calledOnce,
38+
"`checkWorkflow` unexpectedly did not call `validateWorkflow`",
39+
);
40+
checkExpectedLogMessages(t, messages, [
41+
"Detected no issues with the code scanning workflow.",
42+
]);
43+
});
44+
45+
test("checkWorkflow - logs problems with workflow validation", async (t) => {
46+
const messages: LoggedMessage[] = [];
47+
const codeql = createStubCodeQL({});
48+
49+
const validateWorkflow = sinon.stub(workflow, "validateWorkflow");
50+
validateWorkflow.resolves("problem");
51+
52+
await checkWorkflow(getRecordingLogger(messages), codeql);
53+
54+
t.assert(
55+
validateWorkflow.calledOnce,
56+
"`checkWorkflow` unexpectedly did not call `validateWorkflow`",
57+
);
58+
checkExpectedLogMessages(t, messages, [
59+
"Unable to validate code scanning workflow: problem",
60+
]);
61+
});
62+
63+
test("checkWorkflow - skips validation if `SKIP_WORKFLOW_VALIDATION` is `true`", async (t) => {
64+
process.env[EnvVar.SKIP_WORKFLOW_VALIDATION] = "true";
65+
66+
const messages: LoggedMessage[] = [];
67+
const codeql = createStubCodeQL({});
68+
69+
const validateWorkflow = sinon.stub(workflow, "validateWorkflow");
70+
71+
await checkWorkflow(getRecordingLogger(messages), codeql);
72+
73+
t.assert(
74+
validateWorkflow.notCalled,
75+
"`checkWorkflow` called `validateWorkflow` unexpectedly",
76+
);
77+
t.is(messages.length, 0);
78+
});
79+
2280
test("cleanupDatabaseClusterDirectory cleans up where possible", async (t) => {
2381
await withTmpDir(async (tmpDir: string) => {
2482
const dbLocation = path.resolve(tmpDir, "dbs");

‎src/init.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from "fs";
22
import * as path from "path";
33

4+
import * as core from "@actions/core";
45
import * as toolrunner from "@actions/exec/lib/toolrunner";
56
import * as io from "@actions/io";
67
import * as yaml from "js-yaml";
@@ -9,13 +10,37 @@ import { getOptionalInput, isSelfHostedRunner } from "./actions-util";
910
import { GitHubApiDetails } from "./api-client";
1011
import { CodeQL, setupCodeQL } from "./codeql";
1112
import * as configUtils from "./config-utils";
13+
import { EnvVar } from "./environment";
1214
import { CodeQLDefaultVersionInfo, FeatureEnablement } from "./feature-flags";
1315
import { KnownLanguage, Language } from "./languages";
1416
import { Logger, withGroupAsync } from "./logging";
1517
import { ToolsSource } from "./setup-codeql";
1618
import { ZstdAvailability } from "./tar";
1719
import { ToolsDownloadStatusReport } from "./tools-download";
1820
import * as util from "./util";
21+
import { validateWorkflow } from "./workflow";
22+
23+
/**
24+
* A wrapper around `validateWorkflow` which reports the outcome.
25+
*
26+
* @param logger The logger to use.
27+
* @param codeql The CodeQL instance.
28+
*/
29+
export async function checkWorkflow(logger: Logger, codeql: CodeQL) {
30+
// Check the workflow for problems, unless `SKIP_WORKFLOW_VALIDATION` is `true`.
31+
if (process.env[EnvVar.SKIP_WORKFLOW_VALIDATION] !== "true") {
32+
core.startGroup("Validating workflow");
33+
const validateWorkflowResult = await validateWorkflow(codeql, logger);
34+
if (validateWorkflowResult === undefined) {
35+
logger.info("Detected no issues with the code scanning workflow.");
36+
} else {
37+
logger.warning(
38+
`Unable to validate code scanning workflow: ${validateWorkflowResult}`,
39+
);
40+
}
41+
core.endGroup();
42+
}
43+
}
1944

2045
export async function initCodeQL(
2146
toolsInput: string | undefined,

0 commit comments

Comments
 (0)