简体   繁体   中英

Perform JIRA transition via Python REST API

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM