-
Notifications
You must be signed in to change notification settings - Fork 0
/
issue_comment.py
45 lines (41 loc) · 2.01 KB
/
issue_comment.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
import re
from config import AUTHORIZED_LEVEL
from dgp_utils.dgp_tools import *
from issue import log_dump
from operater import make_issue_comment
async def comment_handler(payload: dict) -> str:
return_result = ""
# issue commenter
repo_name = payload["repository"]["full_name"]
issue_number = payload["issue"]["number"]
comment_sender = payload["comment"]["user"]["login"]
authority = payload["comment"]["author_association"]
comment_body = payload["comment"]["body"]
pull_log_command = re.search(r"(/log) (?P<device_id>\w{32})", comment_body)
if not pull_log_command:
pull_log_command = re.search(r"(^/log$)", comment_body)
if pull_log_command:
if authority.lower() not in AUTHORIZED_LEVEL:
print(f"Unauthorized user: {comment_sender} was trying to pull log, authority: {authority}")
return ""
comment_body_list = comment_body.split(" ")
if len(comment_body_list) == 2:
device_id = pull_log_command.group("device_id")
print("Pulling log for device: ", device_id)
dumped_log = get_log(device_id)
if dumped_log["data"]:
data = f"device_id: {device_id} \n```\n{dumped_log['data'][0]['Info']}\n```"
return_result += make_issue_comment(repo_name, issue_number, data)
else:
return_result += make_issue_comment(repo_name, issue_number, f"> {comment_body}\n\n无已捕获的错误日志")
elif len(comment_body_list) == 1:
issue_body = payload["issue"]["body"]
dumped_log = log_dump(issue_body)
if dumped_log["code"] != 0:
print("Find device log, post it")
return_result += make_issue_comment(repo_name, issue_number, dumped_log["data"])
else:
return_result += make_issue_comment(repo_name, issue_number, f"> {comment_body}\n\n未找到设备 ID")
else:
print("Invalid command format when pulling log")
return return_result