Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: schedule without date string #71

Merged
merged 1 commit into from
Jun 22, 2022
Merged
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
26 changes: 26 additions & 0 deletions lib/handle-pull-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
<!-- Merge Schedule Pull Request Comment -->"
`);
});
});
30 changes: 21 additions & 9 deletions lib/handle-pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,33 @@ export default async function handlePullRequest(): Promise<void> {
}

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"
);
}
Expand Down
30 changes: 24 additions & 6 deletions lib/handle-schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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
Expand All @@ -64,6 +68,11 @@ describe("handleSchedule", () => {
In order to let the automerge-automation try again, the label \\"automerge-fail\\" should be removed.
<!-- Merge Schedule Pull Request Comment Fail -->"
`);
expect(createComment.mock.calls[2][2]).toMatchInlineSnapshot(`
":white_check_mark: **Merge Schedule**
Scheduled on next cron expression successfully merged
<!-- Merge Schedule Pull Request Comment -->"
`);
expect(updateComment.mock.calls).toHaveLength(2);
expect(updateComment.mock.calls[0][2]).toMatchInlineSnapshot(`
":white_check_mark: **Merge Schedule**
Expand All @@ -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`,
Expand All @@ -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
Expand All @@ -118,6 +131,11 @@ describe("handleSchedule", () => {
In order to let the automerge-automation try again, the label \\"automerge-fail\\" should be removed.
<!-- Merge Schedule Pull Request Comment Fail -->"
`);
expect(createComment.mock.calls[2][2]).toMatchInlineSnapshot(`
":white_check_mark: **Merge Schedule**
Scheduled on next cron expression successfully merged
<!-- Merge Schedule Pull Request Comment -->"
`);
expect(updateComment.mock.calls).toHaveLength(1);
expect(updateComment.mock.calls[0][2]).toMatchInlineSnapshot(`
":x: **Merge Schedule**
Expand Down
20 changes: 15 additions & 5 deletions lib/handle-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export default async function handleSchedule(): Promise<void> {
}

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`);
Expand Down Expand Up @@ -140,10 +142,18 @@ export default async function handleSchedule(): Promise<void> {
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(
Expand Down
6 changes: 3 additions & 3 deletions lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions test/mocks/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down