From b1b7bd7f18081a4bb69054fcd64f3c1341d29249 Mon Sep 17 00:00:00 2001 From: Johnathan Gilday Date: Wed, 5 Jun 2024 09:21:08 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Support=20Workflows=20Triggered=20B?= =?UTF-8?q?y=20"Push"=20Event=20(#24)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds support for workflows triggered by a "push" event. This is necessary when integrating with tools that scan on pushes to the default branch, because it allows this action to be appended at the end of the workflow, to share the results of the scan with Pixeebot. /closes #work --- __tests__/github.test.ts | 92 ++++++++++++++++++++++++++++++++++++++++ src/github.ts | 17 +++++--- 2 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 __tests__/github.test.ts 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, };