Skip to content

Commit 4235601

Browse files
committed
Log error for non-default analysis-kinds input outside of managed workflows
1 parent 3d6ea97 commit 4235601

5 files changed

Lines changed: 147 additions & 13 deletions

File tree

lib/init-action.js

Lines changed: 17 additions & 1 deletion
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: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/analyses.test.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ import {
1616
} from "./analyses";
1717
import { EnvVar } from "./environment";
1818
import { getRunnerLogger } from "./logging";
19-
import { createFeatures, RecordingLogger, setupTests } from "./testing-utils";
19+
import {
20+
createFeatures,
21+
RecordingLogger,
22+
setupBaseActionsVars,
23+
setupTests,
24+
} from "./testing-utils";
2025
import { AssessmentPayload } from "./upload-lib/types";
2126
import { ConfigurationError } from "./util";
2227

@@ -72,6 +77,7 @@ test.serial(
7277
test.serial(
7378
"getAnalysisKinds - only use `code-scanning` for multiple analysis kinds outside of test mode",
7479
async (t) => {
80+
setupBaseActionsVars();
7581
process.env[EnvVar.TEST_MODE] = "false";
7682
const features = createFeatures([]);
7783
const logger = new RecordingLogger();
@@ -89,6 +95,44 @@ test.serial(
8995
},
9096
);
9197

98+
test.serial(
99+
"getAnalysisKinds - logs error for non-default `analysis-kinds` in custom workflow",
100+
async (t) => {
101+
setupBaseActionsVars({ GITHUB_EVENT_NAME: "push" });
102+
process.env[EnvVar.TEST_MODE] = "false";
103+
const features = createFeatures([]);
104+
const logger = new RecordingLogger();
105+
const requiredInputStub = sinon.stub(actionsUtil, "getRequiredInput");
106+
requiredInputStub.withArgs("analysis-kinds").returns("code-quality");
107+
const result = await getAnalysisKinds(logger, features, true);
108+
t.deepEqual(result, [AnalysisKind.CodeQuality]);
109+
t.assert(
110+
logger.hasMessage(
111+
"An analysis kind other than `code-scanning` was specified in a custom workflow.",
112+
),
113+
);
114+
},
115+
);
116+
117+
test.serial(
118+
"getAnalysisKinds - no error for non-default `analysis-kinds` in managed workflow",
119+
async (t) => {
120+
setupBaseActionsVars({ GITHUB_EVENT_NAME: "dynamic" });
121+
process.env[EnvVar.TEST_MODE] = "false";
122+
const features = createFeatures([]);
123+
const logger = new RecordingLogger();
124+
const requiredInputStub = sinon.stub(actionsUtil, "getRequiredInput");
125+
requiredInputStub.withArgs("analysis-kinds").returns("code-quality");
126+
const result = await getAnalysisKinds(logger, features, true);
127+
t.deepEqual(result, [AnalysisKind.CodeQuality]);
128+
t.assert(
129+
!logger.hasMessage(
130+
"An analysis kind other than `code-scanning` was specified in a custom workflow.",
131+
),
132+
);
133+
},
134+
);
135+
92136
test.serial(
93137
"getAnalysisKinds - includes `code-quality` when deprecated `quality-queries` input is used",
94138
async (t) => {
@@ -133,6 +177,7 @@ for (let i = 0; i < analysisKinds.length; i++) {
133177
test.serial(
134178
`getAnalysisKinds - allows ${analysisKind} with ${otherAnalysis}`,
135179
async (t) => {
180+
setupBaseActionsVars();
136181
process.env[EnvVar.TEST_MODE] = "true";
137182
const features = createFeatures([]);
138183
const requiredInputStub = sinon.stub(actionsUtil, "getRequiredInput");
@@ -151,6 +196,7 @@ for (let i = 0; i < analysisKinds.length; i++) {
151196
test.serial(
152197
`getAnalysisKinds - throws if ${analysisKind} is enabled with ${otherAnalysis}`,
153198
async (t) => {
199+
setupBaseActionsVars();
154200
const features = createFeatures([]);
155201
const requiredInputStub = sinon.stub(actionsUtil, "getRequiredInput");
156202
requiredInputStub

src/analyses.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
fixCodeQualityCategory,
33
getOptionalInput,
44
getRequiredInput,
5+
isDynamicWorkflow,
56
} from "./actions-util";
67
import { EnvVar } from "./environment";
78
import { Feature, FeatureEnablement } from "./feature-flags";
@@ -65,6 +66,21 @@ export async function parseAnalysisKinds(
6566
// Used to avoid re-parsing the input after we have done it once.
6667
let cachedAnalysisKinds: AnalysisKind[] | undefined;
6768

69+
/** Determines whether `code-scanning` is the only enabled analysis kind in `analysisKinds`. */
70+
function isOnlyCodeScanningEnabled(analysisKinds: AnalysisKind[]) {
71+
return (
72+
analysisKinds.length === 1 && analysisKinds[0] === AnalysisKind.CodeScanning
73+
);
74+
}
75+
76+
/** Prepends a generic message about the intended usage for `analysis-kinds` to `message`. */
77+
function makeAnalysisKindUsageError(message: string) {
78+
return (
79+
"The `analysis-kinds` input is experimental and for GitHub-internal use only. " +
80+
`Its behaviour may change at any time or be removed entirely. ${message}`
81+
);
82+
}
83+
6884
/**
6985
* Initialises the analysis kinds for the analysis based on the `analysis-kinds` input.
7086
* This function will also use the deprecated `quality-queries` input as an indicator to enable `code-quality`.
@@ -89,6 +105,26 @@ export async function getAnalysisKinds(
89105
getRequiredInput("analysis-kinds"),
90106
);
91107

108+
// Log an error if we are outside of a GitHub-managed workflow and an analysis kind
109+
// other than `code-scanning` is enabled.
110+
if (
111+
!isInTestMode() &&
112+
!isDynamicWorkflow() &&
113+
!isOnlyCodeScanningEnabled(analysisKinds)
114+
) {
115+
const codeQualityHint = analysisKinds.includes(AnalysisKind.CodeQuality)
116+
? " If your intention is to use quality queries outside of Code Quality, " +
117+
"use the `queries` input with `code-quality` instead."
118+
: "";
119+
120+
logger.error(
121+
makeAnalysisKindUsageError(
122+
"An analysis kind other than `code-scanning` was specified in a custom workflow. " +
123+
`This is not supported and will become a fatal error in a future version of the CodeQL Action.${codeQualityHint}`,
124+
),
125+
);
126+
}
127+
92128
// Warn that `quality-queries` is deprecated if there is an argument for it.
93129
const qualityQueriesInput = getOptionalInput("quality-queries");
94130

@@ -130,10 +166,10 @@ export async function getAnalysisKinds(
130166
!(await features.getValue(Feature.AllowMultipleAnalysisKinds))
131167
) {
132168
logger.error(
133-
"The `analysis-kinds` input is experimental and for GitHub-internal use only. " +
134-
"Its behaviour may change at any time or be removed entirely. " +
169+
makeAnalysisKindUsageError(
135170
"Specifying multiple values as input is no longer supported. " +
136-
"Continuing with only `analysis-kinds: code-scanning`.",
171+
"Continuing with only `analysis-kinds: code-scanning`.",
172+
),
137173
);
138174

139175
// Only enable Code Scanning.

src/testing-utils.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,37 @@ export const DEFAULT_ACTIONS_VARS = {
188188
RUNNER_OS: "Linux",
189189
} as const satisfies Record<string, string>;
190190

191-
// Sets environment variables that make using some libraries designed for
192-
// use only on actions safe to use outside of actions.
193-
export function setupActionsVars(
194-
tempDir: string,
195-
toolsDir: string,
196-
overrides?: Partial<Record<keyof typeof DEFAULT_ACTIONS_VARS, string>>,
197-
) {
191+
/** Partial mappings from GitHub Actions environment variables to values. */
192+
export type ActionVarOverrides = Partial<
193+
Record<keyof typeof DEFAULT_ACTIONS_VARS, string>
194+
>;
195+
196+
/**
197+
* Sets environment variables that are always available on GitHub Actions,
198+
* excluding some that are expected to be set to paths. See `setupActionsVars`.
199+
*
200+
* @param overrides Overrides for the defaults.
201+
*/
202+
export function setupBaseActionsVars(overrides?: ActionVarOverrides) {
198203
const vars = { ...DEFAULT_ACTIONS_VARS, ...overrides };
199204
for (const [key, value] of Object.entries(vars)) {
200205
process.env[key] = value;
201206
}
207+
}
208+
209+
/**
210+
* Sets environment variables that are always available on GitHub Actions.
211+
*
212+
* @param tempDir A value for `RUNNER_TEMP` and `GITHUB_WORKSPACE`.
213+
* @param toolsDir A value for `RUNNER_TOOL_CACHE`.
214+
* @param overrides Overrides for the defaults.
215+
*/
216+
export function setupActionsVars(
217+
tempDir: string,
218+
toolsDir: string,
219+
overrides?: ActionVarOverrides,
220+
) {
221+
setupBaseActionsVars(overrides);
202222
process.env["RUNNER_TEMP"] = tempDir;
203223
process.env["RUNNER_TOOL_CACHE"] = toolsDir;
204224
process.env["GITHUB_WORKSPACE"] = tempDir;

0 commit comments

Comments
 (0)