-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathpost_inline_comments.py
More file actions
170 lines (135 loc) · 4.77 KB
/
Copy pathpost_inline_comments.py
File metadata and controls
170 lines (135 loc) · 4.77 KB
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
#!/usr/bin/env python3
import json
import os
import re
import sys
import urllib.error
import urllib.request
def read_findings(path):
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
return data.get("findings", [])
def build_position_map(diff_path):
with open(diff_path, "r", encoding="utf-8") as f:
lines = f.readlines()
position_map = {}
current_path = None
new_line_no = None
position = 0
in_hunk = False
for raw in lines:
line = raw.rstrip("\n")
if line.startswith("diff --git "):
current_path = None
new_line_no = None
position = 0
in_hunk = False
continue
if line.startswith("+++ b/"):
current_path = line[6:]
if current_path not in position_map:
position_map[current_path] = {}
continue
if line.startswith("@@ "):
match = re.match(r"@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@", line)
if match:
new_line_no = int(match.group(1))
in_hunk = True
continue
if not in_hunk or current_path is None:
continue
if not line:
continue
prefix = line[0]
if prefix not in (" ", "+", "-"):
continue
position += 1
if prefix == " ":
position_map[current_path][new_line_no] = position
new_line_no += 1
elif prefix == "+":
position_map[current_path][new_line_no] = position
new_line_no += 1
elif prefix == "-":
continue
return position_map
def build_payload(commit_id, path, position, title, body):
comment_body = "**%s**" % title
if body:
comment_body += "\n\n%s" % body
return {
"body": comment_body,
"commit_id": commit_id,
"path": path,
"position": position,
}
def post_inline_comment(api_url, token, payload):
data = json.dumps(payload).encode("utf-8")
req = urllib.request.Request(
api_url,
data=data,
method="POST",
headers={
"Authorization": "Bearer %s" % token,
"Accept": "application/vnd.github+json",
"Content-Type": "application/json",
},
)
try:
with urllib.request.urlopen(req) as resp:
return True, "status=%s" % resp.status
except urllib.error.HTTPError as e:
try:
err = e.read().decode("utf-8", errors="ignore")
except Exception:
err = ""
return False, "status=%s body=%s" % (e.code, err)
def main():
findings_path = os.environ.get("FINDINGS_PATH", "findings.json")
diff_path = os.environ.get("PR_DIFF_PATH", "pr.diff")
api_url = os.environ["INLINE_COMMENT_API_URL"]
token = os.environ["GITHUB_TOKEN"]
commit_id = os.environ["GITHUB_HEAD_SHA"]
findings = read_findings(findings_path)
if not findings:
print("no findings found, skip inline comments")
return 0
position_map = build_position_map(diff_path)
created = 0
for finding in findings:
if finding.get("severity") != "critical":
continue
if not finding.get("inline_candidate"):
continue
path = finding.get("path")
start_line = finding.get("start_line")
end_line = finding.get("end_line")
title = finding.get("title", "").strip()
body = finding.get("body", "").strip()
if not path or not start_line or not title:
print("skip finding without required fields: %s" % json.dumps(finding, ensure_ascii=False))
continue
start_line = int(start_line)
end_line = int(end_line) if end_line else None
target_line = start_line
file_positions = position_map.get(path, {})
position = file_positions.get(target_line)
if position is None and end_line and end_line > start_line:
position = file_positions.get(end_line)
if position is not None:
target_line = end_line
target = "%s:%s-%s" % (path, start_line, end_line) if end_line and end_line > start_line else "%s:%s" % (path, start_line)
if position is None:
print("skip finding without diff position: %s" % target)
continue
payload = build_payload(commit_id, path, position, title, body)
ok, msg = post_inline_comment(api_url, token, payload)
if ok:
created += 1
print("inline comment created: %s position=%s %s" % (target, position, msg))
else:
print("inline comment failed: %s position=%s %s" % (target, position, msg))
print("inline comments created: %s" % created)
return 0
if __name__ == "__main__":
sys.exit(main())