简体   繁体   English

用Python和Rest查询Jira

[英]Querying Jira with Python and Rest

I want to pull a list of users in the jira-users group. 我想在jira-users组中提取用户列表。 as i understand it, it can be done with Python using restkit. 据我所知,它可以使用restkit使用Python完成。

Does anyone have any examples or links that give an example of this? 有没有人有任何示例或链接给出这样的例子?

thanks. 谢谢。

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. Jira有一个用于外部查询的REST API,它使用HTTP协议进行请求和响应,响应内容形成为JSON。 So you can use python's urllib and json packages to run request and then parse results. 因此,您可以使用python的urllibjson包来运行请求,然后解析结果。

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 这是Atlassian的Jira REST API文档: http//docs.atlassian.com/jira/REST/latest/ ,例如检查用户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. 考虑到您应该在发送请求之前进行身份验证,您可以在文档中找到必要的信息。

If somebody still need a solution, you can install JIRA rest api lib https://pypi.python.org/pypi/jira/ . 如果有人仍然需要解决方案,您可以安装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
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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