I am heavy user of Renovate App that keeps all my repos up-to-date. One thing that is really nice and useful is a checkbox in the Renovate master GitHub issue that I can click to re-run the dependency check.
If I click on that checkbox, the Renovate app runs and checks the dependencies. How does it do it? Can I use the same approach to re-run the tests when changing the tests I want to run via Pull Request? Turns out, it is pretty simple to set up using GitHub Actions.
🎁 You can find the example project in the repo bahmutov/todomvc-tests-circleci. Specifically, see the GitHub Workflow file .github/workflows/pr.yml and the file .github/PULL_REQUEST_TEMPLATE.md.
The pull request template
We can add a checkbox to the pull request file like this
To re-run the tests, pick the tags above then click the checkbox below
By default the checkbox is unfilled. The user can fill it by editing the pull request text or by clicking on the checkbox in the GitHub UI (assuming the user has the edit permission). Let's open a pull request and click on the checkbox.
We need to detect the click and run the tests.
The easiest way to detect the change in the checkbox is to run a GitHub workflow on pull request edit. You can run a workflow on different pull request events: opened, synchronized, closed, edited. We only are interested in the "edited" events.
To detect if the user clicked the previously empty checkbox, we need to inspect the
github.event object provided to the GitHub action.
- name: Dump GitHub event
The dumped JSON object shows the change and the current body text of the pull request.
Thus we can compare the
event.changes.body.from and the
event.pull_request.body text to see if the checkbox flipped from empty to filled. I have written this check in the bahmutov/should-run-github-action and created a reusable GitHub action everyone can use to determine if the user has filled the checkbox. In our project we use by providing an
id and passing the GH event as an environment variable
- name: Check the PR
After that we can look at the
check-pr step and the output variable it sets called
shouldRun to decide if we need to run the tests.
- name: Check the PR
This is how we can run the tests only when the user filled the checkbox. Of course, if the box is filled already and we click it the first time, it is emptied. The workflow runs, but immediately skips the test step. Then we click the checkbox and the tests run.