Python's call to jira

1: First of all, we must know the basic attributes

# -*- coding:utf-8 -*-
import datetime

from jira import JIRA
jira_ip="127.0.0.1"
jira_user='ceshi1'
jira_passwd="1"
port=8080
jr = JIRA('http://'+jira_ip+':8080/', auth=(jira_user, jira_passwd))#登录
all_bug=jr.search_issues("project = 'JD'")#jql语法,查询项目为JD下面的所有缺陷
for i in all_bug:#项目下缺陷清单名称和id
    print(i,i.id)#缺陷名称,缺陷所对应的ID
    isss = jr.issue(i.id)
    print("缺陷类型:{0}".format(isss.fields.issuetype))#打印缺陷的类型
    print("标题内容:{0}".format(isss.fields.summary))#打印缺陷的主题即标题内容
    print("缺陷描述:{0}".format(isss.fields.description))#打印缺陷的描述即具体内容
    print("缺陷状态:{0}".format(isss.fields.status))#打印缺陷的当前状态,如:已解决,关闭
    print("缺陷解决结果:{0}".format(isss.fields.resolution)) #打印缺陷的解决结果
    print("缺陷级别:{0}".format(isss.fields. priority))#打印缺陷的优先级,如:严重,重要,轻微等
    print("缺陷创建者:{0}".format(isss.fields. reporter))#打印缺陷的创建人名称
    print("缺陷指派人:{0}".format(isss.fields. assignee))#打印缺陷的指派人名称
    print("缺陷创建日期:{0}".format(isss.fields. created))#打印缺陷的创建日期
    print("缺陷修改日期:{0}".format(isss.fields.updated))#打印缺陷的修改日期
    print("缺陷的标签:{0}".format(isss.fields.labels))  # 打印缺陷的标签
    print('%s'%isss.fields.comment.comments)#打印缺陷的备注信息
    print(isss.fields.attachment)#打印缺陷的附件信息
    print('%s'%isss.fields.components)#打印缺陷的所属模块信息
    print("缺陷环境信息:{0}".format(isss.fields.environment))#打印缺陷的环境信息
    print(isss.fields.attachment)

2: Create a project: (administrator rights)

a = [i.key for i in jr.projects()]#获取jira下所有的所有项目
projkey=str.upper(“项目名称”)#由于项目的Key都是大写
if str.upper(“项目名称”) in a:#如果项目存在就跳过
    pass
else:#不存在就创建
    jr.create_project(projkey,  type="Business",)
    #jr.create_component(Component, projkey)
    #version=db+str(0.1)
    #jr.create_version(version, projkey, description="", releaseDate="2020-12-12", startDate="2020-11-11")# 第一个参数版本名称 第二个参数KEY 最后三个参数分别是 “描述”	Start date	Release date

3: Submit bugs and upload attachments:

pj_id=jr.project("JD").id#项目所对用的ID
issue_dict = {
    
    
    'project': {
    
    'id': pj_id},  # 项目id
    'summary': "summary",  # BUG概要
    'description': "description",  # BUG详情
    'priority': {
    
    'name': 'High'},  # bug优先级
    'assignee': {
    
    'name': "分配给谁"},  # 分配人
    'labels': ["ce"],  # 所属项目标签
    'issuetype': {
    
    'id': 10002}  # 问题类型-任务#不同流程版本数字对应不一样可以根据需要用fidder抓包 看表单是啥。
}
iss=jr.create_issue(issue_dict)
jr.add_attachment(issue=iss, attachment="文件路径", filename="文件名")
print("bug 提交成功")

4: Close the bug

利用jql语法查询需要关闭的缺陷。
查找项目名称为JD,标题包含数据,状态不为完成,标签为test的数据。
issue = jr.search_issues("project = 'JD' and  summary ~ '数据' and status !='DONE' and labels='test'" 
if issue:#存在未关闭的
    for i in issue:
        print(i, i.id)
        jr.transition_issue(i.id, transition='41')  # 11待办,21处理中,31完成
        print("关闭bug成功")

5: Reopen the bug and upload the attachment

issue = jr.search_issues("project = 'JD' and  summary ~ '数据' and status ='DONE' and labels='test'" 
if issue2:#存在已关闭需要重新打开。
    for i in issue2:
        print(i, i.id)
        jr.transition_issue(i.id, transition='11')  # 11待办,21处理中,31完成
        id = [i.key for i in issue2]
        issue = jr.issue(id[0])
        jr.add_attachment(issue=issue, attachment=file_dir, attachment="文件路径", filename="文件名")  # 更新附件信息
        print("重新打开bug成功,上传附件成功")

6: jira JQL grammar: #reference other people's

Search requirements JQL statement
Pending tasks assigned to me assignee = currentUser() AND resolution is EMPTY
Tasks that I reported and did not handle completed reporter = currentUser() AND resolution is EMPTY
Overdue tasks in a project project = “XXX” AND due < now()
Tasks in a project that will expire after 3 days project = “XXX” AND due <= endOfDay(3d)
Tasks in the to-do and in-process status of a project project = "XXX" AND status in (to be done, processing)
Tasks in a project that have not changed within 48 hours project = “XXX” AND updated <= -48h ORDER BY updated DESC
The bug of not uploading attachments in a project project = “XXX” and type= Bug and attachments is EMPTY
Tasks completed by users of the test group in a project project = “XXX” and assignee in membersOf(“testers”) and status = 已完成
Tasks solved in the last 30 days in a project project = “XXX” and resolved >= startOfDay(-30d)
Unresolved tasks in the upcoming version of a project project = “XXX” and fixVersion = earliestUnreleasedVersion() and resolution is EMPTY
A bug reopened in a project project = “XXX” and type = Bug and resolution changed from fixed to empty
A bug whose title contains "Server Exception" project = "XXX" and type = Bug and summary ~ "Server Abnormal"
Tasks marked in the current Sprint on a project project = “XXX” and Sprint in openSprints() and Flagged = Impediment
Yesterday I changed my status from To Do to Task in Process status changed FROM pending TO processing BY currentUser() AFTER startOfDay(-1d)
Issue modified by User1 in 2019 issue in updatedBy(User1, 2019-1-1, 2019-12-31)
Tasks included in the released version of a project project = “XXX” and fixVersion in releasedVersions()
Issues that have changed status last week status changed DURING (startOfWeek(-1), endOfWeek(-1))
Link to an Issue task with "is duplicated by" relationship issue in linkedIssues(PRO-1,“is duplicated by”)

Guess you like

Origin blog.csdn.net/qq_34237321/article/details/109746532