How to access JIRA in your Python program

I supposed that your Python environment has been installed

1. Installation of JIRA-Python package

     Pip install jira


2. Connect to JIRA

(1) Without Authentication

def connect_jira(jira_server):
    try:
        jira_options = {'server': jira_server}
        jira = JIRA(options=jira_options))
        return jira
    except Exception,e:
        return None
 

(2) Basic Authentication

def connect_jira(jira_server, jira_user, jira_password):
    try:
        jira_options = {'server': jira_server}
        jira = JIRA(options=jira_options, basic_auth=(jira_user, jira_password))
        return jira
    except Exception,e:
        return None

3. Create a JIRA story object

jira = connect_jira("https://jira.#########.com")
issue =jira.issue('JRA-1330')
print issue.Key
 
 

4. Get all stories for specific sprint

sprint = '####'
issues = jira.search_issues('sprint=%s and type=Story'  % sprint)
for iss in issues:
    print iss.key
 

5. Get all bugs which labels contain 'regression'

sprint = '####'
bugs = jira.search_issues('sprint=%s and type=Bug and labels in ("regression")'  % sprint)
for bug in bugs:
    print bug.fields.summary
 

6. Get all linked bugs of a story

issue = jira.issue('JRA-1330')
issuesLinked = jira.search_issues('issue in linkedIssues(%s) and type=bug' % issue.key)


7. Create an issue

issue_dict = {
    'project': {'key': 'RIO2016'},
    'summary': 'New issue created from jira-python',
    'description': 'just for test',
    'issuetype': {'name': 'Bug'},
    }
issue = jira.create_issue(fields=issue_dict)

8. Set fixVersion

def set_fixversion(issue_object, project_name, version_name):
    versions = jira.project_versions(jira.project(project_name))
    for version in versions:
        if(version.name == version_name):
            issue_object.update(fixVersions=[{"id" : version.id}])
            return True
    return False

issue = jira.issue('RIO2016-774')
set_fixversion(issue, 'RIO2016', '15-1 04/08/2015')

9. Add a new comment

comment = jira.add_comment('RIO2016-774', 'a new comment, just for test')

10. Get history of an issue

issue = jira.issue('RIO2016-504', expand='changelog')
    changelog = issue.changelog
    for history in changelog.histories:
        for item in history.items: 
            if item.field == 'status':
                print history.created
                print getattr(item,'fromString')
                print getattr(item,'toString')

11. Get json of an issue

import json

issue = jira.issue('RIO2016-504')
    print json.dumps(issue.raw)


You also can get more details about jira-python API in page: http://jira-python.readthedocs.org/en/latest/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lemon2050

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值