Merge Schedule Action
Tags: github, git, githubactions
Published: 2021-01-15 19:07:36

---
title: Merge Schedule Action
published: true
date: 2021-01-15 18:04:11 UTC
tags: github,git,githubactions
canonical_url: https://michaelheap.com/merge-schedule-action/
series: Action Spotlight
---

I’m trying to get ahead on the Action Spotlight series, and needed a way to write and stage a post in advance of it’s Friday publication date.

So, this week’s post covers an action that I needed automatically merge the pull request containing this blog post - `merge-schedule-action`.

## What does it do?

Merge Schedule allows you to merge pull requests at a scheduled time. The most common use case that I’ve seen for this so far is to merge content into a static site (like this one) at some point in the future.

Scheduled pull requests allows you to stage and approve content ahead of time and have it automatically merged (and deployed if you have a pipeline set up) automatically.

## How does it work?

There are two separate handlers in `merge-schedule-action`. The first handles a `pull_request` being `opened`, `edited` and `synchronize`-d, and the second is run when triggered on a `schedule`.

It’s important to note that [you cannot schedule merges from forks](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_pull_request.js#L24-L27).

### pull\_request

The `pull_request` handler is a really useful addition to the action. Whenever a PR is opened it [checks if the body contains a `/schedule` command](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_pull_request.js#L83-L85). If it does, a check is added using the `checks` API.

- If the date provided to `/schedule` is invalid, a `failure` check [is added to the PR](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_pull_request.js) to indicate that something is wrong, allowing the author to fix the date.
- If the [provided date is in the past](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_pull_request.js#L53-L67), a `failure` check is added to the PR (but will still be merged by the `schedule` action)
- Otherwise, [create an `in_progress` check](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_pull_request.js#L69-L79) which prevents the PR being merged (unless you’re an admin)

### schedule

This is where the bulk of the work happens. This method is triggered on a schedule that you set in your workflow. You can choose any schedule you like, but I’d recommend hourly (if you set specific publish times) or once per day (if you only set a publish date).

`merge-schedule-action` starts by [loading all open pull requests](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_schedule.js#L19-L25) for the repo the workflow is running under, filtering down to only PRs that [have a schedule command](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_schedule.js#L28) and [are not from a fork](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_schedule.js#L29).

Next, it [filters out any pull requests where the scheduled date is in the future](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_schedule.js#L47-L49).

Then finally, it loops though any remaining pull requests, [merging the PR](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_schedule.js#L58-L63) and [updating the `in_progress` check previously added](https://github.com/gr2m/merge-schedule-action/blob/ee997a26b85a0c86189ff1fc31d30f12c3729bc3/lib/handle_schedule.js#L75-L86) to be `completed`.

## Useful Links

- [GitHub Marketplace](https://github.com/marketplace/actions/merge-schedule)
- [GitHub Repo](https://github.com/gr2m/merge-schedule-action)
- [Example Usage](https://github.com/gr2m/merge-schedule-action#usage)