Python操作jira

举报
Python爱好者 发表于 2021/04/26 14:46:35 2021/04/26
【摘要】 Python 操作 jira
安装
pip install jira
登录
from jira import JIRA

#登录 可以使用cookie进行oauth登录
jira = JIRA(地址, basic_auth=(用户名, 密码))

# 获取此服务器的可变应用程序属性(需要支支系统管理员权限)
props = jira.application_properties()
查询
from collections import Counter

# 找到报告人为admin的所有问题
issues = jira.search_issues('assignee=admin')

#在报告人为管理员的报告中,问题最多的三个项目
top_three = Counter(
    [issue.fields.project.key for issue in issues]).most_common(3)

#获取当前用户及所有项目
print(jira.user(jira.current_user()))#当前用户
for i in jira.projects():
    print(i.name)#项目名

#获取指定的issue
myissue = ira.issue('EN-1');
summary = myissue.fields.summary;#主题
print(myissue, summary)

#获取issue为CMDB-15的issue
issue = jira.issue('CMDB-15')

#打印CMDB-15的issue的所有comment
            print(i.body)

#任务名称
print(issue.__dict__['key']) #任务名称

#问题状态(opened,fixed,progressing, 关闭(close))
print(issue.fields.status)

#任务类型(Bug,Task, Epic, Story)
print(issue.fields.issuetype)

#打印这个问题的名子
print(issue.fields.summary)

#打印这个问题的任务下的comment问题总数

#获取用户为zongyang.yu的issuce
messages = jira.search_issues('assignee=zongyang.yu') #是个iterable

#打印第0个issue的id
print(messages[0].id)

#打印第0个issue的key(这个就是issue的号,比如DEV-01)
print(messages[0].key)

#打印第0个的issue的项目名字
print(messages[0].fields.project.name

#打印第0个issue的任务状态(opened,fixed,progressing, 关闭(close))
print(messages[0].fields.status)

#打印第0个issue的任务类型(Bug,Task, Epic, Story)
print(messages[0].fields.issuetype)

#获取issue的name
print(messages[0].fields.summary)

#获取邮箱为zongyang.yu的issue
comments = [comment for comment in issue.fields.comment.comments if re.search(r'zongyang.yu', comment.author.emailAddress)]

#问题id
print(comments[0])

#问题内容
print(comments[0].__dict__['body'])

#问题作者
print(comments[0].__dict__['author'])

#问题描述
print(comments[0].fields.description)

#问题跟踪者
print(comments[0].fields.reporter)

#问题创建者
print(comments[0].fields.creator)

#添加comment
jira.add_comment(issue, 'Comment text')

#查看json格式的raw
print(comments.fields.project.raw)

#搜索
issues_in_proj = jira.search_issues('project=PROJ')
all_proj_issues_but_mine = jira.search_issues('project=PROJ and assignee != currentUser()')

# 按优先次序排列,我本周末要交的5大问题
oh_crap = jira.search_issues('assignee = currentUser() and due < endOfWeek() order by priority desc', maxResults=5)

# 我最近报告的3个问题的摘要
for issue in jira.search_issues('reporter = currentUser() order by created desc', maxResults=3):
    print('{}: {}'.format(issue.key, issue.fields.summary))
创建
#1
issueadd= {
    'project': {'id': '10316'},
    'issuetype': {'id': '10601'},
    'summary': 'test1010',
    'customfield_11004': {'id': '11108'},
    'customfield_11008': {'id': '11117'},
    'customfield_10413': {'id': '10317'},
    'reporter': {'name': 'yaowanjun','key': 'yaowanjun'},
    'description': 'test\n1、test1\n2、test2',
}
newissue = ira.create_issue(issueadd)
print(newissue)

#2
new_issue = jira.create_issue(
    project='PROJ_key_or_id',
    summary='New issue from jira-python',
    description='Look into this one', issuetype={'name': 'Bug'})

#3 创建多个
issue_list = [
{
    'project': {'id': 123},
    'summary': 'First issue of many',
    'description': 'Look into this one',
    'issuetype': {'name': 'Bug'},
},
{
    'project': {'key': 'FOO'},
    'summary': 'Second issue',
    'description': 'Another one',
    'issuetype': {'name': 'Bug'},
},
{
    'project': {'name': 'Bar'},
    'summary': 'Last issue',
    'description': 'Final issue of batch.',
    'issuetype': {'name': 'Bug'},
}]
issues = jira.create_issues(field_list=issue_list)
修改
修改描述及summary
myissue = jira.issue('EN-5283')

issueupdate = {
    'summary': 'test1011',
    'description': '测试步骤\n步骤1\n步骤2\n实际结果\n期望结果',
}
myissue.update(issueupdate)
print(myissue.fields.summary)
print(myissue.fields.description)

#修改用户
issue.update(summary='new summary', description='A new summary was added')
issue.update(assignee={'name': 'new_user'})

#不进行真实修改
issue.update(notify=False, description='A quiet description change was made')
删除
myissue = jira.issue('EN-5283')
myissue.delete()
components
#查询components,需要传入project
components = jira.project_components(project)

for i in components:
    print(i.name)

#修改components, issue必须为找到,并且fault_component为list,前提必须已经存在的component
issue.update(fields={"components": fault_component})

#创建components
jira.create_component(project=project, name="OS")
watcher
#添加watcher
jira.add_watcher(new_issue_task, i)

#删除watcher
jira.remove_watcher(new_issue_task, i)
工单状态
#查看工单的状态
transitions = self.jira.jira.transitions(issue)
response = [(t['id'], t['name']) for t in transitions]

更改工单的状态
self.jira.jira.transition_issue(issue_instance, transition=flag)
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200