Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions lib/handle-pull-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ describe("handlePullRequest", () => {
]);
});

test("pull request without head repo is not treated as fork", async () => {
const createComment = vi.spyOn(comment, "createComment");
const eventPath = generatePullRequestWebhook({
body: "Pull request body\n/schedule 2022-06-12",
headRepo: null,
});
process.env.GITHUB_EVENT_PATH = eventPath;

await handlePullRequest();

expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
`Schedule date found: "2022-06-12"\n`,
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
]);
expect(createComment.mock.calls).toHaveLength(1);
});

test("no schedule command", async () => {
const eventPath = generatePullRequestWebhook();
process.env.GITHUB_EVENT_PATH = eventPath;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function hasScheduleCommand(text: string | null): boolean {
}

export function isFork(pullRequest: SimplePullRequest): boolean {
return pullRequest.head.repo.fork;
return Boolean(pullRequest.head?.repo?.fork);
}

export function getScheduleDateString(text: string | null): string {
Expand Down
4 changes: 3 additions & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ type PullRequestWebhookOptions = {
state?: "open" | "closed";
body?: string;
fork?: boolean;
headRepo?: { fork?: boolean } | null;
};

export function generatePullRequestWebhook({
number = 2,
state = "open",
body = "Simple body",
fork = false,
headRepo = undefined,
}: PullRequestWebhookOptions = {}) {
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#webhook-payload-example-33
const payload = {
Expand All @@ -34,7 +36,7 @@ export function generatePullRequestWebhook({
state,
body,
head: {
repo: {
repo: headRepo ?? {
fork,
},
},
Expand Down