Skip to content

Commit 360eb46

Browse files
fix: typo affecting commit and compareSHA (#257)
1 parent 1db06c3 commit 360eb46

File tree

4 files changed

+126
-110
lines changed

4 files changed

+126
-110
lines changed

packages/bundler-plugin-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"zod": "^3.22.4"
4848
},
4949
"devDependencies": {
50-
"@octokit/webhooks-definitions": "^3.67.3",
50+
"@octokit/webhooks-types": "^7.6.1",
5151
"@sentry/core": "^8.42.0",
5252
"@types/node": "^20.11.15",
5353
"@types/semver": "^7.5.6",

packages/bundler-plugin-core/src/utils/providers/GitHubActions.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
} from "../../types.ts";
88
import { type Output } from "../Output.ts";
99
import { debug } from "../logging.ts";
10-
import { type PullRequestEvent } from "@octokit/webhooks-definitions/schema";
10+
import {
11+
type PullRequestEvent,
12+
type MergeGroupEvent,
13+
} from "@octokit/webhooks-types";
1114

1215
export function detect(envs: ProviderEnvs): boolean {
1316
return Boolean(envs?.GITHUB_ACTIONS);
@@ -184,9 +187,12 @@ function _getSHA(
184187

185188
const context = GitHub.context;
186189
let commit = envs?.GITHUB_SHA;
187-
if (["pull_request", " pull_request_target"].includes(context.eventName)) {
190+
if (["pull_request", "pull_request_target"].includes(context.eventName)) {
188191
const payload = context.payload as PullRequestEvent;
189192
commit = payload.pull_request.head.sha;
193+
} else if ("merge_group" === context.eventName) {
194+
const payload = context.payload as MergeGroupEvent;
195+
commit = payload.merge_group.head_sha;
190196
}
191197

192198
debug(`Using commit: ${commit ?? null}`, { enabled: output.debug });
@@ -205,9 +211,12 @@ function _getCompareSHA(
205211

206212
let compareSha = null;
207213
const context = GitHub.context;
208-
if (["pull_request", " pull_request_target"].includes(context.eventName)) {
214+
if (["pull_request", "pull_request_target"].includes(context.eventName)) {
209215
const payload = context.payload as PullRequestEvent;
210216
compareSha = payload.pull_request.base.sha;
217+
} else if ("merge_group" === context.eventName) {
218+
const payload = context.payload as MergeGroupEvent;
219+
compareSha = payload.merge_group.base_sha;
211220
}
212221

213222
debug(`Using compareSha: ${compareSha}`, { enabled: output.debug });

packages/bundler-plugin-core/src/utils/providers/__tests__/GitHubActions.test.ts

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("GitHub Actions Params", () => {
5454
headLabel = "",
5555
}: {
5656
data?: object;
57-
eventName?: "" | "pull_request" | "pull_request_target";
57+
eventName?: "" | "pull_request" | "pull_request_target" | "merge_group";
5858
baseLabel?: string;
5959
headLabel?: string;
6060
} = { data: {}, eventName: "" },
@@ -63,22 +63,38 @@ describe("GitHub Actions Params", () => {
6363
mocks.baseLabel.mockReturnValue(baseLabel);
6464
mocks.headLabel.mockReturnValue(headLabel);
6565

66-
vi.mocked(GitHub).context = {
67-
eventName,
68-
payload: {
69-
// @ts-expect-error - forcing the payload to be a PullRequestEvent
70-
pull_request: {
71-
head: {
72-
sha: "test-head-sha",
73-
label: headLabel,
66+
// not sure if empty string is correct here but for parity with previous tests
67+
// TODO: verify that empty string belongs here, from a glance it seems PushEvent does not
68+
// include a pull_request key
69+
if (["pull_request", "pull_request_target", ""].includes(eventName)) {
70+
vi.mocked(GitHub).context = {
71+
eventName,
72+
payload: {
73+
// @ts-expect-error - forcing the payload to be a PullRequestEvent
74+
pull_request: {
75+
head: {
76+
sha: "test-head-sha",
77+
label: headLabel,
78+
},
79+
base: {
80+
sha: "test-base-sha",
81+
label: baseLabel,
82+
},
7483
},
75-
base: {
76-
sha: "test-base-sha",
77-
label: baseLabel,
84+
},
85+
};
86+
} else if (eventName === "merge_group") {
87+
// @ts-expect-error - forcing the payload to be a MergeGroupEvent
88+
vi.mocked(GitHub).context = {
89+
eventName,
90+
payload: {
91+
merge_group: {
92+
base_sha: "test-base-sha",
93+
head_sha: "test-head-sha",
7894
},
7995
},
80-
},
81-
};
96+
};
97+
}
8298

8399
server.use(
84100
http.get(
@@ -324,7 +340,66 @@ describe("GitHub Actions Params", () => {
324340
build: "2",
325341
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
326342
commit: "test-head-sha",
327-
compareSha: null,
343+
compareSha: "test-base-sha",
344+
job: "testWorkflow",
345+
pr: "1",
346+
service: "github-actions",
347+
slug: "testOrg/testRepo",
348+
};
349+
expect(params).toMatchObject(expected);
350+
});
351+
352+
it("gets the correct branch from a merge group CI run", async () => {
353+
setup({
354+
data: {
355+
jobs: [
356+
{
357+
id: 1,
358+
name: "fakeJob",
359+
html_url: "https://fake.com",
360+
},
361+
],
362+
},
363+
eventName: "merge_group",
364+
baseLabel: "codecov:baseBranch",
365+
headLabel: "testOrg:headBranch",
366+
});
367+
368+
const inputs: ProviderUtilInputs = {
369+
args: { ...createEmptyArgs() },
370+
envs: {
371+
GITHUB_ACTIONS: "true",
372+
GITHUB_HEAD_REF: "branch",
373+
GITHUB_JOB: "testJob",
374+
GITHUB_REF: "refs/pull/1/merge",
375+
GITHUB_REPOSITORY: "testOrg/testRepo",
376+
GITHUB_RUN_ID: "2",
377+
GITHUB_SERVER_URL: "https://github.com",
378+
GITHUB_SHA: "test-head-sha",
379+
GITHUB_WORKFLOW: "testWorkflow",
380+
},
381+
};
382+
383+
const output = new Output(
384+
{
385+
apiUrl: "http://localhost",
386+
bundleName: "GHA-test",
387+
debug: false,
388+
dryRun: true,
389+
enableBundleAnalysis: true,
390+
retryCount: 0,
391+
telemetry: false,
392+
},
393+
{ metaFramework: "vite" },
394+
);
395+
const params = await GitHubActions.getServiceParams(inputs, output);
396+
397+
const expected: ProviderServiceParams = {
398+
branch: "branch",
399+
build: "2",
400+
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
401+
commit: "test-head-sha",
402+
compareSha: "test-base-sha",
328403
job: "testWorkflow",
329404
pr: "1",
330405
service: "github-actions",
@@ -383,7 +458,7 @@ describe("GitHub Actions Params", () => {
383458
build: "2",
384459
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
385460
commit: "test-head-sha",
386-
compareSha: null,
461+
compareSha: "test-base-sha",
387462
job: "testWorkflow",
388463
pr: "1",
389464
service: "github-actions",

0 commit comments

Comments
 (0)