Skip to content

Commit ee0b233

Browse files
committed
Avoid warning on workflow_call triggers
Typically, we warn when there is no `push` trigger in the workflow file that triggered this run. However, when this action is triggered by a `workflow_call` event, we assume there is a custom process for triggering the action and we don't want to warn in this case.
1 parent 4b812a5 commit ee0b233

7 files changed

+109
-52
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Note that the only difference between `v2` and `v3` of the CodeQL Action is the
77
## [UNRELEASED]
88

99
- Update default CodeQL bundle version to 2.17.2. [#2270](https://github.com/github/codeql-action/pull/2270)
10+
- Avoid printing out a warning for a missing `on.push` trigger when the CodeQL Action is triggered via a `workflow_call` event. [#2274](https://github.com/github/codeql-action/pull/2274)
1011

1112
## 3.25.3 - 25 Apr 2024
1213

@@ -30,7 +31,7 @@ No user facing changes.
3031

3132
- The `setup-python-dependencies` input to the `init` Action
3233
- The `CODEQL_ACTION_DISABLE_PYTHON_DEPENDENCY_INSTALLATION` environment variable
33-
34+
3435
We recommend removing any references to these from your workflows. For more information, see the release notes for CodeQL Action v3.23.0 and v2.23.0.
3536
- Automatically overwrite an existing database if found on the filesystem. [#2229](https://github.com/github/codeql-action/pull/2229)
3637
- Bump the minimum CodeQL bundle version to 2.12.6. [#2232](https://github.com/github/codeql-action/pull/2232)

lib/workflow.js

+20-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/workflow.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/workflow.test.js

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/workflow.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/workflow.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,44 @@ test("getWorkflowErrors() should not report an error if PRs are totally unconfig
643643
);
644644
});
645645

646+
test("getWorkflowErrors() should not report a warning if there is a workflow_call trigger", async (t) => {
647+
const errors = await getWorkflowErrors(
648+
yaml.load(`
649+
name: "CodeQL"
650+
on:
651+
workflow_call:
652+
`) as Workflow,
653+
await getCodeQLForTesting(),
654+
);
655+
656+
t.deepEqual(...errorCodes(errors, []));
657+
});
658+
659+
test("getWorkflowErrors() should not report a warning if there is a workflow_call trigger as a string", async (t) => {
660+
const errors = await getWorkflowErrors(
661+
yaml.load(`
662+
name: "CodeQL"
663+
on: workflow_call
664+
`) as Workflow,
665+
await getCodeQLForTesting(),
666+
);
667+
668+
t.deepEqual(...errorCodes(errors, []));
669+
});
670+
671+
test("getWorkflowErrors() should not report a warning if there is a workflow_call trigger as an array", async (t) => {
672+
const errors = await getWorkflowErrors(
673+
yaml.load(`
674+
name: "CodeQL"
675+
on:
676+
- workflow_call
677+
`) as Workflow,
678+
await getCodeQLForTesting(),
679+
);
680+
681+
t.deepEqual(...errorCodes(errors, []));
682+
});
683+
646684
test("getCategoryInputOrThrow returns category for simple workflow with category", (t) => {
647685
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
648686
t.is(

src/workflow.ts

+24-25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "path";
33
import zlib from "zlib";
44

55
import * as core from "@actions/core";
6+
import * as github from "@actions/github";
67
import * as yaml from "js-yaml";
78

89
import * as api from "./api-client";
@@ -193,37 +194,35 @@ export async function getWorkflowErrors(
193194
}
194195
}
195196

196-
let missingPush = false;
197+
// If there is no push trigger, we will not be able to analyze the default branch.
198+
// So add a warning to the user to add a push trigger.
199+
// If there is a workflow_call trigger, we don't need a push trigger since we assume
200+
// that the workflow_call trigger is called from a workflow that has a push trigger.
201+
const hasPushTrigger = hasWorkflowTrigger("push", doc);
202+
const hasPullRequestTrigger = hasWorkflowTrigger("pull_request", doc);
203+
const hasWorkflowCallTrigger = hasWorkflowTrigger("workflow_call", doc);
197204

198-
if (doc.on === undefined) {
199-
// this is not a valid config
200-
} else if (typeof doc.on === "string") {
201-
if (doc.on === "pull_request") {
202-
missingPush = true;
203-
}
204-
} else if (Array.isArray(doc.on)) {
205-
const hasPush = doc.on.includes("push");
206-
const hasPullRequest = doc.on.includes("pull_request");
207-
if (hasPullRequest && !hasPush) {
208-
missingPush = true;
209-
}
210-
} else if (isObject(doc.on)) {
211-
const hasPush = Object.prototype.hasOwnProperty.call(doc.on, "push");
212-
const hasPullRequest = Object.prototype.hasOwnProperty.call(
213-
doc.on,
214-
"pull_request",
215-
);
205+
if (hasPullRequestTrigger && !hasPushTrigger && !hasWorkflowCallTrigger) {
206+
errors.push(WorkflowErrors.MissingPushHook);
207+
}
216208

217-
if (!hasPush && hasPullRequest) {
218-
missingPush = true;
219-
}
209+
return errors;
210+
}
211+
212+
function hasWorkflowTrigger(triggerName: string, doc: Workflow): boolean {
213+
if (!doc.on) {
214+
return false;
220215
}
221216

222-
if (missingPush) {
223-
errors.push(WorkflowErrors.MissingPushHook);
217+
if (typeof doc.on === "string") {
218+
return doc.on === triggerName;
224219
}
225220

226-
return errors;
221+
if (Array.isArray(doc.on)) {
222+
return doc.on.includes(triggerName);
223+
}
224+
225+
return Object.prototype.hasOwnProperty.call(doc.on, triggerName);
227226
}
228227

229228
export async function validateWorkflow(

0 commit comments

Comments
 (0)