4
votes

How to mark a JIRA issue as resolved or closed via its REST API (version 2) using Python?

I found the documentation at http://docs.atlassian.com/jira/REST/latest/#id199544 but I had various errors including:

  • HTTP Error 415: Unsupported Media Type
  • HTTP Error 400
1

1 Answers

6
votes

After search for a long time I found the solution, which I'm posting here for anyone else who's interested in making a Git/Gerrit hook to do such thing like me:

First open http://example.com/rest/api/2/issue/<ISSUE>/transitions?expand=transitions.fields in your browser for your website and issue number to find the transition ID.

Supposing it's 1000:

import urllib
import urllib2
import base64
import json

key = 'JIRA-123'
comment = "It's done!"
username = 'username'
password = 'password'

# See http://docs.atlassian.com/jira/REST/latest/#id199544
url = 'http://example.com/rest/api/2/issue/%s/transitions' % key
auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
data = json.dumps({
    'transition': {
        'id': 1000    # Resolved (for my setup)
    },
    'update': {
        'comment': [
            {
                'add': {
                    'body': comment
                }
            }
        ]
    },
})
request = urllib2.Request(url, data, {
    'Authorization': 'Basic %s' % auth,
    'Content-Type': 'application/json',
})
print urllib2.urlopen(request).read()

You can completely omit the comment section if you don't wish to add a comment.