Skip to content

Commit 7d9b7a1

Browse files
authored
Merge pull request #2274 from github/aeisenberg/no-warn-workflow_call
Avoid warning on workflow_call triggers
2 parents 715d348 + ca7f194 commit 7d9b7a1

7 files changed

+108
-59
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ No user facing changes.
1111
## 3.25.4 - 08 May 2024
1212

1313
- Update default CodeQL bundle version to 2.17.2. [#2270](https://github.com/github/codeql-action/pull/2270)
14+
- 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)
1415

1516
## 3.25.3 - 25 Apr 2024
1617

@@ -34,7 +35,7 @@ No user facing changes.
3435

3536
- The `setup-python-dependencies` input to the `init` Action
3637
- The `CODEQL_ACTION_DISABLE_PYTHON_DEPENDENCY_INSTALLATION` environment variable
37-
38+
3839
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.
3940
- Automatically overwrite an existing database if found on the filesystem. [#2229](https://github.com/github/codeql-action/pull/2229)
4041
- Bump the minimum CodeQL bundle version to 2.12.6. [#2232](https://github.com/github/codeql-action/pull/2232)

lib/workflow.js

+20-27
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

+23-29
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ export interface Workflow {
4747
on?: string | string[] | WorkflowTriggers;
4848
}
4949

50-
function isObject(o: unknown): o is object {
51-
return o !== null && typeof o === "object";
52-
}
53-
5450
const GLOB_PATTERN = new RegExp("(\\*\\*?)");
5551

5652
function escapeRegExp(string) {
@@ -193,37 +189,35 @@ export async function getWorkflowErrors(
193189
}
194190
}
195191

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

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-
);
200+
if (hasPullRequestTrigger && !hasPushTrigger && !hasWorkflowCallTrigger) {
201+
errors.push(WorkflowErrors.MissingPushHook);
202+
}
216203

217-
if (!hasPush && hasPullRequest) {
218-
missingPush = true;
219-
}
204+
return errors;
205+
}
206+
207+
function hasWorkflowTrigger(triggerName: string, doc: Workflow): boolean {
208+
if (!doc.on) {
209+
return false;
220210
}
221211

222-
if (missingPush) {
223-
errors.push(WorkflowErrors.MissingPushHook);
212+
if (typeof doc.on === "string") {
213+
return doc.on === triggerName;
224214
}
225215

226-
return errors;
216+
if (Array.isArray(doc.on)) {
217+
return doc.on.includes(triggerName);
218+
}
219+
220+
return Object.prototype.hasOwnProperty.call(doc.on, triggerName);
227221
}
228222

229223
export async function validateWorkflow(

0 commit comments

Comments
 (0)