Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/bundler-plugin-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@octokit/webhooks-definitions": "^3.67.3",
"@octokit/webhooks-types": "^7.6.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, thanks for moving this over

"@sentry/core": "^8.42.0",
"@types/node": "^20.11.15",
"@types/semver": "^7.5.6",
Expand Down
15 changes: 12 additions & 3 deletions packages/bundler-plugin-core/src/utils/providers/GitHubActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
} from "../../types.ts";
import { type Output } from "../Output.ts";
import { debug } from "../logging.ts";
import { type PullRequestEvent } from "@octokit/webhooks-definitions/schema";
import {
type PullRequestEvent,
type MergeGroupEvent,
} from "@octokit/webhooks-types";

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

const context = GitHub.context;
let commit = envs?.GITHUB_SHA;
if (["pull_request", " pull_request_target"].includes(context.eventName)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch on that hidden typo

if (["pull_request", "pull_request_target"].includes(context.eventName)) {
const payload = context.payload as PullRequestEvent;
commit = payload.pull_request.head.sha;
} else if ("merge_group" === context.eventName) {
const payload = context.payload as MergeGroupEvent;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it reasonable to write tests for these?

commit = payload.merge_group.head_sha;
}

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

let compareSha = null;
const context = GitHub.context;
if (["pull_request", " pull_request_target"].includes(context.eventName)) {
if (["pull_request", "pull_request_target"].includes(context.eventName)) {
const payload = context.payload as PullRequestEvent;
compareSha = payload.pull_request.base.sha;
} else if ("merge_group" === context.eventName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll defer to @JerrySentry - any backend issues if we use the synthetic merge group commit sha to upload bundle sizes? I tried to find the history but seem to recall an issue in the past related to that

const payload = context.payload as MergeGroupEvent;
compareSha = payload.merge_group.base_sha;
}

debug(`Using compareSha: ${compareSha}`, { enabled: output.debug });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("GitHub Actions Params", () => {
headLabel = "",
}: {
data?: object;
eventName?: "" | "pull_request" | "pull_request_target";
eventName?: "" | "pull_request" | "pull_request_target" | "merge_group";
baseLabel?: string;
headLabel?: string;
} = { data: {}, eventName: "" },
Expand All @@ -63,22 +63,35 @@ describe("GitHub Actions Params", () => {
mocks.baseLabel.mockReturnValue(baseLabel);
mocks.headLabel.mockReturnValue(headLabel);

vi.mocked(GitHub).context = {
eventName,
payload: {
// @ts-expect-error - forcing the payload to be a PullRequestEvent
pull_request: {
head: {
sha: "test-head-sha",
label: headLabel,
if (["pull_request", "pull_request_target"].includes(eventName)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/utils/providers/tests/GitHubActions.test.ts > GitHub Actions Params > gets correct params for a push event

Saw there is a failing unit test ^. For that, I think you can add "" for the mock creator here like if (["pull_request", "pull_request_target", ""].includes(eventName)) {.
That is to handle the case of setup({ eventName: "" }); in the "gets correct params for a push event" test.

vi.mocked(GitHub).context = {
eventName,
payload: {
// @ts-expect-error - forcing the payload to be a PullRequestEvent
pull_request: {
head: {
sha: "test-head-sha",
label: headLabel,
},
base: {
sha: "test-base-sha",
label: baseLabel,
},
},
base: {
sha: "test-base-sha",
label: baseLabel,
},
};
} else if (eventName === "merge_group") {
// @ts-expect-error - forcing the payload to be a MergeGroupEvent
vi.mocked(GitHub).context = {
eventName,
payload: {
merge_group: {
base_sha: "test-base-sha",
head_sha: "test-head-sha",
},
},
},
};
};
}

server.use(
http.get(
Expand Down Expand Up @@ -264,7 +277,7 @@ describe("GitHub Actions Params", () => {
build: "2",
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
commit: "test-head-sha",
compareSha: null,
compareSha: "test-base-sha",
job: "testWorkflow",
pr: "1",
service: "github-actions",
Expand Down Expand Up @@ -324,7 +337,66 @@ describe("GitHub Actions Params", () => {
build: "2",
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
commit: "test-head-sha",
compareSha: null,
compareSha: "test-base-sha",
job: "testWorkflow",
pr: "1",
service: "github-actions",
slug: "testOrg/testRepo",
};
expect(params).toMatchObject(expected);
});

it("gets the correct branch from a merge group CI run", async () => {
setup({
data: {
jobs: [
{
id: 1,
name: "fakeJob",
html_url: "https://fake.com",
},
],
},
eventName: "merge_group",
baseLabel: "codecov:baseBranch",
headLabel: "testOrg:headBranch",
});

const inputs: ProviderUtilInputs = {
args: { ...createEmptyArgs() },
envs: {
GITHUB_ACTIONS: "true",
GITHUB_HEAD_REF: "branch",
GITHUB_JOB: "testJob",
GITHUB_REF: "refs/pull/1/merge",
GITHUB_REPOSITORY: "testOrg/testRepo",
GITHUB_RUN_ID: "2",
GITHUB_SERVER_URL: "https://github.com",
GITHUB_SHA: "test-head-sha",
GITHUB_WORKFLOW: "testWorkflow",
},
};

const output = new Output(
{
apiUrl: "http://localhost",
bundleName: "GHA-test",
debug: false,
dryRun: true,
enableBundleAnalysis: true,
retryCount: 0,
telemetry: false,
},
{ metaFramework: "vite" },
);
const params = await GitHubActions.getServiceParams(inputs, output);

const expected: ProviderServiceParams = {
branch: "branch",
build: "2",
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
commit: "test-head-sha",
compareSha: "test-base-sha",
job: "testWorkflow",
pr: "1",
service: "github-actions",
Expand Down Expand Up @@ -383,7 +455,7 @@ describe("GitHub Actions Params", () => {
build: "2",
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2",
commit: "test-head-sha",
compareSha: null,
compareSha: "test-base-sha",
job: "testWorkflow",
pr: "1",
service: "github-actions",
Expand Down Expand Up @@ -451,7 +523,7 @@ describe("GitHub Actions Params", () => {
build: "2",
buildURL: "https://github.com/testOrg/testRepo/actions/runs/2/jobs/2",
commit: "test-head-sha",
compareSha: null,
compareSha: "test-base-sha",
job: "testWorkflow",
pr: "1",
service: "github-actions",
Expand Down
Loading
Loading