|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import re |
| 6 | +import sys |
| 7 | +import urllib.error |
| 8 | +import urllib.request |
| 9 | + |
| 10 | + |
| 11 | +def read_findings(path): |
| 12 | + with open(path, "r", encoding="utf-8") as f: |
| 13 | + data = json.load(f) |
| 14 | + return data.get("findings", []) |
| 15 | + |
| 16 | + |
| 17 | +def build_position_map(diff_path): |
| 18 | + with open(diff_path, "r", encoding="utf-8") as f: |
| 19 | + lines = f.readlines() |
| 20 | + |
| 21 | + position_map = {} |
| 22 | + current_path = None |
| 23 | + new_line_no = None |
| 24 | + position = 0 |
| 25 | + in_hunk = False |
| 26 | + |
| 27 | + for raw in lines: |
| 28 | + line = raw.rstrip("\n") |
| 29 | + |
| 30 | + if line.startswith("diff --git "): |
| 31 | + current_path = None |
| 32 | + new_line_no = None |
| 33 | + position = 0 |
| 34 | + in_hunk = False |
| 35 | + continue |
| 36 | + |
| 37 | + if line.startswith("+++ b/"): |
| 38 | + current_path = line[6:] |
| 39 | + if current_path not in position_map: |
| 40 | + position_map[current_path] = {} |
| 41 | + continue |
| 42 | + |
| 43 | + if line.startswith("@@ "): |
| 44 | + match = re.match(r"@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@", line) |
| 45 | + if match: |
| 46 | + new_line_no = int(match.group(1)) |
| 47 | + in_hunk = True |
| 48 | + continue |
| 49 | + |
| 50 | + if not in_hunk or current_path is None: |
| 51 | + continue |
| 52 | + |
| 53 | + if not line: |
| 54 | + continue |
| 55 | + |
| 56 | + prefix = line[0] |
| 57 | + if prefix not in (" ", "+", "-"): |
| 58 | + continue |
| 59 | + |
| 60 | + position += 1 |
| 61 | + |
| 62 | + if prefix == " ": |
| 63 | + position_map[current_path][new_line_no] = position |
| 64 | + new_line_no += 1 |
| 65 | + elif prefix == "+": |
| 66 | + position_map[current_path][new_line_no] = position |
| 67 | + new_line_no += 1 |
| 68 | + elif prefix == "-": |
| 69 | + continue |
| 70 | + |
| 71 | + return position_map |
| 72 | + |
| 73 | + |
| 74 | +def build_payload(commit_id, path, position, title, body): |
| 75 | + comment_body = "**%s**" % title |
| 76 | + if body: |
| 77 | + comment_body += "\n\n%s" % body |
| 78 | + return { |
| 79 | + "body": comment_body, |
| 80 | + "commit_id": commit_id, |
| 81 | + "path": path, |
| 82 | + "position": position, |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | +def post_inline_comment(api_url, token, payload): |
| 87 | + data = json.dumps(payload).encode("utf-8") |
| 88 | + req = urllib.request.Request( |
| 89 | + api_url, |
| 90 | + data=data, |
| 91 | + method="POST", |
| 92 | + headers={ |
| 93 | + "Authorization": "Bearer %s" % token, |
| 94 | + "Accept": "application/vnd.github+json", |
| 95 | + "Content-Type": "application/json", |
| 96 | + }, |
| 97 | + ) |
| 98 | + try: |
| 99 | + with urllib.request.urlopen(req) as resp: |
| 100 | + return True, "status=%s" % resp.status |
| 101 | + except urllib.error.HTTPError as e: |
| 102 | + try: |
| 103 | + err = e.read().decode("utf-8", errors="ignore") |
| 104 | + except Exception: |
| 105 | + err = "" |
| 106 | + return False, "status=%s body=%s" % (e.code, err) |
| 107 | + |
| 108 | + |
| 109 | +def main(): |
| 110 | + findings_path = os.environ.get("FINDINGS_PATH", "findings.json") |
| 111 | + diff_path = os.environ.get("PR_DIFF_PATH", "pr.diff") |
| 112 | + api_url = os.environ["INLINE_COMMENT_API_URL"] |
| 113 | + token = os.environ["GITHUB_TOKEN"] |
| 114 | + commit_id = os.environ["GITHUB_HEAD_SHA"] |
| 115 | + |
| 116 | + findings = read_findings(findings_path) |
| 117 | + if not findings: |
| 118 | + print("no findings found, skip inline comments") |
| 119 | + return 0 |
| 120 | + |
| 121 | + position_map = build_position_map(diff_path) |
| 122 | + created = 0 |
| 123 | + |
| 124 | + for finding in findings: |
| 125 | + if finding.get("severity") != "critical": |
| 126 | + continue |
| 127 | + if not finding.get("inline_candidate"): |
| 128 | + continue |
| 129 | + |
| 130 | + path = finding.get("path") |
| 131 | + start_line = finding.get("start_line") |
| 132 | + end_line = finding.get("end_line") |
| 133 | + title = finding.get("title", "").strip() |
| 134 | + body = finding.get("body", "").strip() |
| 135 | + |
| 136 | + if not path or not start_line or not title: |
| 137 | + print("skip finding without required fields: %s" % json.dumps(finding, ensure_ascii=False)) |
| 138 | + continue |
| 139 | + |
| 140 | + start_line = int(start_line) |
| 141 | + end_line = int(end_line) if end_line else None |
| 142 | + target_line = start_line |
| 143 | + |
| 144 | + file_positions = position_map.get(path, {}) |
| 145 | + position = file_positions.get(target_line) |
| 146 | + if position is None and end_line and end_line > start_line: |
| 147 | + position = file_positions.get(end_line) |
| 148 | + if position is not None: |
| 149 | + target_line = end_line |
| 150 | + |
| 151 | + target = "%s:%s-%s" % (path, start_line, end_line) if end_line and end_line > start_line else "%s:%s" % (path, start_line) |
| 152 | + |
| 153 | + if position is None: |
| 154 | + print("skip finding without diff position: %s" % target) |
| 155 | + continue |
| 156 | + |
| 157 | + payload = build_payload(commit_id, path, position, title, body) |
| 158 | + ok, msg = post_inline_comment(api_url, token, payload) |
| 159 | + if ok: |
| 160 | + created += 1 |
| 161 | + print("inline comment created: %s position=%s %s" % (target, position, msg)) |
| 162 | + else: |
| 163 | + print("inline comment failed: %s position=%s %s" % (target, position, msg)) |
| 164 | + |
| 165 | + print("inline comments created: %s" % created) |
| 166 | + return 0 |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + sys.exit(main()) |
0 commit comments