diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts new file mode 100644 index 0000000..9259a16 --- /dev/null +++ b/__tests__/github.test.ts @@ -0,0 +1,92 @@ +const fakeContext = { + sha: "workflow-sha", + repo: { + owner: "owner", + repo: "repo", + }, +}; +describe("github", () => { + beforeEach(() => { + jest.resetModules(); + jest.clearAllMocks(); + }); + + it("creates GitHubContext from push event", () => { + jest.doMock("@actions/github", () => ({ + context: { + ...fakeContext, + eventName: "push", + }, + })); + const github = require("../src/github"); + + const result = github.getGitHubContext(); + expect(result).toEqual({ + owner: "owner", + repo: "repo", + sha: "workflow-sha", + }); + }); + + it("creates GitHubContext from pull_request event", () => { + jest.doMock("@actions/github", () => ({ + context: { + ...fakeContext, + eventName: "pull_request", + issue: { number: 42 }, + payload: { + pull_request: { + head: { sha: "pr-sha" }, + }, + }, + }, + })); + const github = require("../src/github"); + + const result = github.getGitHubContext(); + expect(result).toEqual({ + owner: "owner", + repo: "repo", + sha: "pr-sha", + prNumber: 42, + }); + }); + + it("creates GitHubContext from check_run event", () => { + jest.doMock("@actions/github", () => ({ + context: { + ...fakeContext, + eventName: "check_run", + payload: { + check_run: { + pull_requests: [{ number: 42 }], + head_sha: "check-run-sha", + }, + }, + }, + })); + const github = require("../src/github"); + + const result = github.getGitHubContext(); + expect(result).toEqual({ + owner: "owner", + repo: "repo", + sha: "check-run-sha", + prNumber: 42, + }); + }); + + it("throws an error for unsupported event", () => { + jest.doMock("@actions/github", () => ({ + context: { + ...fakeContext, + eventName: "unsupported", + }, + })); + const github = require("../src/github"); + + expect(() => github.getGitHubContext()).toThrow( + "Unsupported event: unsupported", + ); + }); +}); diff --git a/src/github.ts b/src/github.ts index 339179c..a5d16a4 100644 --- a/src/github.ts +++ b/src/github.ts @@ -26,12 +26,13 @@ export interface PullRequestInfo { */ export function getGitHubContext(): GitHubContext { const context = github.context; - const { eventName, sha } = context; + const { eventName } = context; - const commitInfo = - eventName !== "workflow_dispatch" - ? eventHandlers[eventName](context) - : { sha }; + const handler = eventHandlers[eventName]; + if (!handler) { + throw new Error(`Unsupported event: ${eventName}`); + } + const commitInfo = handler(context); return { ...getRepositoryInfo(), ...commitInfo }; } @@ -71,9 +72,15 @@ function getCheckRunContext(context: Context): PullRequestInfo { return { prNumber, sha }; } +function getShaFromContext(context: Context): PullRequestInfo { + return { sha: context.sha }; +} + const eventHandlers: { [eventName: string]: (context: Context) => PullRequestInfo; } = { check_run: getCheckRunContext, pull_request: getPullRequestContext, + push: getShaFromContext, + workflow_dispatch: getShaFromContext, };