Deborah Digges A Technical blog

PR Bot

Being a collaborator for the Open Hatch Community, I often request new contributors to send in an email to permit us to use their work; it involves commenting on every pull request on the project submitted by a new contributor.

There is no time for cut-and-dried monotony. There is time for work. And time for love. That leaves no other time. - Coco Chanel

Why not automate it? This led me to the Github API, in particular the Events API, which provides a feed of the interesting things happening on a repository. The Event of interest to me was the Pull Request Event, that is triggered on every state change to a pull request on Github.

Github allows subscription to events through WebHooks. A webhook allows one to configure a URL to which a POST request will be sent, if and when the event of interest is triggered. The POST body will have data relevant to the event, which for the Pull Request Event includes: the action—what state change occurred on the Pull Request Object—the Pull Request ID, and the Pull Request Object itself.

The next thing to do was to figure out how to comment on a Pull Request using the Github API. It seemed simple enough. However, it turned out that the dubious Pull Request API for comments is meant for commenting on diffs only and not on the pull request as a whole. The Issues API for comments is what is to be used for commenting on Pull Requests as well—counterintuitive as that may be.

The API for creating a comment has the signature: /repos/:owner/:repo/issues/:number/comments. I was confused as to how to get an issue number corresponding to a pull request, as there is usually no one-to-one correspondence between them. The final piece of the puzzle was the _links field in the POST body that contains the API endpoint for creating comments on that pull request.

XKCD Automation

It remains to be seen of how much use this tool will be.

Check out the code here ; see pr-bot in action by submitting a Pull Request! :)