Tuesday, July 17, 2012

Create an issue in Redmine using the REST API via Python (PyActiveResource)

I have not found any solid documentation of the Redmine API accessed via Python's PyActiveResource, so I will show how I did it.

First you have to install pyactiveresource

pip install pyactiveresource


easy_install pyactiveresource

In the file rest-test.py: (pastbin link: http://pastebin.com/H9mEsCtP)

from pyactiveresource.activeresource import ActiveResource

# site settings. Activate API access under Administration - Authentication - Enable REST web service
# Get your API key on the "My account" page, in the right frame (API access key)
api_key = ''YourApiKeyHere"
api_site = "http://%s@your.url.here" % api_key

# connect to access the projects in redmine
class Project(ActiveResource):
    _site = api_site

# connect to access the issues in redmine
class Issue(ActiveResource):
    _site = api_site

#### general info that could be useful
# attributes of an issue
#{'status': None, 'due_date': None, 'description': u'Sorry if this goes out as a mail to everyone. Please ignore it :)\n\nMvh\nMartin Dahl\xf6', 'project': None, 'author': None, 'id': '42', 'priority': None, 'created_on': '2012-07-17T10:09:11+02:00', 'tracker': None, 'estimated_hours': None, 'updated_on': '2012-07-17T10:09:11+02:00', 'start_date': '2012-07-17', 'done_ratio': '0', 'subject': 'Test to see the api syntax'}

#### general info that could be useful
# dir of an issue
#['__class__', '__cmp__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_build_list', '_build_object', '_class_delete', '_class_get', '_class_post', '_class_put', '_collection_path', '_connection', '_custom_method_collection_url', '_custom_method_element_url', '_custom_method_new_element_url', '_element_path', '_find_class_for', '_find_every', '_find_one', '_find_single', '_format', '_headers', '_id_from_response', '_initialized', '_instance_delete', '_instance_get', '_instance_post', '_instance_put', '_password', '_plural', '_prefix', '_prefix_options', '_prefix_parameters', '_query_string', '_singular', '_site', '_split_options', '_timeout', '_update', '_user', 'attributes', 'create', 'delete', 'destroy', 'errors', 'exists', 'find', 'find_first', 'find_one', 'get', 'is_valid', 'klass', 'post', 'put', 'save', 'to_dict', 'to_xml']

#### general info that could be useful
### list all issues' attributes
# A weird thing is that many of the important attributes of the issues are reported as None..

# get a list of all the issues
#issues = Issue.find()

# go through the list of issues
#for i, v in enumerate(issues):

    # print number in the loop and issue id, just to keep track
#    print 'issue [',i,']',' =', v

    # print each attribute of the issue
#    for item in issues[i].attributes:
#       print item, " =  ", issues[i].attributes[item]

    # separator
#    print '======================'

#### general info that could be useful
### list all project's attributes

# get a list of all the projects
#projects = Project.find()

# go through the list of projects
#for i, v in enumerate(projects):

    # print number in the loop and project id, just to keep track
#    print 'project [',i,']',' =', v

    # print each attribute of the project
#    for item in projects[i].attributes:
#       print item, " =  ", projects[i].attributes[item]

    # separator
#    print '======================'

# properties for the new issue
subject = "Example"
description = "This is a question body.\n\nYepp, sure is.."
pid = 14 # get this by running the code above (list all project's attributes)
tid = 3 # get this by looking in the overview of the project in question, in the ordinary web interface. Hover the mouse over the links to the different issue trackers you have on that project.  Ex. http://whatever.url/projects/name.of.your.project/issues?set_filter=1&tracker_id=3 where tracker_id is what you are after.

# create the issue and save it
newIssue = Issue({'subject': subject, 'project_id': pid, 'tracker_id': tid, 'description': description})

# done, it should have showed up in your redmine installation now.


  1. Hi

    Thanks for the tutorial.

    How do I get the list of available priorities?

    I can't figure out how to handle enumerations.

    1. Hmm, I no idea I'm afraid. I never needed to change the priority of an issue :/