GitHub-Powered Changelog Scripts

My current project does periodic releases, we build a few things, then we work on getting a bunch of user feedback and changing/fixing things before we actually release. This means we need to be organised with tags and branches. We’re using GitHub for collaboration, including our issue trackers, commits which contribute to an issue have the issue number in the commit message, and when a branch merges in to the main line, we use the “fixes #42” notation to simultaneously close off the issue that it relates to.

This has been working pretty well, and today I got the question “what’s new since I last saw this project?” – so I created a changelog. It’s rather rough-and-ready but I had fun so I thought I’d share.

Git the Log

First I grabbed all the log messages that had the word “fixes” in them, just by running a command-line one-liner:

git log last-release...HEAD --oneline | grep fixes > log.txt

We use tags when we release so instead of last-release, I used a tag name, but you could equally use a revision number. I grabbed the output and threw it into a text file, then parsed it in more detail so that I could get the issue information from GitHub using their API. For this I used PHP, because it’s great for talking to APIs.

Here’s the script that I used to parse out the actual issue numbers I wanted:

$filename = "log.txt";
$entries = file($filename);

foreach($entries as $entry) {
    $matches = array();
    preg_match("/fixes #([0-9]+)/", $entry, $matches);

    if(isset($matches[1])) {
        $id = $matches[1];

This just takes the file and reads it into an array, then does a little regex on each array to grab issue numbers from the commit messages (it’s probably easy to do this in the previous step, but my PHP is still better than my commandline-fu).

Get Details From Github

Once I have that issue ID, I can just grab the issue details from Github – in this case I simply went for the issue title and its URL, to provide a simple changelog to the team.

Before you begin, you will need an access token, get a GitHub “personal access token” from https://github.com/settings/applications. In my script, this is stored in $access_token. I’m using the PHP cURL extension in this example but you could easily use another library such as Guzzle which has a nicer interface.

        $url = 'https://api.github.com/repos/organisation/project/issues/' . $id;
        $ch = curl_init($url);
        $headers = array(
            "Authorization: token " . $access_token,
            "User-Agent: php-curl");

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $issue_json = curl_exec($ch);

        $issue = json_decode($issue_json, TRUE);

        echo $id . ": " . $issue['title'] . " (" . $issue['url'] . ")\n";

First you will need to edit the URL to put in your own organisation and project values. Then intialise the request and add a couple of headers and the CURLOPT_RETURNTRANSFER option to stop curl from echoing the output instead of returning it. Once we get the response, we json_decode() it and then the data is available in an array – which is echoed out in a very hi-tech manner!

I can imagine using variations of this script in other automated processes, perhaps to notify the team of which issues were closed yesterday, or automating a changelog when a new tag is pushed to the deployment server. However you use it, I thought I’d share in case it’s useful. If you have any additional tips or advice, leave me a comment and share in return :)

5 thoughts on “GitHub-Powered Changelog Scripts

  1. Pingback: PHPDeveloper.org: Lorna Mitchell: GitHub-Powered Changelog Scripts

  2. would you like to send this command in a PR to gush using gush with this command http://github.com/cordoval/gush ?
    you can see the other commands that await for your delight, and instructions here below on how to tackle the issue i have already created for you:

    “`
    gush i:take 168
    // here you do your changes and commit on your local branch automatically created for your delight
    gush p:create
    “`

    That is it! See you on github!

  3. Pingback: Changelog-Skript auf GitHub-Basis - entwickler.de

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.