Skip to content

Commit 6479197

Browse files
committed
fix: typo affecting commit and compareSHA
i also added checks for the merge group event just in case
1 parent 1db06c3 commit 6479197

File tree

4 files changed

+125
-112
lines changed

4 files changed

+125
-112
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: 90 additions & 18 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,35 @@ 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+
if (["pull_request", "pull_request_target"].includes(eventName)) {
67+
vi.mocked(GitHub).context = {
68+
eventName,
69+
payload: {
70+
// @ts-expect-error - forcing the payload to be a PullRequestEvent
71+
pull_request: {
72+
head: {
73+
sha: "test-head-sha",
74+
label: headLabel,
75+
},
76+
base: {
77+
sha: "test-base-sha",
78+
label: baseLabel,
79+
},
7480
},
75-
base: {
76-
sha: "test-base-sha",
77-
label: baseLabel,
81+
},
82+
};
83+
} else if (eventName === "merge_group") {
84+
// @ts-expect-error - forcing the payload to be a MergeGroupEvent
85+
vi.mocked(GitHub).context = {
86+
eventName,
87+
payload: {
88+
merge_group: {
89+
base_sha: "test-base-sha",
90+
head_sha: "test-head-sha",
7891
},
7992
},
80-
},
81-
};
93+
};
94+
}
8295

8396
server.use(
8497
http.get(
@@ -264,7 +277,7 @@ describe("GitHub Actions Params", () => {
264277
build: "2",
265278
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
266279
commit: "test-head-sha",
267-
compareSha: null,
280+
compareSha: "test-base-sha",
268281
job: "testWorkflow",
269282
pr: "1",
270283
service: "github-actions",
@@ -324,7 +337,66 @@ describe("GitHub Actions Params", () => {
324337
build: "2",
325338
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
326339
commit: "test-head-sha",
327-
compareSha: null,
340+
compareSha: "test-base-sha",
341+
job: "testWorkflow",
342+
pr: "1",
343+
service: "github-actions",
344+
slug: "testOrg/testRepo",
345+
};
346+
expect(params).toMatchObject(expected);
347+
});
348+
349+
it("gets the correct branch from a merge group CI run", async () => {
350+
setup({
351+
data: {
352+
jobs: [
353+
{
354+
id: 1,
355+
name: "fakeJob",
356+
html_url: "https://fake.com",
357+
},
358+
],
359+
},
360+
eventName: "merge_group",
361+
baseLabel: "codecov:baseBranch",
362+
headLabel: "testOrg:headBranch",
363+
});
364+
365+
const inputs: ProviderUtilInputs = {
366+
args: { ...createEmptyArgs() },
367+
envs: {
368+
GITHUB_ACTIONS: "true",
369+
GITHUB_HEAD_REF: "branch",
370+
GITHUB_JOB: "testJob",
371+
GITHUB_REF: "refs/pull/1/merge",
372+
GITHUB_REPOSITORY: "testOrg/testRepo",
373+
GITHUB_RUN_ID: "2",
374+
GITHUB_SERVER_URL: "https://github.com",
375+
GITHUB_SHA: "test-head-sha",
376+
GITHUB_WORKFLOW: "testWorkflow",
377+
},
378+
};
379+
380+
const output = new Output(
381+
{
382+
apiUrl: "http://localhost",
383+
bundleName: "GHA-test",
384+
debug: false,
385+
dryRun: true,
386+
enableBundleAnalysis: true,
387+
retryCount: 0,
388+
telemetry: false,
389+
},
390+
{ metaFramework: "vite" },
391+
);
392+
const params = await GitHubActions.getServiceParams(inputs, output);
393+
394+
const expected: ProviderServiceParams = {
395+
branch: "branch",
396+
build: "2",
397+
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
398+
commit: "test-head-sha",
399+
compareSha: "test-base-sha",
328400
job: "testWorkflow",
329401
pr: "1",
330402
service: "github-actions",
@@ -383,7 +455,7 @@ describe("GitHub Actions Params", () => {
383455
build: "2",
384456
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
385457
commit: "test-head-sha",
386-
compareSha: null,
458+
compareSha: "test-base-sha",
387459
job: "testWorkflow",
388460
pr: "1",
389461
service: "github-actions",
@@ -451,7 +523,7 @@ describe("GitHub Actions Params", () => {
451523
build: "2",
452524
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2/jobs/2",
453525
commit: "test-head-sha",
454-
compareSha: null,
526+
compareSha: "test-base-sha",
455527
job: "testWorkflow",
456528
pr: "1",
457529
service: "github-actions",

0 commit comments

Comments
 (0)