-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
207 lines (201 loc) · 10 KB
/
main.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
from src.api.feishu_sign import sign
import sys
import os
from util.get_json import config
from asyncio.log import logger
import datetime
from jira import JIRA
from datetime import datetime as dt
import time
from src.api.log import log # 封装日志引入
from src.api.feishu_robot_push import feishu_robot # 飞书机器人引入
from src.api.feishu_sign import sign # 飞书签名
# 日志实例化
logger = log().logger()
# 读取配置文件
Config = config("{}/config/config.json".format(os.getcwd())
).get_config_from_json()
UserInfo = config("{}/config/userInfo.json".format(os.getcwd())
).get_config_from_json()
# jira初始化
jira = JIRA(auth=(Config["jira"]["username"], Config["jira"]
["password"]), options={'server': Config["jira"]["url"]})
# 飞书签名
timestamp = str(round(time.time()))
secret = Config["feishu"]["secret"]
sign = sign(timestamp, secret).gen_sign()
"""
@Time :2022/02/11
@Author :liyinchi
@File :main.py
@Ddescription: 定时任务
"""
def job():
# 存放jira bug list
result = []
# 读取人员名单
workers = UserInfo["userInfo"]
# 初始化迭代
i = 0
k = 0
# 遍历人员名单
for worker in workers:
# [激活状态bug数量]
open_issues = jira.search_issues(
'status = 激活 AND creator in ("'+worker["username"]+'") ORDER BY priority DESC, updated DESC', maxResults=-1)# 注意:如果不加maxResults=-1参数,则实际总数大于50时只能查出50条数据。
# 日志输出
logger.info('【{}】{}'.format(worker["username"], open_issues))
# 放入第一层数据
result.append([
{"username": worker["username"]},
{"name": worker["name"]},
{"open": []}, # 截止今日,”激活“状态bug总数 list
{"resolved": []}, # 截止今日,"已解决"状态bug总数 list
{"last_week_add": []}, # "最近一周新增,"bug总数list
{"last_week_close": []} # "最近一周关闭,"bug总数list
])
# 放入第一层数据open节点中
for index in range(len(open_issues)):
print("result[{}]][2]".format(i), result[i][2])
(result[i][2])['open'].append({
"key": open_issues[index].key,
"id": open_issues[index].id,
"url": Config["jira"]["url_browse"]+open_issues[index].key,
})
# [开发已解决状态bug数量]
resolved_issues = jira.search_issues(
'status = 已解决 AND creator in ("'+worker["username"]+'") ORDER BY priority DESC, updated DESC', maxResults=-1)
# 日志输出
logger.info('【{}】{}'.format(worker["username"], resolved_issues))
# 放入第一层数据resolved节点中
for index in range(len(resolved_issues)):
print("result[{}][3]".format(i), result[i][3])
(result[i][3])['resolved'].append({
"key": resolved_issues[index].key,
"id": resolved_issues[index].id,
"url": Config["jira"]["url_browse"]+resolved_issues[index].key,
})
# [最近一周新建bug数量]
last_week_add_issues = jira.search_issues(
'creator in ("'+worker["username"]+'") AND created >= -1w ORDER BY priority DESC, updated DESC', maxResults=-1)
# 日志输出
logger.info('【{}】{}'.format(worker["username"], last_week_add_issues))
# 放入第一层数据last_week_add节点中
for index in range(len(last_week_add_issues)):
print("result[{}][4]".format(i), result[i][4])
(result[i][4])['last_week_add'].append({
"key": last_week_add_issues[index].key,
"id": last_week_add_issues[index].id,
"url": Config["jira"]["url_browse"]+last_week_add_issues[index].key,
})
# [最近一周关闭bug数量]
last_week_close_issues = jira.search_issues(
'status = 已关闭 AND creator in ("'+worker["username"]+'") AND created >= -1w ORDER BY priority DESC, updated DESC', maxResults=-1)
# 日志输出
logger.info('【{}】{}'.format(worker["username"], last_week_close_issues))
# 放入第一层数据last_week_close节点中
for index in range(len(last_week_close_issues)):
print("result[{}][5]".format(i), result[i][5])
(result[i][5])['last_week_close'].append({
"key": last_week_close_issues[index].key,
"id": last_week_close_issues[index].id,
"url": Config["jira"]["url_browse"]+last_week_close_issues[index].key,
})
i += 1
logger.info('result' + str(result))
"""
result的数据格式:
[[{"username":"zhanggsan"},{"name":"张三"},{"open":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"resolved":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"last_week_add":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"last_week_close":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}]}],
[{"username":"lisi"},{"name":"李四"},{"open":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"resolved":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}]}],
[{"username":"wangwu"},{"name":"王五"},{"open":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"resolved":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}]}]]
"""
# 遍历result 依次调用飞书机器人发送结果
i = 0
for j in result:
#
# logger.info("j数据类型:"+ str(type(j))) # <class 'list'>
# 姓名
logger.info("姓名:" + j[1]['name'])
logger.info("激活 bug:" + str(len(j[2]["open"])-1))
logger.info("已解决 bug:" + str(len(j[3]["resolved"])-1))
logger.info("最近一周新增 bug:" + str(len(j[4]["last_week_add"])-1))
logger.info("最近一周关闭 bug:" + str(len(j[5]["last_week_close"])-1))
message_body = {
"timestamp": timestamp,
"sign": sign,
"msg_type": "post", # 消息类型 包括:text、post、image、file、audio、media、sticker、interactive、share_chat、share_user等,类型定义请参考发送消息content说明:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/im-v1/message/create_json
"content":
{
"post": {
"zh_cn": {
"title": "JIRA bug 日常提醒【{}】【{}】".format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), j[1]['name']),
"content": [
[{
"tag": "text",
"text": "未处理:{}".format(len(j[2]["open"])),
},
{
"tag": "a",
"href": "https://jira.homeking365.com/browse/S5A-1924?filter=10818&jql=status = {} AND assignee in ({}) ORDER BY priority DESC, updated DESC".format("激活", j[0]['username']),
"text": "点击查看"
}],
[{
"tag": "text",
"text": "已解决:{}".format(len(j[3]["resolved"])),
},
{
"tag": "a",
"href": "https://jira.homeking365.com/browse/S5A-1924?filter=10818&jql=status = {} AND assignee in ({}) ORDER BY priority DESC, updated DESC".format("已解决", j[0]['username']),
"text": "点击查看"
}],
[{
"tag": "text",
"text": "最近一周新增bug:{}".format(len(j[4]["last_week_add"])),
},
{
"tag": "a",
"href": "https://jira.homeking365.com/browse/S5A-1924?filter=10818&jql=creator in ({}) AND created >= -1w ORDER BY priority DESC, updated DESC".format(j[0]['username']),
"text": "点击查看"
}],
[{
"tag": "text",
"text": "最近一周关闭bug:{}".format(len(j[5]["last_week_close"])),
},
{
"tag": "a",
"href": "https://jira.homeking365.com/browse/S5A-1924?filter=10818&jql=status = {} AND assignee in ({}) ORDER BY priority DESC, updated DESC".format("已关闭", j[0]['username']),
"text": "点击查看"
}],
# [
# {
# "tag": "at",
# "user_id": "all",
# "user_name": " 所有人 "
# }
# ],
# [{
# "tag": "img",
# "image_key": "img_7ea74629-9191-4176-998c-2e603c9c5e8g",
# "width": 100,
# "height": 100
# }]
]
},
}}
}
i += 1
# 发送飞书机器人
response_message = feishu_robot(
Config["feishu"]["webhook"], message_body).push()
logger.info(response_message)
# BlockingScheduler
scheduler = BlockingScheduler()
# 创建定时器
scheduler.add_job(job, 'cron', day_of_week=Config["timing_task"]["week"], hour=Config["timing_task"]
["hour"], minute=Config["timing_task"]["minute"]) # 每周1-5 上午9点30分执行
# 启动
scheduler.start()