Subversion / Basecamp integration using PHP

October 28th, 2007

This article explains how to automatically post a message to Basecamp detailing any changes in a Subversion code repository.

basecampsvnsmall.png

Motivation

More and more of the projects (mostly website development) that I am involved with involve a number of developers. We use Basecamp to keep a tab on our correspondence, deadlines, bugs etc. This has proved pretty successful, and with the intention of its continued use, I set out to make it more useful for our particular (code-based) needs.

Another tool used on these projects is Subversion (SVN), to keep a track of changes in the codebase. It seemed like a good idea to tie SVN in with Basecamp, keeping the people on the project informed in the changes in the code.

Luckily Basecamp has an open API, making this sort of integration very easy.

Assumptions

This article assumes working knowledge of SVN i.e. how to create and access repositories etc. You should also have full access to the server on which the repository is hosted i.e. an ability to add/remove files, change folder permissions etc. The installation below is based on a linux system and basic linux console commands are assumed. A tiny amount of PHP is required.

Basics

You must have the API support turned on for your Basecamp account, this can be done in the settings page.

You should have a working SVN repository already installed. The examples assume the repository is installed at /path/to/repository/.

Installation

The script will be run after someone commits a change to the repository. A default installation of Subversion has a series of scripts that are executed at various stages of the commit process. These “hooks” can be found in the folder /path/to/repository/hooks/. The one we are interested in is the “post-commit” hook. We will instruct SVN to run our PHP script in this file.

The files in the hooks/ folder are just skeleton scripts. You will notice that they have a .tmpl extension. This must be removed before SVN processes them:

$ cp post-commit.tmpl post-commit

The script must also have execution rights:

$ chmod a+x post-commit

Now it is a matter of installing the custom scripts.

My code is based on a PHP wrapper written by Aaron Quint. This bit of code requires two PHP modules to work — HTTP_Request and XML_Serializer. These may be included in your installation of PHP already. If not these can be installed via PEAR (may need root access):

$ pear install HTTP_Request XML_Serializer-0.18.0

Now download my code. It contains all the files you need. This can be downloaded from the console using wget.

Unpack this into the hooks/ folder:

$ tar -xzvf BasecampSVN.tar.gz

You now need to configure notify.php with your particular Basecamp details. These are stored in variables in notify.php:

$bc_user
$bc_password
$bc_base_url
$bc_project_id
$bc_category_id
$bc_company_id

Their explanations are pretty self-explanatory, but there is additional information in the script.

Now add the following lines to the post-commit file to tell SVN to run the notify script:

REPOS="$1"
REV="$2"
php $REPOS/hooks/notify.php $REPOS $REV

Testing

Give it a try now, change some files in the repository and commit them. All going well, you should see a message posted on Basecamp, detailing the changes to the repository.

If this isn’t the case, you can run the script manually and look for any error messages that may be returned:

$ php notify.php /path/to/repository revision_number

Other features

Just like any other Basecamp message, you can tell the script to send email notifications to people in a certain company and make the post private. These options can be set in notify.php.

Conclusion

This script has certainly proved successful on the projects I have been working on. It provides the practical advantange of letting the developers see where and when the code has been changed. It also lets the client of the project see that the project is making progress, something that can be difficult to convince them of at times ;)

Special thanks to Cazinc Web Design for supporting the development of this open source project.

Resources

BasecampSVN - v0.1.2

Your Thoughts

Any problems, bugs, comments or suggestions are always welcome. Just leave a post below.

— Martin McNickle