Skip to content

Commit

Permalink
✨ Support Workflows Triggered By "Push" Event (#24)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gilday committed Jun 5, 2024
1 parent 1a637a9 commit b1b7bd7
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
92 changes: 92 additions & 0 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
@@ -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",
);
});
});
17 changes: 12 additions & 5 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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,
};

0 comments on commit b1b7bd7

Please sign in to comment.