Wednesday, November 26, 2014

Using python-redmine to communicate with Redmine via Rest API

I'm using Redmine, the opensourced Project Management system, as a support ticket system. The way it's working right now is:
  1. whenever someone needs help, she will send an email to helpme@mycompany.com.
  2. Then there is a ruby task (rake) runs every minute to pull emails from that address (via IMAP) and create a ticket (issue) in the helpdesk project in my Redmine server.
But the thing is the Redmine's rake task can not create issue with category (Department A, Department B,...) based on the issue's content.  We can only specify the fixed category beforehand in the rake command:


  rake redmine:email:receive_imap RAILS_ENV="production" \\
    host=imap.foo.bar username=redmine@somenet.foo password=xxx ssl=1 \\
    project=foo \\
    tracker=bug \\
    allow_override=tracker,priority \\
    category='Department A'

So, I looked around and found that Redmine has REST API. It's even greater with python-redmine, a python library to help me communicate with Redmine via the REST API:

Github repo: https://github.com/maxtepkeev/python-redmine

With the python-redmine library, I can add logic to the issue creating process. Here is how to get started with python-redmine:

1. Enable REST API in Redmine:

Go to Administration (in the top left of the screen) >> Settings >> Authentication tab >> Check the Enable REST web service checkbox


2. Get my API Key:

Go to My account (in the top right of the screen)>> Look in the right column, API access key section


Note: this user need to be a Redmine administrator to create ticket on behalf of other users.

3. Trying python-redmine:

$ pip install python-redmine

>>> from redmine import Redmine
>>> redmine_url = '<http:/myredmine.com>'
>>> myapi_key = '<myapikey>'
>>> ticket_author = '<some username>'
>>> helpdesk_project_id = '<myhelpdesk id>'
>>> service = Redmine(redmine_url, key=myapi_key, impersonate=ticket_author)
>>> newissue = service.issue.create(project_id=helpdesk_project_id, subject='This ticket is created using python redmine', description='This is genius!!!', status_id=1, category_id=1)


Now I can create a Django app with a form for people to submit helpdesk tickets in whatever category they want.

For more detail about how to using python-redmine, please read its official documentations: http://python-redmine.readthedocs.org/resources/issue.html

Pretty neat!