Skip to content

Commit 55c0837

Browse files
committed
Move checkWorkflow to workflow.ts
1 parent 5060176 commit 55c0837

File tree

11 files changed

+1607
-1678
lines changed

11 files changed

+1607
-1678
lines changed

‎lib/analyze-action.js‎

Lines changed: 178 additions & 198 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: 424 additions & 427 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: 395 additions & 390 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: 168 additions & 186 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: 156 additions & 176 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: 162 additions & 182 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import { loadPropertiesFromApi } from "./feature-flags/properties";
4040
import {
4141
checkInstallPython311,
4242
checkPacksForOverlayCompatibility,
43-
checkWorkflow,
4443
cleanupDatabaseClusterDirectory,
4544
initCodeQL,
4645
initConfig,
@@ -87,6 +86,7 @@ import {
8786
getErrorMessage,
8887
BuildMode,
8988
} from "./util";
89+
import { checkWorkflow } from "./workflow";
9090

9191
/**
9292
* Sends a status report indicating that the `init` Action is starting.

‎src/init.test.ts‎

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

44
import test, { ExecutionContext } from "ava";
5-
import * as sinon from "sinon";
65

7-
import * as actionsUtil from "./actions-util";
86
import { createStubCodeQL } from "./codeql";
9-
import { EnvVar } from "./environment";
107
import {
118
checkPacksForOverlayCompatibility,
12-
checkWorkflow,
139
cleanupDatabaseClusterDirectory,
1410
} from "./init";
1511
import { KnownLanguage } from "./languages";
1612
import {
1713
LoggedMessage,
18-
checkExpectedLogMessages,
1914
createTestConfig,
2015
getRecordingLogger,
2116
setupTests,
2217
} from "./testing-utils";
2318
import { ConfigurationError, withTmpDir } from "./util";
24-
import * as workflow from "./workflow";
2519

2620
setupTests(test);
2721

28-
test("checkWorkflow - validates workflow if `SKIP_WORKFLOW_VALIDATION` is not set", async (t) => {
29-
const messages: LoggedMessage[] = [];
30-
const codeql = createStubCodeQL({});
31-
32-
sinon.stub(actionsUtil, "isDynamicWorkflow").returns(false);
33-
const validateWorkflow = sinon.stub(workflow, "validateWorkflow");
34-
validateWorkflow.resolves(undefined);
35-
36-
await checkWorkflow(getRecordingLogger(messages), codeql);
37-
38-
t.assert(
39-
validateWorkflow.calledOnce,
40-
"`checkWorkflow` unexpectedly did not call `validateWorkflow`",
41-
);
42-
checkExpectedLogMessages(t, messages, [
43-
"Detected no issues with the code scanning workflow.",
44-
]);
45-
});
46-
47-
test("checkWorkflow - logs problems with workflow validation", async (t) => {
48-
const messages: LoggedMessage[] = [];
49-
const codeql = createStubCodeQL({});
50-
51-
sinon.stub(actionsUtil, "isDynamicWorkflow").returns(false);
52-
const validateWorkflow = sinon.stub(workflow, "validateWorkflow");
53-
validateWorkflow.resolves("problem");
54-
55-
await checkWorkflow(getRecordingLogger(messages), codeql);
56-
57-
t.assert(
58-
validateWorkflow.calledOnce,
59-
"`checkWorkflow` unexpectedly did not call `validateWorkflow`",
60-
);
61-
checkExpectedLogMessages(t, messages, [
62-
"Unable to validate code scanning workflow: problem",
63-
]);
64-
});
65-
66-
test("checkWorkflow - skips validation if `SKIP_WORKFLOW_VALIDATION` is `true`", async (t) => {
67-
process.env[EnvVar.SKIP_WORKFLOW_VALIDATION] = "true";
68-
69-
const messages: LoggedMessage[] = [];
70-
const codeql = createStubCodeQL({});
71-
72-
sinon.stub(actionsUtil, "isDynamicWorkflow").returns(false);
73-
const validateWorkflow = sinon.stub(workflow, "validateWorkflow");
74-
75-
await checkWorkflow(getRecordingLogger(messages), codeql);
76-
77-
t.assert(
78-
validateWorkflow.notCalled,
79-
"`checkWorkflow` called `validateWorkflow` unexpectedly",
80-
);
81-
t.is(messages.length, 0);
82-
});
83-
84-
test("checkWorkflow - skips validation for `dynamic` workflows", async (t) => {
85-
const messages: LoggedMessage[] = [];
86-
const codeql = createStubCodeQL({});
87-
88-
const isDynamicWorkflow = sinon
89-
.stub(actionsUtil, "isDynamicWorkflow")
90-
.returns(true);
91-
const validateWorkflow = sinon.stub(workflow, "validateWorkflow");
92-
93-
await checkWorkflow(getRecordingLogger(messages), codeql);
94-
95-
t.assert(isDynamicWorkflow.calledOnce);
96-
t.assert(
97-
validateWorkflow.notCalled,
98-
"`checkWorkflow` called `validateWorkflow` unexpectedly",
99-
);
100-
t.is(messages.length, 0);
101-
});
102-
10322
test("cleanupDatabaseClusterDirectory cleans up where possible", async (t) => {
10423
await withTmpDir(async (tmpDir: string) => {
10524
const dbLocation = path.resolve(tmpDir, "dbs");

‎src/init.ts‎

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,21 @@
11
import * as fs from "fs";
22
import * as path from "path";
33

4-
import * as core from "@actions/core";
54
import * as toolrunner from "@actions/exec/lib/toolrunner";
65
import * as io from "@actions/io";
76
import * as yaml from "js-yaml";
87

9-
import {
10-
getOptionalInput,
11-
isDynamicWorkflow,
12-
isSelfHostedRunner,
13-
} from "./actions-util";
8+
import { getOptionalInput, isSelfHostedRunner } from "./actions-util";
149
import { GitHubApiDetails } from "./api-client";
1510
import { CodeQL, setupCodeQL } from "./codeql";
1611
import * as configUtils from "./config-utils";
17-
import { EnvVar } from "./environment";
1812
import { CodeQLDefaultVersionInfo, FeatureEnablement } from "./feature-flags";
1913
import { KnownLanguage, Language } from "./languages";
2014
import { Logger, withGroupAsync } from "./logging";
2115
import { ToolsSource } from "./setup-codeql";
2216
import { ZstdAvailability } from "./tar";
2317
import { ToolsDownloadStatusReport } from "./tools-download";
2418
import * as util from "./util";
25-
import { validateWorkflow } from "./workflow";
26-
27-
/**
28-
* A wrapper around `validateWorkflow` which reports the outcome.
29-
*
30-
* @param logger The logger to use.
31-
* @param codeql The CodeQL instance.
32-
*/
33-
export async function checkWorkflow(logger: Logger, codeql: CodeQL) {
34-
// Check the workflow for problems, unless `SKIP_WORKFLOW_VALIDATION` is `true`
35-
// or the workflow trigger is `dynamic`.
36-
if (
37-
!isDynamicWorkflow() &&
38-
process.env[EnvVar.SKIP_WORKFLOW_VALIDATION] !== "true"
39-
) {
40-
core.startGroup("Validating workflow");
41-
const validateWorkflowResult = await validateWorkflow(codeql, logger);
42-
if (validateWorkflowResult === undefined) {
43-
logger.info("Detected no issues with the code scanning workflow.");
44-
} else {
45-
logger.warning(
46-
`Unable to validate code scanning workflow: ${validateWorkflowResult}`,
47-
);
48-
}
49-
core.endGroup();
50-
}
51-
}
5219

5320
export async function initCodeQL(
5421
toolsInput: string | undefined,

‎src/workflow.test.ts‎

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ import test, { ExecutionContext } from "ava";
22
import * as yaml from "js-yaml";
33
import * as sinon from "sinon";
44

5-
import { getCodeQLForTesting } from "./codeql";
6-
import { setupTests } from "./testing-utils";
5+
import * as actionsUtil from "./actions-util";
6+
import { createStubCodeQL, getCodeQLForTesting } from "./codeql";
7+
import { EnvVar } from "./environment";
78
import {
9+
checkExpectedLogMessages,
10+
getRecordingLogger,
11+
LoggedMessage,
12+
setupTests,
13+
} from "./testing-utils";
14+
import {
15+
checkWorkflow,
816
CodedError,
917
formatWorkflowCause,
1018
formatWorkflowErrors,
@@ -13,6 +21,7 @@ import {
1321
Workflow,
1422
WorkflowErrors,
1523
} from "./workflow";
24+
import * as workflow from "./workflow";
1625

1726
function errorCodes(
1827
actual: CodedError[],
@@ -870,3 +879,78 @@ test("getCategoryInputOrThrow throws error for workflow with multiple calls to a
870879
},
871880
);
872881
});
882+
883+
test("checkWorkflow - validates workflow if `SKIP_WORKFLOW_VALIDATION` is not set", async (t) => {
884+
const messages: LoggedMessage[] = [];
885+
const codeql = createStubCodeQL({});
886+
887+
sinon.stub(actionsUtil, "isDynamicWorkflow").returns(false);
888+
const validateWorkflow = sinon.stub(workflow.internal, "validateWorkflow");
889+
validateWorkflow.resolves(undefined);
890+
891+
await checkWorkflow(getRecordingLogger(messages), codeql);
892+
893+
t.assert(
894+
validateWorkflow.calledOnce,
895+
"`checkWorkflow` unexpectedly did not call `validateWorkflow`",
896+
);
897+
checkExpectedLogMessages(t, messages, [
898+
"Detected no issues with the code scanning workflow.",
899+
]);
900+
});
901+
902+
test("checkWorkflow - logs problems with workflow validation", async (t) => {
903+
const messages: LoggedMessage[] = [];
904+
const codeql = createStubCodeQL({});
905+
906+
sinon.stub(actionsUtil, "isDynamicWorkflow").returns(false);
907+
const validateWorkflow = sinon.stub(workflow.internal, "validateWorkflow");
908+
validateWorkflow.resolves("problem");
909+
910+
await checkWorkflow(getRecordingLogger(messages), codeql);
911+
912+
t.assert(
913+
validateWorkflow.calledOnce,
914+
"`checkWorkflow` unexpectedly did not call `validateWorkflow`",
915+
);
916+
checkExpectedLogMessages(t, messages, [
917+
"Unable to validate code scanning workflow: problem",
918+
]);
919+
});
920+
921+
test("checkWorkflow - skips validation if `SKIP_WORKFLOW_VALIDATION` is `true`", async (t) => {
922+
process.env[EnvVar.SKIP_WORKFLOW_VALIDATION] = "true";
923+
924+
const messages: LoggedMessage[] = [];
925+
const codeql = createStubCodeQL({});
926+
927+
sinon.stub(actionsUtil, "isDynamicWorkflow").returns(false);
928+
const validateWorkflow = sinon.stub(workflow.internal, "validateWorkflow");
929+
930+
await checkWorkflow(getRecordingLogger(messages), codeql);
931+
932+
t.assert(
933+
validateWorkflow.notCalled,
934+
"`checkWorkflow` called `validateWorkflow` unexpectedly",
935+
);
936+
t.is(messages.length, 0);
937+
});
938+
939+
test("checkWorkflow - skips validation for `dynamic` workflows", async (t) => {
940+
const messages: LoggedMessage[] = [];
941+
const codeql = createStubCodeQL({});
942+
943+
const isDynamicWorkflow = sinon
944+
.stub(actionsUtil, "isDynamicWorkflow")
945+
.returns(true);
946+
const validateWorkflow = sinon.stub(workflow.internal, "validateWorkflow");
947+
948+
await checkWorkflow(getRecordingLogger(messages), codeql);
949+
950+
t.assert(isDynamicWorkflow.calledOnce);
951+
t.assert(
952+
validateWorkflow.notCalled,
953+
"`checkWorkflow` called `validateWorkflow` unexpectedly",
954+
);
955+
t.is(messages.length, 0);
956+
});

0 commit comments

Comments
 (0)