Skip to content

Commit

Permalink
feat: use node v16 and support custom check name (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruisaraiva19 authored Jun 8, 2022
1 parent 4e5959d commit f9a8fa8
Show file tree
Hide file tree
Showing 9 changed files with 9,354 additions and 4,366 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/merge-schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ on:
- synchronize
schedule:
# https://crontab.guru/every-hour
- cron: 0 * * * *
- cron: '0 * * * *'
# manual trigger for testing
workflow_dispatch: {}

jobs:
merge_schedule:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- run: "npm ci"
- run: "npm run build"
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'npm'
- run: npm ci
- run: npm run build
- uses: ./
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7 changes: 5 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ jobs:
name: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'npm'
- run: npm ci
- run: npm run build
- run: rm .gitignore # dist/ folder is ignored by default
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.15.1
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Create `.github/workflows/merge-schedule.yml`

```yml
name: Merge Schedule

on:
pull_request:
types:
Expand All @@ -16,38 +17,40 @@ on:
- synchronize
schedule:
# https://crontab.guru/every-hour
- cron: 0 * * * *
- cron: '0 * * * *'

jobs:
merge_schedule:
runs-on: ubuntu-latest
steps:
- uses: gr2m/merge-schedule-action@v1
- uses: gr2m/merge-schedule-action@v2
with:
# Merge method to use. Possible values are merge, squash or
# rebase. Default is merge.
merge_method: squash
# Time zone to use. Default is UTC.
time_zone: "America/Los_Angeles"
# Time zone to use. Default is UTC.
time_zone: 'America/Los_Angeles'
# Name for the check. Default is `Merge Schedule`.
check_name: 'Scheduled Merge'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
In your pull requests, add a line to the end of the pull request description look looking like this
In your pull requests, add a line to the end of the pull request description looking like this
```
/schedule 2019-12-31
/schedule 2022-06-08
```

Or if you need a more precise, timezone-safe setting, you can use an ISO 8601 date string
Or if you need a more precise, timezone-safe setting, you can use an `ISO 8601` date string

```
/schedule 2019-12-31T00:00:00.000Z
/schedule 2022-06-08T09:00:00.000Z
```

Any string that works with the [`new Date()` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date) will work.

To control at which time of the day you want the pull request to be merged, I recommend to adapt the `- cron: ...` setting in the workflow file.
To control at which time of the day you want the pull request to be merged, I recommend adapting the `- cron: ...` setting in the workflow file.

The action sets a pending commit status if the pull request was recognized as being scheduled.

Expand Down
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ branding:
icon: "git-merge"
color: purple
runs:
using: "node12"
using: "node16"
main: "dist/index.js"
inputs:
merge_method:
Expand All @@ -18,3 +18,8 @@ inputs:
Time zone to use. Default is UTC.
required: false
default: UTC
check_name:
description: |-
Name for the check. Default is `Merge Schedule`.
required: false
default: 'Merge Schedule'
15 changes: 8 additions & 7 deletions lib/handle_pull_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async function handlePullRequest() {
const octokit = new Octokit();

const eventPayload = require(process.env.GITHUB_EVENT_PATH);
const checkName = process.env.INPUT_CHECK_NAME;

core.info(
`Handling pull request ${eventPayload.action} for ${eventPayload.pull_request.html_url}`
Expand Down Expand Up @@ -38,12 +39,12 @@ async function handlePullRequest() {
const { data } = await octokit.checks.create({
owner: eventPayload.repository.owner.login,
repo: eventPayload.repository.name,
name: "Merge Schedule",
name: checkName,
head_sha: eventPayload.pull_request.head.sha,
conclusion: "failure",
output: {
title: `"${datestring}" is not a valid date`,
summary: "TO BE DONE: add useful summary",
summary: "",
},
});
core.info(`Check run created: ${data.html_url}`);
Expand All @@ -54,12 +55,12 @@ async function handlePullRequest() {
const { data } = await octokit.checks.create({
owner: eventPayload.repository.owner.login,
repo: eventPayload.repository.name,
name: "Merge Schedule",
name: checkName,
head_sha: eventPayload.pull_request.head.sha,
conclusion: "failure",
output: {
title: `"${datestring}" is already in the past`,
summary: "TO BE DONE: add useful summary",
summary: "",
},
});
core.info(`Check run created: ${data.html_url}`);
Expand All @@ -69,12 +70,12 @@ async function handlePullRequest() {
const { data } = await octokit.checks.create({
owner: eventPayload.repository.owner.login,
repo: eventPayload.repository.name,
name: "Merge Schedule",
name: checkName,
head_sha: eventPayload.pull_request.head.sha,
status: "in_progress",
output: {
title: `Scheduled to be merged on ${datestring}`,
summary: "TO BE DONE: add useful summary",
summary: "",
},
});
core.info(`Check run created: ${data.html_url}`);
Expand All @@ -91,5 +92,5 @@ function getScheduleDateString(text) {
// https://stackoverflow.com/a/1353711/206879
function isValidDate(datestring) {
const date = new Date(datestring);
return date instanceof Date && !isNaN(date);
return date instanceof Date && !Number.isNaN(date);
}
45 changes: 26 additions & 19 deletions lib/handle_schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async function handleSchedule() {
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");

const mergeMethod = process.env.INPUT_MERGE_METHOD;
const checkName = process.env.INPUT_CHECK_NAME;

core.info(`Loading open pull request`);
const pullRequests = await octokit.paginate(
Expand Down Expand Up @@ -62,27 +63,33 @@ async function handleSchedule() {
});

// find check runs by the Merge schedule action
const checkRuns = await octokit.paginate(octokit.checks.listForRef, {
owner,
repo,
ref: pullRequest.ref,
});
const checkRuns = await octokit.paginate(
octokit.checks.listForRef,
{
owner,
repo,
ref: pullRequest.ref,
},
(response) => {
return response.data.filter((check) => check.name === checkName);
}
);

const checkRun = checkRuns.pop();
if (!checkRun) continue;

await octokit.checks.update({
check_run_id: checkRun.id,
owner,
repo,
name: "Merge Schedule",
head_sha: pullRequest.headSha,
conclusion: "success",
output: {
title: `Scheduled on ${pullRequest.scheduledDate}`,
summary: "Merged successfully",
},
});
if (checkRun) {
await octokit.checks.update({
check_run_id: checkRun.id,
owner,
repo,
name: checkName,
head_sha: pullRequest.headSha,
conclusion: "success",
output: {
title: `Scheduled on ${pullRequest.scheduledDate}`,
summary: "Merged successfully",
},
});
}

core.info(`${pullRequest.html_url} merged`);
}
Expand Down
Loading

0 comments on commit f9a8fa8

Please sign in to comment.