调用Jira API统计一段时间每个账户内ticket被resolved的数量

Jira是不能分组统计的,只能用dashboard里用二维表格统计,但是如果查询语句是“status changed to resolved by”,统计表格里是不能显示出来“resolved by”这个用户名的。
之前同事是手动做了特别多的查询,然后把这些查询放到confluence里进行统计的,所以就想用jira api分账户统计一下,代码如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import json
from functools import reduce

import requests
from prettytable import PrettyTable

token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # 从JIRA的Porta里profile页面创建一个Personal Access Tokens
headers = {
"Authorization": "Bearer %s" % token,
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=UTF-8",
}
duiring_time = "2022-01-01, 2023-01-01"
jira_user_accounts = [
'zhangsan',
'lisi',
'wangwu'
]


if __name__ == '__main__':
requests.packages.urllib3.disable_warnings()
total_result = []
for jira_user in jira_user_accounts:
params = {
"startAt": 0,
"maxResults": 2000,
"jql": "project = CSFS AND status changed to resolved by (%s) during (%s)" % (jira_user, duiring_time),
"fields": ['customfield_40669']
}
res = requests.post("https://jiradc2.ext.net.nokia.com/rest/api/2/search", headers=headers,
data=json.dumps(params, ensure_ascii=False).encode("utf-8"), verify=False)
issues = res.json()['issues']
if len(issues) > 0:
display_name = "Unknown"
# 想办法找到所有的display name
try:
for issue in issues:
resolve_users = issue['fields']["customfield_40669"]
for resolve_user in resolve_users:
# 这个ticket可能会被多个人resolve,取最后一个resolver
if resolve_user['name'] == jira_user:
display_name = resolve_user["displayName"]
raise StopIteration
except StopIteration:
total_result.append({"name": jira_user, 'count': len(issues), "display_name": display_name})
else:
total_result.append({"name": jira_user, 'count': 0})

total_result.sort(key=lambda u: u["count"], reverse=True)

total_count = reduce(lambda x, y: x + y["count"], total_result, 0)
print("total: " + str(total_count))
myTable = PrettyTable(["user", "display name", "count"])
for result in total_result:
if 'display_name' in result:
myTable.add_row([result['name'], result['display_name'], result['count']])
else:
myTable.add_row([result['name'], '', result['count']])
print(myTable)

查询后的结果:

1
2
3
4
5
6
7
+----------+-----------------------+-------+
| user | dispaly | count |
+----------+-----------------------+-------+
| zhangsa | San Zhang (Company) | 302 |
| lisi | Si Li (Company) | 113 |
| wangwu | Wu Wang (Company) | 93 |
+----------+-----------------------+-------+

效果不错,值得分享一下。

Notice: 正常情况下,这里会有一个基于utteranc.es的留言系统,如果看不到,可能要想想办法才能看到。

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2024 Tiaobug All Rights Reserved.

本站总访问量    次