Building a GitHub action that checks your checked boxes
Is the title confusing? Check. Checks, checking checks? For sure.
To improve the quality of pull requests and ensure proper quality assurance, we’ve begun incorporating pull request templates that outline standard checks. These checks cover aspects that cannot be reviewed by our continuous integration pipeline, such as documentation updates, spelling errors, CSS checks, and even whether you’ve reviewed your own PR (which you should).
Like any good developer, many people are inclined to ignore these check boxes, so instead of pointing this out in PRs, I decided to build a “todo” checker. Additionally, the experience of running the checker locally has been lackluster, so I am here to show you how to build it.
Getting Started
First, let’s start with the directory structure below 👇
.github/
├── actions
│ └── todo-checker
│ ├── Dockerfile
│ ├── Gemfile
│ ├── Gemfile.lock
│ ├── run.rb
│ └── run.sh
├── pull_request_template.md
└── workflows
└── todo.yml
Then, the todo.yml
workflow appears as follows:
name: Todos
on: pull_request_target
jobs:
verify:
name: Todos
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ./.github/actions/todo-checker
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
And finally, the Ruby file will look like this:
require "octokit"
require "json"
puts current_repo = ENV["GITHUB_REPOSITORY"]
puts root = ENV["GITHUB_WORKSPACE"]
puts "---> #{ENV["GITHUB_EVENT_NAME"]} #{ENV["GITHUB_EVENT_PATH"]}"
context = JSON.parse(File.read(ENV["GITHUB_EVENT_PATH"]), object_class: OpenStruct)
client = Octokit::Client.new(access_token: ENV["GITHUB_TOKEN"])
# Check for todos
puts "---> Checking todos for repo '#{current_repo}' #{context.number}"
pr = client.pull(current_repo, context.number.to_i)
def has_open_todos?(pull_request)
if pull_request.body.match(/\- \[ \]/)
puts "You have open todos that need fixing!"
exit 1
end
end
has_open_todos?(pr)
Now, prior to merging your pull request, this action will check for open todos to make sure that all checks have been completed.