From eb752538cb26cb79d2c51d855e37a0749f5f8f7a Mon Sep 17 00:00:00 2001 From: Rui Saraiva Date: Wed, 22 Jun 2022 10:01:11 +0100 Subject: [PATCH] feat: schedule without date string --- lib/handle-pull-request.test.ts | 26 ++++++++++++++++++++++++++ lib/handle-pull-request.ts | 30 +++++++++++++++++++++--------- lib/handle-schedule.test.ts | 30 ++++++++++++++++++++++++------ lib/handle-schedule.ts | 20 +++++++++++++++----- lib/utils.test.ts | 6 +++--- lib/utils.ts | 2 +- test/mocks/github.ts | 13 +++++++++++++ 7 files changed, 103 insertions(+), 24 deletions(-) diff --git a/lib/handle-pull-request.test.ts b/lib/handle-pull-request.test.ts index e0f01a8..21a82ec 100644 --- a/lib/handle-pull-request.test.ts +++ b/lib/handle-pull-request.test.ts @@ -239,4 +239,30 @@ describe("handlePullRequest", () => { ]); expect(updateComment.mock.calls).toHaveLength(0); }); + + test("schedule merge without date", async () => { + const mockStdout = mockProcessStdout(); + const createComment = vi.spyOn(comment, "createComment"); + const eventPath = generatePullRequestWebhook({ + body: "Pull request body\n/schedule", + }); + process.env.GITHUB_EVENT_PATH = eventPath; + + await handlePullRequest(); + + expect(mockStdout.mock.calls).toEqual([ + [ + "Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n", + ], + [ + `Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`, + ], + ]); + expect(createComment.mock.calls).toHaveLength(1); + expect(createComment.mock.calls[0][2]).toMatchInlineSnapshot(` + ":hourglass: **Merge Schedule** + Scheduled to be merged the next time the merge action is scheduled via the cron expressions + " + `); + }); }); diff --git a/lib/handle-pull-request.ts b/lib/handle-pull-request.ts index 6ed7f64..5f7ba87 100644 --- a/lib/handle-pull-request.ts +++ b/lib/handle-pull-request.ts @@ -62,21 +62,33 @@ export default async function handlePullRequest(): Promise { } const datestring = getScheduleDateString(pullRequest.body); - core.info(`Schedule date found: "${datestring}"`); + if (datestring) { + core.info(`Schedule date found: "${datestring}"`); + } let commentBody = ""; - if (!isValidDate(datestring)) { - commentBody = generateBody(`"${datestring}" is not a valid date`, "error"); - } else if (new Date(datestring) < localeDate()) { - let message = `${stringifyDate(datestring)} (UTC) is already in the past`; - if (process.env.INPUT_TIME_ZONE !== "UTC") { - message = `${message} on ${process.env.INPUT_TIME_ZONE} time zone`; + if (datestring) { + if (!isValidDate(datestring)) { + commentBody = generateBody( + `"${datestring}" is not a valid date`, + "error" + ); + } else if (new Date(datestring) < localeDate()) { + let message = `${stringifyDate(datestring)} (UTC) is already in the past`; + if (process.env.INPUT_TIME_ZONE !== "UTC") { + message = `${message} on ${process.env.INPUT_TIME_ZONE} time zone`; + } + commentBody = generateBody(message, "warning"); + } else { + commentBody = generateBody( + `Scheduled to be merged on ${stringifyDate(datestring)} (UTC)`, + "pending" + ); } - commentBody = generateBody(message, "warning"); } else { commentBody = generateBody( - `Scheduled to be merged on ${stringifyDate(datestring)} (UTC)`, + `Scheduled to be merged the next time the merge action is scheduled via the cron expressions`, "pending" ); } diff --git a/lib/handle-schedule.test.ts b/lib/handle-schedule.test.ts index 9cfaebc..a4f2a15 100644 --- a/lib/handle-schedule.test.ts +++ b/lib/handle-schedule.test.ts @@ -33,8 +33,8 @@ describe("handleSchedule", () => { expect(mockStdout.mock.calls).toEqual([ [`Loading open pull requests\n`], - [`5 scheduled pull requests found\n`], - [`4 due pull requests found\n`], + [`6 scheduled pull requests found\n`], + [`5 due pull requests found\n`], [`https://github.com/gr2m/merge-schedule-action/pull/2 merged\n`], [ `Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`, @@ -51,8 +51,12 @@ describe("handleSchedule", () => { `Comment updated: https://github.com/gr2m/merge-schedule-action/issues/6#issuecomment-61\n`, ], [`Label added: "automerge-fail"\n`], + [`https://github.com/gr2m/merge-schedule-action/pull/7 merged\n`], + [ + `Comment created: https://github.com/gr2m/merge-schedule-action/issues/7#issuecomment-72\n`, + ], ]); - expect(createComment.mock.calls).toHaveLength(2); + expect(createComment.mock.calls).toHaveLength(3); expect(createComment.mock.calls[0][2]).toMatchInlineSnapshot(` ":white_check_mark: **Merge Schedule** Scheduled on 2022-06-08 (UTC) successfully merged @@ -64,6 +68,11 @@ describe("handleSchedule", () => { In order to let the automerge-automation try again, the label \\"automerge-fail\\" should be removed. " `); + expect(createComment.mock.calls[2][2]).toMatchInlineSnapshot(` + ":white_check_mark: **Merge Schedule** + Scheduled on next cron expression successfully merged + " + `); expect(updateComment.mock.calls).toHaveLength(2); expect(updateComment.mock.calls[0][2]).toMatchInlineSnapshot(` ":white_check_mark: **Merge Schedule** @@ -88,8 +97,8 @@ describe("handleSchedule", () => { expect(mockStdout.mock.calls).toEqual([ [`Loading open pull requests\n`], - [`5 scheduled pull requests found\n`], - [`4 due pull requests found\n`], + [`6 scheduled pull requests found\n`], + [`5 due pull requests found\n`], [`https://github.com/gr2m/merge-schedule-action/pull/2 merged\n`], [ `Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`, @@ -105,8 +114,12 @@ describe("handleSchedule", () => { `Comment updated: https://github.com/gr2m/merge-schedule-action/issues/6#issuecomment-61\n`, ], [`Label added: "automerge-fail"\n`], + [`https://github.com/gr2m/merge-schedule-action/pull/7 merged\n`], + [ + `Comment created: https://github.com/gr2m/merge-schedule-action/issues/7#issuecomment-72\n`, + ], ]); - expect(createComment.mock.calls).toHaveLength(2); + expect(createComment.mock.calls).toHaveLength(3); expect(createComment.mock.calls[0][2]).toMatchInlineSnapshot(` ":white_check_mark: **Merge Schedule** Scheduled on 2022-06-08 (UTC) successfully merged @@ -118,6 +131,11 @@ describe("handleSchedule", () => { In order to let the automerge-automation try again, the label \\"automerge-fail\\" should be removed. " `); + expect(createComment.mock.calls[2][2]).toMatchInlineSnapshot(` + ":white_check_mark: **Merge Schedule** + Scheduled on next cron expression successfully merged + " + `); expect(updateComment.mock.calls).toHaveLength(1); expect(updateComment.mock.calls[0][2]).toMatchInlineSnapshot(` ":x: **Merge Schedule** diff --git a/lib/handle-schedule.ts b/lib/handle-schedule.ts index eb1ef89..7266de1 100644 --- a/lib/handle-schedule.ts +++ b/lib/handle-schedule.ts @@ -68,7 +68,9 @@ export default async function handleSchedule(): Promise { } const duePullRequests = pullRequests.filter( - (pullRequest) => new Date(pullRequest.scheduledDate) < localeDate() + (pullRequest) => + pullRequest.scheduledDate === "" || + new Date(pullRequest.scheduledDate) < localeDate() ); core.info(`${duePullRequests.length} due pull requests found`); @@ -140,10 +142,18 @@ export default async function handleSchedule(): Promise { pullRequest.number ); - const commentBody = generateBody( - `Scheduled on ${pullRequest.scheduledDate} (UTC) successfully merged`, - "success" - ); + let commentBody = ""; + if (pullRequest.scheduledDate) { + commentBody = generateBody( + `Scheduled on ${pullRequest.scheduledDate} (UTC) successfully merged`, + "success" + ); + } else { + commentBody = generateBody( + `Scheduled on next cron expression successfully merged`, + "success" + ); + } if (previousComment) { const { data } = await updateComment( diff --git a/lib/utils.test.ts b/lib/utils.test.ts index 070bdfe..f4ef1a6 100644 --- a/lib/utils.test.ts +++ b/lib/utils.test.ts @@ -22,11 +22,11 @@ test("getScheduleDateString", () => { test("hasScheduleCommand", () => { expect(hasScheduleCommand(null)).toBe(false); expect(hasScheduleCommand("")).toBe(false); - expect(hasScheduleCommand("/schedule")).toBe(false); + expect(hasScheduleCommand("/schedule")).toBe(true); expect(hasScheduleCommand("/schedule ")).toBe(true); - expect(hasScheduleCommand("\n/schedule")).toBe(false); + expect(hasScheduleCommand("\n/schedule")).toBe(true); expect(hasScheduleCommand("\n/schedule ")).toBe(true); - expect(hasScheduleCommand("Something\n/schedule")).toBe(false); + expect(hasScheduleCommand("Something\n/schedule")).toBe(true); expect(hasScheduleCommand("Something /schedule ")).toBe(false); expect(hasScheduleCommand("Something\n/schedule ")).toBe(true); expect(hasScheduleCommand("Something /schedule \nelse")).toBe(false); diff --git a/lib/utils.ts b/lib/utils.ts index fe245b4..353b985 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -2,7 +2,7 @@ import type { SimplePullRequest } from "@octokit/webhooks-types"; export function hasScheduleCommand(text: string | null): boolean { if (!text) return false; - return /(^|\n)\/schedule /.test(text); + return /(^|\n)\/schedule/.test(text); } export function isFork(pullRequest: SimplePullRequest): boolean { diff --git a/test/mocks/github.ts b/test/mocks/github.ts index ca5bff6..3a63762 100644 --- a/test/mocks/github.ts +++ b/test/mocks/github.ts @@ -109,6 +109,19 @@ const pullRequests = [ }, labels: [], }, + { + number: 7, + html_url: githubPullRequestUrl(7), + state: "open", + body: "Simple body not schedule date\n/schedule", + head: { + sha: "abc123success", + repo: { + fork: false, + }, + }, + labels: [], + }, ]; const pullRequestComments = pullRequests.map((pullRequest) => { let body = "";