Category Archive : Jira相关

使用jira的api获得project的board参数和sprint参数

需求

每个jira项目都有sprint参数和board参数。关系为一对多的关系。
project:board=1:n
board:sprint=1:n
如果要查询出一个项目有哪些正在进行的sprint,还需要费一番功夫。因为目前jira -api的python库里并没有给出方法,不过我们可以通过下面的方法获得:

通过get请求,根据项目的key或者ID获得board信息,地址和参数如下

url+"/rest/agile/1.0/board?projectKeyOrId=" + projectKeyOrId

通过get请求,根据board的ID查询到sprint的信息,根据state进行筛选

url+"/rest/agile/1.0/board/" + str(item['id']) + "/sprint?state=future,active"

代码

# -- coding: UTF-8 --
import requests
from jira import JIRA
url = 'https://jira.atlassian.com'
jira = JIRA(server=url, basic_auth=('username', 'password'))
cookies = jira._session.cookies
projectKeyOrId = "project_key"
board_url = url+"/rest/agile/1.0/board?projectKeyOrId=" + projectKeyOrId
response = requests.get(url, cookies=cookies,
                        headers={"Accept": "application/json"})
# print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
qq = response.json()
sprint_result_list = []
for item in qq['values']:
    sprint_url = url+"/rest/agile/1.0/board/" + str(item['id']) + "/sprint?state=future,active"
    response = requests.get(url, cookies=cookies,
                            headers={"Accept": "application/json"})
    sprint_json = response.json()
    if 'values' in sprint_json:
        sprint_list = sprint_json['values']
        for sprint in sprint_list:
            element = {'sprint_id': sprint['id'], 'sprint_name': sprint['name']}
            if element not in sprint_result_list:
                sprint_result_list.append(element)
print(sprint_result_list)

jira-api使用gevent,快速批量修改数据

需求

因为我们本地数据库中经常会产生大量的数据,这些数据需要同步至jira.覆盖掉jira上的老数据。
因为jira服务器部署在国外,连接巨慢。每次修改一个,至少需要花费10秒左右的时间。
而jira-api没有提供批量或者异步同步的方法,所以需要自己使用一个异步的方式来实现一次性把所有的请求发送出去。
jira服务端有时候吃不消,会关闭掉我的一些连接,所以还需要在被关闭之后,记录下这些同步至jira失败的,重新继续修改操作

用到的库

gevent  库

gevent是一个基于libev的并发库。它为各种并发和网络相关的任务提供了整洁的API。

jira库。
里面有jira登录,修改,新增issue的方法

代码实现

# -*- coding: UTF-8 -*-
import datetime
import random
import gevent
from gevent import monkey
from jira import JIRA
from model import addDefectDAO
monkey.patch_all()
issue_list = []
def update(issue_key):
    # 产生一点小小的间隔,避免一瞬间发出去100多个请求
    gevent.sleep(random.randint(1, 5) * 0.001)
    global issue_list
    starttime = datetime.datetime.now()
    print(starttime)
    print("start:" + issue_key)
    print("start:" + issue_key)
    issue = jira.issue(issue_key)
    issue_dict = {
        'summary': issue_key
    }
    try:
        issue.update(fields=issue_dict)
        issue_list.remove(issue_key)
    except Exception:
        pass
    print("end:" + issue_key)
# 从一个本地数据库取出一堆issue的信息,602是本地的project信息编号
all = addDefectDAO.defect_select_not_draft_by_project_id(602)
for item in all:
    issue_list.append(item["issue_key"])
url = "https://www.jira.com"
username = "username"
password = "password"
jira = JIRA(server=url, basic_auth=(username, password), max_retries=0, timeout=400, async_=True, async_workers=5)
# jira2 = Jira(url=url, username=username, password=password)
print("登录成功")
starttime = datetime.datetime.now()
api_list = []
while issue_list != []:
    for i in issue_list:
        api_list.append(gevent.spawn(update, i))
    gevent.joinall(api_list)
endtime = datetime.datetime.now()
print('总耗时')
print((endtime - starttime).seconds)
print('*******')

注意事项

1.需要引用monkey.patch_all()
2.引用monkey.patch_all()之后会有错误信息:

UserWarning: libuv only supports millisecond timer resolution; all times less will be set to 1 ms

这个是已知bug,在11月29日有人在github上已经提过了,作者会在之后的版本修改。目前不影响使用。请忽略
3.通过这样的方法修改之后,经过我的测试,速度大概快了5倍以上

Jira-API的详细使用例子

下面是Jira-API的详细使用的例子,包含:

  • Jira的登陆,通过jql批量查询jira-issue,
  • 获得jira-project下的所有issue,assignee的详细信息,
  • 添加和更新defect
  • 下载和上传附件
  • 通过Jira登录的cookies搭配requsts库发送自定义的一些http请求
 
# -*- coding: UTF-8 -*-
import traceback
import requests
from atlassian import Jira
from jira import JIRA
import globalParam
jira = object
jira2 = object
#url是jira的地址
url = "https://www.Jira.com"
# 登录jira,成功为1,失败为0
def login(username, password):
    global jira, jira2
    result = 2
    try:
        jira = JIRA(server=url, basic_auth=(username, password), max_retries=1, timeout=400)
        jira2 = Jira(url=url, username=username, password=password)
        globalParam.reporter = get_current_user()
    except Exception:
        result = 0
        return result
    result = 1
    return result
# 获得一个项目的所有assignee的信息
def get_all_assignable_users_by_project_key(project_key):
    return jira2.get_all_assignable_users_for_project(project_key)
#下载附件到本地
def download_attachment(jira_attachment_id, filepath):
    attachment = jira.attachment(jira_attachment_id)
    image = attachment.get()
    with open(filepath, 'wb') as f:
        f.write(image)
#删除Jira上的附件
def delete_attachment(jira_attachment_id):
    jira.delete_attachment(jira_attachment_id)
#获得当前登陆的用户名
def get_current_user():
    return jira.myself()['displayName']
#获得当前登陆的用户的Key
def get_current_eid():
    return jira.myself()['key']
#根据jira-key获得开发人员
# key 可以是key'JIRA-1451'
def get_assignee(self, key):
    global jira
    issue = jira.issue(key)
    return issue.fields.assignee
#获得所有的Jira项目
def get_all_project():
    try:
        projects = jira.projects()
    except Exception:
        print(traceback.format_exc())
        result = 0
        return result
    return projects
# 根据jira-project-key参数获得所有的issue的各类信息
def get_issues_by_project_key(project_key):
    # 因为component从jira取出来是list,所以用逗号将它分割,重新组合成string
    jqlstring = "project = " + project_key + " AND issuetype = Defect ORDER BY updated DESC"
    try:
        query = jira.search_issues(
            jql_str=jqlstring,
            json_result=True,
            fields="key,summary,description,environment,comment,"
                   "components,labels,assignee,reporter,status,attachment,updated",
            maxResults=4000)
    except Exception:
        print(traceback.format_exc())
        result = 0
        return result
    issues = query["issues"]
    return issues
# 根据jira-project-key参数和时间获得所有的issue的各类信息
def get_issues_by_project_key_and_time(project_key, last_synctime):
    # 因为component从jira取出来是list,所以用逗号将它分割,重新组合成string
    jqlstring = "project = " + project_key + " AND updatedDate >='" + last_synctime + 
                "' AND issuetype = Defect ORDER BY updated DESC"
    try:
        query = jira.search_issues(
            jql_str=jqlstring,
            json_result=True,
            fields="key,summary,description,environment,comment,"
                   "components,labels,assignee,reporter,status,attachment,updated",
            maxResults=4000)
    except Exception:
        print(traceback.format_exc())
        result = 0
        return result
    issues = query["issues"]
    return issues
#根据jira-project-key参数获得所有的component信息
def get_components_by_project_key(project_key):
    try:
        components = jira.project_components(project_key)
    except Exception:
        print(traceback.format_exc())
        result = 0
        return result
    return components
#根据jira-project-key参数获得所有的version信息
def get_version_by_project_key(project_key):
    jira_project = jira.project(project_key)
    versions = jira_project.versions
    return versions
#添加一个defect
def add_defect(project_key, summary, description, steps_to_reproduce, labels, defect_severity, env, components,
               frequency, product_id, version_name, assignee_eid):
    jira_project = jira.project(project_key)
    label_list = labels.split(",")
    pid = jira_project.id
    if isinstance(product_id, int):
        product_id = str(product_id)
    components_list = []
    components_name = components.split(",")
    for item in components_name:
        components_list.append({"name": item})
    issue_dict = {
        'project': {'id': pid},
        'summary': summary,
        'description': description,
        'issuetype': {'name': 'Defect'},
        'labels': label_list,
        'customfield_10040': {"value": defect_severity},  # BUG的Defect Severity字段
        'customfield_10033': steps_to_reproduce,  # BUG的描述步骤
        'environment': env,
        'components': components_list,
        'customfield_10336': {"value": frequency},
        'customfield_13600': product_id,
        'customfield_11170': {"name": version_name},
        "assignee": {"name": assignee_eid}
    }
    if assignee_eid is None:
        del issue_dict['assignee']
    if env == "":
        del issue_dict['environment']
    if components == "":
        del issue_dict['components']
    if product_id == "" or product_id == "n":
        del issue_dict['customfield_13600']
    if version_name == "":
        del issue_dict['customfield_11170']
    new_issue = jira.create_issue(fields=issue_dict)
    return new_issue.key
#更新一个defect
def update_defect(issue_key, summary, description, steps_to_reproduce,
                  labels, defect_severity, env, components, frequency, product_id, version_name, assignee_eid):
    issue = jira.issue(issue_key)
    label_list = labels.split(",")
    components_list = []
    components_name = components.split(",")
    if components == "":
        pass
    for item in components_name:
        components_list.append({"name": item})
    issue_dict = {
        'summary': summary,
        'description': description,
        'issuetype': {'name': 'Defect'},
        'labels': label_list,
        'customfield_10040': {"value": defect_severity},  # BUG的Defect Severity字段
        'customfield_10033': steps_to_reproduce,  # BUG的描述步骤
        'environment': env,
        'components': components_list,
        'customfield_10336': {"value": frequency},
        'customfield_13600': str(product_id),
        'customfield_11170': {"name": version_name},
        "assignee": {"name": assignee_eid}
    }
    if assignee_eid is None:
        del issue_dict['assignee']
    if env == "":
        del issue_dict['environment']
    if components == "":
        del issue_dict['components']
    if version_name == "":
        del issue_dict['customfield_11170']
    update_issue = issue.update(fields=issue_dict)
    cccc = update_issue
    return issue_key
#将一个本地的附件上传到Jira上
def add_attachment(issue, path, filename):
    newpath = path.encode('utf-8')
    cc = unicode(newpath, "utf-8")
    result = jira.add_attachment(issue=issue, attachment=cc, filename=filename)
    return result
#根据jira-key获得该issue所有的附件
def get_attachment(issue_key):
    issue = jira.issue(issue_key)
    return issue.fields.attachment
#通过项目project_key值,抓取页面上的一些自定义参数,例如自定义参数13600,对应products参数
def get_products_by_project_key(project_key):
    product_map = {}
    jiraproject = jira.project(project_key)
    #jira._session.cookies可以用来搭配requests库,发送请求
    cookies = jira._session.cookies
    body = {"pid": jiraproject.id}
    url = "https://www.jira.com/secure/QuickCreateIssue!default.jspa?decorator=none"
    result = requests.post(url, data=body, cookies=cookies,
                           headers={"Content-type": "application/x-www-form-urlencoded"})
    html = result.text
    try:
        select1 = html.split('"id":"customfield_13600","label":"Product/s","required":false,')[1]
        select2 = select1.split('</select>')[0]
        if not select2.__contains__("\n\tNone\n\n"):
            select3 = select2.split('<option')
            c = select3
            for item in select3:
                if item.__contains__('value='):
                    patern_id = item.split('value=\"')[1]
                    id = patern_id.split('\"')[0]
                    patern_value = item.split("\n    ")
                    value = patern_value[1].strip()
                    product_map[id] = value
    except Exception:
        # print(traceback.format_exc())
        pass
    return product_map
#给一个jira-issue添加comment
def add_comment(issue_key, comment_body):
    comment = jira.add_comment(issue_key, comment_body)
    return comment.id

将Jira获取的UTC时间转本地时间yyyy-MM-dd’T’HH:mm:ss

通过API获得jira 备注(comment)的更新时间,发现获得的UTC时间格式为
2018-08-14T02:52:22.216+0000
这个时候,如果需要转换成datetime :yyyy-MM-dd'T'HH:mm:ss格式,
或者date:yyyy-MM-dd,可以用下面的代码:

#!/usr/bin/python
# -*- coding:utf-8 -*-
import re
import time
from datetime import datetime
def utc2local(utc_st):
    # UTC时间转本地时间(+8:00)
    now_stamp = time.time()
    local_time = datetime.fromtimestamp(now_stamp)
    utc_time = datetime.utcfromtimestamp(now_stamp)
    offset = local_time - utc_time
    local_st = utc_st + offset
    return local_st
def local2utc(local_st):
    # 本地时间转UTC时间(-8:00)
    time_struct = time.mktime(local_st.timetuple())
    utc_st = datetime.utcfromtimestamp(time_struct)
    return utc_st
def utc_format(utcstr):
    """
    将不标准的utc时间字符串转换成datetime格式
    :param utcstr: 不标准的utc字符串,如‘2018-04-23T03:43:35.000+0000’
    :return:
    """
    utcstr = re.sub('.ddd+0000', '', utcstr, count=1)
    try:
        UTC_FORMAT = "%Y-%m-%dT%H:%M:%S"
        d_time = datetime.strptime(utcstr, UTC_FORMAT)
    except Exception:
        UTC_FORMAT = "%Y-%m-%d %H:%M:%S"
        d_time = datetime.strptime(utcstr, UTC_FORMAT)
    return d_time
def get_local_date(utcstr):
    """
    将jira返回的str类型的时间,转换成datetime格式,并去除日期之后的内容(提供给SprintDailyIssuesBar使用的日期处理接口)
    :param utcstr: jira返回的time字符串
    :return:datetime格式
    """
    utc_st = utc_format(utcstr)
    # print utc_st
    local_st = utc2local(utc_st)
    # print local_st
    local_str = local_st.strftime('%Y-%m-%d')
    local_date = datetime.strptime(local_str, "%Y-%m-%d")
    # print local_date
    return local_date
def get_local_datetime_str(utcstr):
    """
    将jira返回的str类型的UTC时间,转换成str类型的local time,包含具体的时、分、秒(提供给tableData使用,转换时间)
    :param utcstr: jira返回的time字符串
    :return:string
    """
    utc_st = utc_format(utcstr)
    # print utc_st
    local_st = utc2local(utc_st)
    # print local_st
    local_str = local_st.strftime('%Y-%m-%d %H:%M:%S')
    return local_str
 if __name__ == '__main__':
     utcstr1 = '2018-08-14T02:52:22.216+0000'
     tt=get_local_datetime_str(utcstr1)
     print(tt)

将Jira获取的UTC时间转本地时间yyyy-MM-dd’T’HH:mm:ss

通过API获得jira 备注(comment)的更新时间,发现获得的UTC时间格式为
2018-08-14T02:52:22.216+0000
这个时候,如果需要转换成datetime :yyyy-MM-dd'T'HH:mm:ss格式,
或者date:yyyy-MM-dd,可以用下面的代码:

#!/usr/bin/python
# -*- coding:utf-8 -*-
import re
import time
from datetime import datetime
def utc2local(utc_st):
    # UTC时间转本地时间(+8:00)
    now_stamp = time.time()
    local_time = datetime.fromtimestamp(now_stamp)
    utc_time = datetime.utcfromtimestamp(now_stamp)
    offset = local_time - utc_time
    local_st = utc_st + offset
    return local_st
def local2utc(local_st):
    # 本地时间转UTC时间(-8:00)
    time_struct = time.mktime(local_st.timetuple())
    utc_st = datetime.utcfromtimestamp(time_struct)
    return utc_st
def utc_format(utcstr):
    """
    将不标准的utc时间字符串转换成datetime格式
    :param utcstr: 不标准的utc字符串,如‘2018-04-23T03:43:35.000+0000’
    :return:
    """
    utcstr = re.sub('.\d\d\d\+0000', '', utcstr, count=1)
    try:
        UTC_FORMAT = "%Y-%m-%dT%H:%M:%S"
        d_time = datetime.strptime(utcstr, UTC_FORMAT)
    except Exception:
        UTC_FORMAT = "%Y-%m-%d %H:%M:%S"
        d_time = datetime.strptime(utcstr, UTC_FORMAT)
    return d_time
def get_local_date(utcstr):
    """
    将jira返回的str类型的时间,转换成datetime格式,并去除日期之后的内容(提供给SprintDailyIssuesBar使用的日期处理接口)
    :param utcstr: jira返回的time字符串
    :return:datetime格式
    """
    utc_st = utc_format(utcstr)
    # print utc_st
    local_st = utc2local(utc_st)
    # print local_st
    local_str = local_st.strftime('%Y-%m-%d')
    local_date = datetime.strptime(local_str, "%Y-%m-%d")
    # print local_date
    return local_date
def get_local_datetime_str(utcstr):
    """
    将jira返回的str类型的UTC时间,转换成str类型的local time,包含具体的时、分、秒(提供给tableData使用,转换时间)
    :param utcstr: jira返回的time字符串
    :return:string
    """
    utc_st = utc_format(utcstr)
    # print utc_st
    local_st = utc2local(utc_st)
    # print local_st
    local_str = local_st.strftime('%Y-%m-%d %H:%M:%S')
    return local_str
 if __name__ == '__main__':
     utcstr1 = '2018-08-14T02:52:22.216+0000'
     tt=get_local_datetime_str(utcstr1)
     print(tt)

苏ICP备18047533号-1