10
10
11
11
import argparse
12
12
from git import Repo # type: ignore
13
+ import html
13
14
import github
14
15
import os
15
16
import re
@@ -97,6 +98,13 @@ def __init__(self, token: str, repo: str, pr_number: int, label_name: str):
97
98
self ._team_name = "pr-subscribers-{}" .format (
98
99
label_name .replace ("+" , "x" )
99
100
).lower ()
101
+ self .COMMENT_TAG = "<!--LLVM PR SUMMARY COMMENT-->\n "
102
+
103
+ def get_summary_comment (self ) -> github .IssueComment .IssueComment :
104
+ for comment in self .pr .as_issue ().get_comments ():
105
+ if self .COMMENT_TAG in comment .body :
106
+ return comment
107
+ return None
100
108
101
109
def run (self ) -> bool :
102
110
patch = None
@@ -121,7 +129,7 @@ def run(self) -> bool:
121
129
122
130
# Get the diff
123
131
try :
124
- patch = requests .get (self .pr .diff_url ).text
132
+ patch = html . escape ( requests .get (self .pr .diff_url ).text )
125
133
except :
126
134
patch = ""
127
135
diff_stats += "\n <pre>\n " + patch
@@ -131,22 +139,35 @@ def run(self) -> bool:
131
139
patch_link = f"Full diff: { self .pr .diff_url } \n "
132
140
if len (patch ) > DIFF_LIMIT :
133
141
patch_link = f"\n Patch is { human_readable_size (len (patch ))} , truncated to { human_readable_size (DIFF_LIMIT )} below, full version: { self .pr .diff_url } \n "
134
- diff_stats = diff_stats [0 :DIFF_LIMIT ] + "...\n <truncated>\n "
142
+ diff_stats = html . escape ( diff_stats [0 :DIFF_LIMIT ]) + "...\n <truncated>\n "
135
143
diff_stats += "</pre>"
144
+ team_mention = "@llvm/{}" .format (team .slug )
136
145
137
146
body = self .pr .body
138
- comment = (
139
- "@llvm/{}" .format (team .slug )
140
- + "\n \n <details>\n "
141
- + f"<summary>Changes</summary>\n \n "
142
- + f"{ body } \n --\n "
143
- + patch_link
144
- + "\n "
145
- + f"{ diff_stats } \n \n "
146
- + "</details>"
147
- )
147
+ comment = f"""
148
+ { self .COMMENT_TAG }
149
+ { team_mention }
150
+
151
+ <details>
152
+ <summary>Changes</summary>
153
+ { body }
154
+ --
155
+ { patch_link }
156
+ { diff_stats }
157
+ </details>
158
+ """
148
159
149
- self .pr .as_issue ().create_comment (comment )
160
+ summary_comment = self .get_summary_comment ()
161
+ if not summary_comment :
162
+ self .pr .as_issue ().create_comment (comment )
163
+ elif team_mention + "\n " in summary_comment .body :
164
+ print ("Team {} already mentioned." .format (team .slug ))
165
+ else :
166
+ summary_comment .edit (
167
+ summary_comment .body .replace (
168
+ self .COMMENT_TAG , self .COMMENT_TAG + team_mention + "\n "
169
+ )
170
+ )
150
171
return True
151
172
152
173
def _get_curent_team (self ) -> Optional [github .Team .Team ]:
0 commit comments