Thursday, August 9th, 2012

Working on a BitBucket API

A recent project I have been working on lead me to using BitBucket for source code management. I mainly used Subversion in the past and know how to use that over a distributed system. BitBucket has a very nice REST API, and I thought of how to take advantage of this API to enable issues to be created. Beyond that, I wanted issues to be created automatically when an issue occurs in one of my Django applications.

I have already begun developing the API and logging.Handler sub-class to work with BitBucket. The source code is public and available on my BitBucket under the project Python BitBucket API. Have a look and let me know what you think of it so far. I am trying to create a Pythonic API, whereas each BitBucket object is a Python object, which can be saved back and edited via it's properties. A concept similar to Django models. Here is an example of how to create a new issue using this:

from api import API

api = API("kveroneau", "**password**")
issue = api.new_issue("kveroneau", "python-bitbucket")

issue.title = "Testing Python API"
issue.content = "Hello World!\nLine 2"
json =

Every property available on bitbucket for an issue is available as an attribute on this Issue object, so you can set everything from the Milestone to the Priority. Here is how you can read in all the issues on your project:

from api import API

api = API("kveroneau", "**password**")
issues = api.get_issues("kveroneau", "python-bitbucket")

for issue in issues:
  print "Issue title: %s" % issue.title
  print "Issue priority: %s" % issue.priority
  print "Issue content:\n%s\n\n" % issue.content

See, very simple, straightforward, and most of all, easy to understand. This is how all Pythonic APIs should be. Leave any comments below on what you think of this API, and please download a copy of and take it for a spin.

Python Powered | © 2012-2014 Kevin Veroneau