Automatically adding release notes to TeamCity builds from Github pull requests

Jason Schloer
3 min readNov 11, 2015

--

As part of our effort to move to continuous internal delivery, we’re creating a build for every pull request and pushing it to Fabric beta. I had simply been updating the version number by appending the pull request id, but this meant that in order for QA to figure out what was intended to be in the build they had to bring up github and review the pull request comments and the commits within that request. I wanted an easier way, so I worked out how to grab the comments from the commits in a pull request.

First, take your working TeamCity build and add a new step. I named mine “Build release notes” and made it a command line runner. I set it up as a custom script in the run type.

#!/bin/bash
pullrequest=`echo %teamcity.build.vcs.branch.IOSPullRequests% | sed “s/refs\/pull\/\(.*\)\/head/\1/” | sed “s/refs\/heads\/develop/develop/”`

The beginning of the script just grabs the pull request number. Our default build for this configuration is “devlop” so we make sure to account for that as well.

json=`curl -s -X GET https://api.github.com/repos/<account>/<repo>/pulls/$pullrequest/commits?access_token=111111111111111111111111111111`

Now things get a bit more interesting. Here we’re grabbing a json array of the commit records in our pull request from github (more info here) and saving it into the “json” variable. Note that you’ll have to setup an access token or use some other authentication method to get this to work properly. Check the github api docs for more info on that.

We’re going to want to grab all of the “message” strings out of the records in this array and use those to build our release notes. The easiest way I found to deal with json in a bash script was using this gist. I made some tweaks and came up with the following:

function jsonval {
temp=`echo $json | sed ‘s/\\\\\//\//g’ | sed ‘s/[{}]//g’ | awk -v k=”text” ‘{n=split($0,a,”,”); for (i=1; i<=n; i++) print a[i]}’ | sed ‘s/\”\:\”/\|/g’| sed ‘s/\\\\n/-/g’| sed ‘s/[\,]/ /g’| sed ‘s/\”//g’ | grep -w $prop`
echo ${temp##*|}
}

It’s fairly simplistic in capability, but it suits our purpose and is reusable in case we want to use any other fields from the pull request json.

We now set our $prop to the json property we want and call the jsonval function.

prop=’message’
message=`jsonval`

Note that those are single quotes around message and back-ticks around jsonval.

At this point $message contains our commit messages, which hopefully describe what changed in this pull request. We encourage our devs to name their branches after the youTrack issue number, as well as reference it in commit logs. Between these two, we usually have one or more mentions of the issue number, which makes it easy to figure out what needs to be tested.

One last thing to do is to remove the “message: ” prefix from each line and save the release notes somewhere. We use release.txt.

echo ‘Notes from commits in pull request:’ > release.txt
echo $message | sed $’s/message: /\\\n/g’ >> release.txt

That’s it. Now the release.txt contains the text from all of our commits. For our setup, we make sure to use the release notes when uploading builds to Fabric beta. Check out my other posts on how to setup automatic uploads for iOS and Android.

--

--