Grab the latest version number from a GitHub repository

I am currently hosting several repositories at GitHub. For each, I have also created a GitHub gh-pages branch, so that I have a public site for the project (the gh-pages branch), as well as a project site (the main repository page).

The goal of the public site is to present the repository and make it easy to download the latest release, while the goal of the project web site is to provide people who have already begun using the repository with information, code etc.

One example where I do this is at the public Cloney web site:

As you can see, each web site features a Download button. Before tonight, none featured a version number. So, my thought was “how do I grab the latest version number from the GitHub repository”. The answer is really simple – use the GitHub API.

First – publish new versions as tags

For the example in this post to work, each version must be pushed as a tag to the GitHub repository. This is really easy to do.

Let’s say that you have a new version (let’s say..hmmmm….2.1.0.5) of your great project and the master branch has been pushed to your GitHub repo. Now, just type the following (provided that you call the GitHub remote origin):

   git tag 2.1.0.5
   git push origin 2.1.0.5

This will create a new tag with the version number and push it to the GitHub repository.

Also worth mentioning is that this method requires that each new tag name have a string value that is greater than one preceding it. If you name one tag “release 0.1.0.0” and another “2.1.5.0”, the first will be returned…and you do not want that, do you?

Then – use the GitHub API to grab all tags

The GitHub API is really slick, and let’s you do most anything possible. You can find all the info you need at http://develop.github.com/p/repo.html. However, instead of jQuerying the API directly, I decided to try fitzgen’s JavaScript github-api library.

To grab all tags for a certain repository, you just have write the following:

   var repo = new gh.repo("danielsaidi", "Facadebook");
   repo.tags(function(result){ alert(JSON.stringify(result)); });

Wow, that was easy! Now, how do I grab the latest version number from the result?

Finally – add some candy ontop

Since I will use this solution for all my GitHub repo web sites, I decided to package my custom script according to the rest of the JavaScript library.

I therefore created another async method for the gh.repo prototype, as such:

   gh.repo.prototype.getLatestRelease = function(callback) {
      this.tags(function(result) {
         var latest = "";
         for (var prop in result.tags) {
            if (prop > latest) {
               latest = prop;
            }
         }
         callback(latest);
      });
   }

On each web site, I have a span with the id “version”.

At the end of the github.js file, I finally added the following snippet:

   $(document).ready(function() {
      var repo = new gh.repo("danielsaidi", "Facadebook");
      var tags = repo.getLatestRelease(function(result){ $("#version").html(result); });
   });

That is is! When the page loads, I load all available repository tags, iterate over them and grab the “highest” tag name (version number) available.

The result is rather nice:

Version number is now displayed within the download button

Hope this helps!

Advertisements