Huskeraider
Huskeraider

Reputation: 89

Querying Jira with Python and Rest

I want to pull a list of users in the jira-users group. as i understand it, it can be done with Python using restkit.

Does anyone have any examples or links that give an example of this?

thanks.

Upvotes: 4

Views: 33388

Answers (3)

Springhills
Springhills

Reputation: 386

import urllib2, base64
import requests
import ssl
import json
import os
from pprint import pprint
import getpass

UserName = raw_input("Ener UserName: ")
pswd = getpass.getpass('Password:')

# Total number of users or licenses used in JIRA. REST api of jira can take values of 50 incremental
ListStartAt = [0,50,100,150,200,250,300]
counter = 0
for i in ListStartAt:
    request = urllib2.Request("https://jiraserver.com/rest/api/2/group/member?groupname=GROUPNAME&startAt=%s" %i)

    base64string = base64.encodestring('%s:%s' % (UserName, pswd)).replace('\n', '')
    request.add_header("Authorization", "Basic %s" % base64string) 
    gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    result = urllib2.urlopen(request, context=gcontext)

    JsonGroupdata = result.read()
    jsonToPython = json.loads(JsonGroupdata)

    try:
        for i in range (0,50):
            print jsonToPython["values"][i]["key"]
            counter = counter+1
    except Exception as e:
        pass
print counter

Upvotes: 0

Lexx Tesla
Lexx Tesla

Reputation: 151

If somebody still need a solution, you can install JIRA rest api lib https://pypi.python.org/pypi/jira/. Just a simple example for your question:

from jira.client import JIRA

jira_server = "http://yourjiraserver.com"
jira_user = "login"
jira_password = "pass"

jira_server = {'server': jira_server}
jira = JIRA(options=jira_server, basic_auth=(jira_user, jira_password))

group = jira.group_members("jira-users")
for users in group:
    print users

Upvotes: 5

MostafaR
MostafaR

Reputation: 3695

Jira has a REST API for external queries, it's using HTTP protocol for request and responses and the response content is formed as JSON. So you can use python's urllib and json packages to run request and then parse results.

This is Atlassian's document for Jira REST API: http://docs.atlassian.com/jira/REST/latest/ and for example check the users API: http://docs.atlassian.com/jira/REST/latest/#id120322

Consider that you should do authentication before send your request, you can find necessary information in the document.

Upvotes: 4

Related Questions