Comment a Github pull request from Travis CI

Hubot
Travis CI is a continuous integration platform which means it is used to run various processes after code changes like unit tests, code style checks, build releases... Most of the time, you are only interested in the end result (ie did unit/code style/whatever tests pass?) and in that case Github displays that nicely in the page (and even in the page icon lately). However, from time to time, it might be useful to get a little more info for instance to see an overview of the code coverage of the tests or where you don't respect the expected code styles without having to load the full build log which is usually quite long, hard to read and slow to load.

In my own case, for each pull request, Travis CI builds and pushes this website so that I can see my changes in a kind of staging environment, so in addition to the build result, I would like to have a link to click on to see my changes. Doing that is not super complicated but requires some steps to get a working solution.

Trigger a script at a given build step

First, you need to change your .travis.yml to trigger a script at a Travis CI build step suitable for your need. In my case, I do that in the after_success step because I only want to comment if the build was successful, so in my .travis.yml file I have something like:

after_success:
    - ./path/to/script.sh

Detect a pull request build

Then in this script, I have to detect whether Travis CI is building a pull request or not. There are various ways of doing that, but the Travis CI documentation advices to use the TRAVIS_PULL_REQUEST environment variable which directly gives this information. So the script will contain a condition like:

if [ "$TRAVIS_PULL_REQUEST" != "false" ] ; then
   # hey that's a pull request
fi

Post a comment with the Github API

Remains to actually post the comment. For that, I use the Github REST API v3 because when I set up this system, it was the only available solution. To use the API, you'll need to create a Personal access token as described in the user a documentation. The token is associated with one or several scopes. The scopes define what you can do with the token. To post a comment, the token needs to have the scope repo/public_repo, at least this is what I learned after various tries :)

Once you get a token, you need to make it available to the build process. For such a sensible information (remember a token is like a password), Travis CI offers two options:

  1. define an encrypted environment variable in .travis.yml
  2. define an environment variable in Repository settings

In both cases, the environment variable is made available in the build process in a secured way (at least it's not displayed in the build log). I use the second option and the environment variable is called GITHUB_TOKEN.

With that in place, we can now call the Github API. If you look at the API documentation for pull requests, you won't find any resource to post a comment, but there's one to comment an issue which happens to also work for a pull request.

For instance the following curl based command line will create a Hello world comment on a pull request build in Travis CI:

curl -H "Authorization: token ${GITHUB_TOKEN}" -X POST \
-d "{\"body\": \"Hello world\"}" \
"https://api.github.com/repos/${TRAVIS_REPO_SLUG}/issues/${TRAVIS_PULL_REQUEST}/comments"

Of course, the content of the comment can be customized as long as the request body is a valid JSON document.

And that's it, as an example, you can look at the pull request I created to write this blog post, Travis CI kindly commented for me the URL of the staging website to view it online thanks to those two lines in my deploy.sh script.