JIRA 模块 bug管理工具
from jira import JIRA #导入jira jira=JIRA(server='http://127.0.0.1:8080', basic_auth=('name', 'password')) #和jira服务器建立连接
#获取当前用户 jira.user(jira.current_user()) #获取当前用户所有项目,返回项目字典 for j in jira.projects(): print j, j.name #打印项目名称;
#通过字段id号获取issu issue = jira.issue('JRA-1330', fields='summary,description,comment') summary = issue.fields.summary description = issue.fields.description
#查询issues jql = "project=PROJ AND component = 模块 AND status=closed AND summary ~标题 AND description ~描述"
try: issues = self.jira.search_issues(jql, maxResults=500) #maxResults参数是设置返回数据的最大值,默认是50。 except Exception as e:
print(e)
sys.exit(-1)
bug_count=len(issues) #查询的bug数 for issue in issue_search(): issue = str(issue) + issue.fields.summary #标题
projectKey = issue.key.split("-")[0] # 获得bug的项目key print issue return issue
components = jira.project_components(jira.project("项目key")) # 获得某项目的所有模块名称
# 创建项目模块
jira.create_component(compoment, project, description=description, leadUserName=None, assigneeType=None, isAssigneeTypeValid=False)
#查询相关参数 projects #所有项目,projects = jira.projects() project #项目 components #模块 summary #标题 issuetype #获取缺陷类型,如缺陷,线上问题、改进等 deion #获取缺陷的具体描述内容 assignee #经办人,currentUser()现在的用户 reporter #报告人 resolution #解决结果,重复Duplicate,开始unresolved, status #bug状态,关闭closed,已解决resolved due #到期时间,due < endOfWeek() created #创建时间
updated #更新时间 priority #优先级,按优先级排序orderby priority desc #评论 comments_a = issue.fields.comments.comments comments_b = jira.comments(issue) # comments_b ==comments_a
#更新bug信息 issue = jira.issue('JIRA-2') issue.update(versions=[{"name":'V1.1'}]) issue.update(labels=['Label A','Label B']) issue.update(components=[{'name': 'component'}]) issue.update(summary='new summary', description='description') issue.update(assignee={'name': 'new_user'}) issue.update(issuetype={'name': 'Task'}) #更改bug状态status transitions=self.jira.transitions(issue)
print([(t['id'],t['name']) for t in transitions]) self.jira.transition_issue(issue, 'To Do') #或者 self.jira.transition_issue(issue, 11) issue_dict= { 'summary': 'issue概要', 'description': 'issue描述', } issue.update(fields=issue_dict)
def create_issue(self, summary, description, assignee, compoment, project, issuetype='Task', priority='Medium'):
"""
创建提交bug
:param project: 项目
:param issuetype: 问题类型,Task
:param summary: 主题
:param compoment: 模块
:param description: 描述
:param assignee: 经办人
:param priority: 优先级
:return:
"""
issue_dict = {
'project': {'key': project},
'issuetype': {'name': issuetype},
'summary': summary,
'components': [{'name': compoment}],
'description': description,
'assignee': {'name': assignee},
'priority': {'name': priority},
}
return self.jira.create_issue(issue_dict)
#封装jira一些方法 from jira import JIRA import sys class JiraTool(): def __init__(self, server, username, password, maxResults = 500): self.server = server self.basic_auth = (username, password) # issues查询的最大值 self.maxResults = maxResults def login(self): self.jira = JIRA(server=self.server, basic_auth=self.basic_auth) if self.jira == None: print('连接失败') sys.exit(-1) def get_projects(self): """ 获得jira 的所有项目 :return: """ return [(p.key, p.name, p.id) for p in self.jira.projects()] def get_components(self, project): """ 获得某项目的所有模块 :param project: :return: """ return [(c.name, c.id) for c in self.jira.project_components(self.jira.project(project))] def create_component(self, project, compoment, description, leadUserName=None, assigneeType=None, isAssigneeTypeValid=False): """ # 创建项目模块 :param project: 模块所属项目 :param compoment:模块名称 :param description:模块描述 :param leadUserName: :param assigneeType: :param isAssigneeTypeValid: :return: """ components = self.jira.project_components(self.jira.project(project)) if compoment not in [c.name for c in components]: self.jira.create_component(compoment, project, description=description, leadUserName=leadUserName, assigneeType=assigneeType, isAssigneeTypeValid=isAssigneeTypeValid) def create_issue(self, project, compoment, summary, description, assignee, issuetype, priority='Medium'): """ 创建提交bug :param project: 项目 :param issuetype: 问题类型,Task :param summary: 主题 :param compoment: 模块 :param description: 描述 :param assignee: 经办人 :param priority: 优先级 :return: """ issue_dict = { 'project': {'key': project}, 'issuetype': {'id': issuetype}, 'summary': summary, 'components': [{'name': compoment}], 'description': description, 'assignee': {'name': assignee}, 'priority': {'name': priority}, } return self.jira.create_issue(issue_dict) def delete_issue(self, issue): """ 删除bug :param issue: :return: """ issue.delete() def update_issue_content(self, issue, issue_dict): """ 更新bug内容 :param issue: :param issue_dict: issue_dict = { 'project': {'key': project}, 'issuetype': {'id': issuetype}, 'summary': summary, 'components': [{'name': compoment}], 'description': description, 'assignee': {'name': assignee}, 'priority': {'name': priority}, } :return: """ issue.update(fields=issue_dict) def update_issue_issuetype(self, issue, issuetype): """ 更新bug 状态 :param issue: :param issuetype: 可以为id值如11,可以为值如'恢复开启问题' :return: """ transitions = self.jira.transitions(issue) # print([(t['id'], t['name']) for t in transitions]) self.jira.transition_issue(issue, issuetype) def search_issues(self, jql): """ 查询bug :param jql: 查询语句,如"project=项目key AND component = 模块 AND status=closed AND summary ~标题 AND description ~描述" :return: """ try: # maxResults参数是设置返回数据的最大值,默认是50。 issues = self.jira.search_issues(jql, maxResults=self.maxResults) except Exception as e: print(e) sys.exit(-1) return issues def search_issue_content(self, issue, content_type): """ 获取bug 的相关信息 :param issue: :param content_type:项目project; 模块名称components; 标题summary; 缺陷类型issuetype; 具体描述内容description; 经办人assignee; 报告人reporter; 解决结果resolution; bug状态status; 优先级priority; 创建时间created; 更新时间updated; 评论comments :return: """ # 评论 if content_type == 'comments': return [c.body for c in self.jira.comments(issue)] if hasattr(issue.fields, content_type): result = getattr(issue.fields, content_type) if isinstance(result, list): return [c.name for c in result if hasattr(c, 'name')] return result
if __name__ == '__main__':
jiratool = JiraTool('http://127.0.0.0:8080', '用户名', '密码')
jiratool.login()
issues = jiratool.search_issues('project=项目名称 AND component = 模块名称')
for issue in issues:
print(jiratool.search_issue_content(issue,'comments'))
参考官网官网:https://jira.readthedocs.io/en/master/