#封装jira一些方法
from jira importJIRAimportsysclassJiraTool():def __init__(self, server, username, password, maxResults = 500):
self.server=server
self.basic_auth=(username, password)#issues查询的最大值
self.maxResults =maxResultsdeflogin(self):
self.jira= JIRA(server=self.server, basic_auth=self.basic_auth)if self.jira ==None:print('连接失败')
sys.exit(-1)defget_projects(self):"""获得jira 的所有项目
:return:"""
return [(p.key, p.name, p.id) for p inself.jira.projects()]defget_components(self, project):"""获得某项目的所有模块
:param project:
:return:"""
return [(c.name, c.id) for c inself.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 incomponents]:
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},
}returnself.jira.create_issue(issue_dict)defdelete_issue(self, issue):"""删除bug
:param issue:
:return:"""issue.delete()defupdate_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)defupdate_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)defsearch_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)exceptException as e:print(e)
sys.exit(-1)returnissuesdefsearch_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 inself.jira.comments(issue)]ifhasattr(issue.fields, content_type):
result=getattr(issue.fields, content_type)ifisinstance(result, list):return [c.name for c in result if hasattr(c, 'name')]return result