Skip to content

Commit 96d12db

Browse files
committed
fix: skip draft before status telemetry
1 parent affb5f3 commit 96d12db

4 files changed

Lines changed: 87 additions & 40 deletions

File tree

‎lib/entry-points.js‎

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

‎src/analysis-skip.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,8 @@ export function getPullRequestCommitShas(
199199
return { base: baseSha, head: headSha };
200200
}
201201

202-
/** Determine whether the current run should stop before CodeQL initialization. */
203-
export async function getAnalysisSkipReason(
204-
logger: Logger,
205-
): Promise<string | undefined> {
202+
/** Determine whether a confirmed draft should skip before status reporting. */
203+
export function getDraftAnalysisSkipReason(logger: Logger): string | undefined {
206204
const skipIfDraft = actionsUtil.getOptionalInput("skip-if-draft") === "true";
207205
const draftState = getDraftState();
208206
const managedWorkflowDraft = process.env.CODE_SCANNING_IS_DRAFT === "true";
@@ -214,7 +212,13 @@ export async function getAnalysisSkipReason(
214212
"Draft status is unavailable in this workflow event; continuing with analysis.",
215213
);
216214
}
215+
return undefined;
216+
}
217217

218+
/** Determine whether the current run should stop before CodeQL initialization. */
219+
export async function getAnalysisSkipReason(
220+
logger: Logger,
221+
): Promise<string | undefined> {
218222
const skipUnchangedLanguage =
219223
actionsUtil.getOptionalInput("skip-if-no-language-changes") === "true";
220224
if (!skipUnchangedLanguage) {

‎src/init-action.test.ts‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,26 @@ test.serial(
4747
};
4848

4949
try {
50-
sinon.stub(apiClient, "getGitHubVersion").resolves({
51-
type: util.GitHubVariant.DOTCOM,
52-
});
53-
sinon.stub(featureFlags, "initFeatures").returns(createFeatures([]));
50+
const getGitHubVersionStub = sinon
51+
.stub(apiClient, "getGitHubVersion")
52+
.resolves({
53+
type: util.GitHubVariant.DOTCOM,
54+
});
55+
const initFeaturesStub = sinon
56+
.stub(featureFlags, "initFeatures")
57+
.returns(createFeatures([]));
5458
sinon
5559
.stub(analyses, "getAnalysisKinds")
5660
.resolves([analyses.AnalysisKind.CodeScanning]);
57-
sinon.stub(configFile, "getConfigFileInput").resolves(undefined);
58-
sinon.stub(statusReport, "createStatusReportBase").resolves(undefined);
61+
const getConfigFileInputStub = sinon
62+
.stub(configFile, "getConfigFileInput")
63+
.resolves(undefined);
64+
const createStatusReportBaseStub = sinon
65+
.stub(statusReport, "createStatusReportBase")
66+
.resolves(undefined);
67+
const sendStatusReportStub = sinon
68+
.stub(statusReport, "sendStatusReport")
69+
.resolves();
5970
sinon.stub(util, "checkDiskUsage").resolves(undefined);
6071
sinon.stub(util, "checkForTimeout").resolves();
6172

@@ -71,6 +82,11 @@ test.serial(
7182

7283
await runWrapper();
7384

85+
t.false(getGitHubVersionStub.called);
86+
t.false(initFeaturesStub.called);
87+
t.false(getConfigFileInputStub.called);
88+
t.false(createStatusReportBaseStub.called);
89+
t.false(sendStatusReportStub.called);
7490
t.false(getToolsInputStub.called);
7591
t.false(initCodeQLStub.called);
7692
t.false(databaseInitStub.called);

‎src/init-action.ts‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import {
1616
persistInputs,
1717
} from "./actions-util";
1818
import { AnalysisKind, getAnalysisKinds } from "./analyses";
19-
import { getAnalysisSkipReason } from "./analysis-skip";
19+
import {
20+
getAnalysisSkipReason,
21+
getDraftAnalysisSkipReason,
22+
} from "./analysis-skip";
2023
import { getGitHubVersion, GitHubApiCombinedDetails } from "./api-client";
2124
import {
2225
getDependencyCachingEnabled,
@@ -201,6 +204,17 @@ async function sendCompletedStatusReport(
201204
}
202205
}
203206

207+
function markAnalysisSkipped(reason: string, logger: Logger) {
208+
logger.info(
209+
`Skipping CodeQL before tool download and database initialization: ${reason}.`,
210+
);
211+
core.exportVariable(EnvVar.ANALYSIS_SKIP_REASON, reason);
212+
core.exportVariable(EnvVar.JOB_STATUS, JobStatus.SuccessStatus);
213+
core.exportVariable(EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY, "true");
214+
core.setOutput("analysis-skipped", "true");
215+
core.setOutput("analysis-skip-reason", reason);
216+
}
217+
204218
async function run(
205219
actionState: ActionState<["Base", "Logger", "Env", "Actions"]>,
206220
) {
@@ -228,6 +242,22 @@ async function run(
228242
// Make inputs accessible in the `post` step.
229243
persistInputs();
230244

245+
// This is a workflow configuration error independent of draft state, so
246+
// retain the existing validation before taking the early skip path.
247+
if (process.env[EnvVar.SETUP_CODEQL_ACTION_HAS_RUN] === "true") {
248+
throw new ConfigurationError(
249+
`The 'init' action should not be run in the same workflow as 'setup-codeql'.`,
250+
);
251+
}
252+
253+
const draftSkipReason = getDraftAnalysisSkipReason(logger);
254+
if (draftSkipReason !== undefined) {
255+
// Do not send start/completion status reports here. They make API calls
256+
// before and after the skip, and a draft needs no CodeQL status payload.
257+
markAnalysisSkipped(draftSkipReason, logger);
258+
return;
259+
}
260+
231261
apiDetails = {
232262
auth: getRequiredInput("token"),
233263
externalRepoAuth: getOptionalInput("external-repository-token"),
@@ -290,23 +320,9 @@ async function run(
290320
// Send a status report indicating that an analysis is starting.
291321
await sendStartingStatusReport(startedAt, { analysisKinds }, logger);
292322

293-
// Throw a `ConfigurationError` if the `setup-codeql` action has been run.
294-
if (process.env[EnvVar.SETUP_CODEQL_ACTION_HAS_RUN] === "true") {
295-
throw new ConfigurationError(
296-
`The 'init' action should not be run in the same workflow as 'setup-codeql'.`,
297-
);
298-
}
299-
300323
const analysisSkipReason = await getAnalysisSkipReason(logger);
301324
if (analysisSkipReason !== undefined) {
302-
logger.info(
303-
`Skipping CodeQL before tool download and database initialization: ${analysisSkipReason}.`,
304-
);
305-
core.exportVariable(EnvVar.ANALYSIS_SKIP_REASON, analysisSkipReason);
306-
core.exportVariable(EnvVar.JOB_STATUS, JobStatus.SuccessStatus);
307-
core.exportVariable(EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY, "true");
308-
core.setOutput("analysis-skipped", "true");
309-
core.setOutput("analysis-skip-reason", analysisSkipReason);
325+
markAnalysisSkipped(analysisSkipReason, logger);
310326
await sendCompletedStatusReport(
311327
startedAt,
312328
undefined,

0 commit comments

Comments
 (0)