Skip to content

Commit e8e769c

Browse files
committed
* Escape #
* Escape PR description * Trunkate the list of files if it's > 20K * Color the diff on github
1 parent bc85028 commit e8e769c

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

llvm/utils/git/github-automation.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _get_curent_team(team_name, teams) -> Optional[github.Team.Team]:
5151
def escape_description(str):
5252
# https://github.com/github/markup/issues/1168#issuecomment-494946168
5353
str = html.escape(str, False)
54-
return str.replace("@", "@<!-- -->")
54+
return str.replace("@", "@<!-- -->").replace("#", "#<!-- -->")
5555

5656

5757
class IssueSubscriber:
@@ -123,6 +123,11 @@ def run(self) -> bool:
123123
print(f"couldn't find team named {self.team_name}")
124124
return False
125125

126+
# GitHub limits comments to 65,536 characters, let's limit the diff
127+
# and the file list to 20kB each.
128+
STAT_LIMIT = 20 * 1024
129+
DIFF_LIMIT = 20 * 1024
130+
126131
# Get statistics for each file
127132
diff_stats = f"{self.pr.changed_files} Files Affected:\n\n"
128133
for file in self.pr.get_files():
@@ -133,37 +138,40 @@ def run(self) -> bool:
133138
diff_stats += f"-{file.deletions}"
134139
diff_stats += ") "
135140
if file.status == "renamed":
136-
print(f"(from {file.previous_filename})")
141+
print(f"(from {file.previous_filename})"
137142
diff_stats += "\n"
138-
diff_stats += "\n"
143+
if len(diff_stats) > STAT_LIMIT)
144+
break
139145

140146
# Get the diff
141147
try:
142148
patch = html.escape(requests.get(self.pr.diff_url).text)
143149
except:
144150
patch = ""
145-
diff_stats += "\n<pre>\n" + patch
146151

147152
# GitHub limits comments to 65,536 characters, let's limit the diff to 20kB.
148-
DIFF_LIMIT = 20 * 1024
149153
patch_link = f"Full diff: {self.pr.diff_url}\n"
150154
if len(patch) > DIFF_LIMIT:
151155
patch_link = f"\nPatch is {human_readable_size(len(patch))}, truncated to {human_readable_size(DIFF_LIMIT)} below, full version: {self.pr.diff_url}\n"
152-
diff_stats = html.escape(diff_stats[0:DIFF_LIMIT]) + "...\n<truncated>\n"
153-
diff_stats += "</pre>"
156+
patch = html.escape(patch[0:DIFF_LIMIT]) + "...\n<truncated>\n"
154157
team_mention = "@llvm/{}".format(team.slug)
155158

156-
body = self.pr.body
159+
body = escape_description(self.pr.body)
157160
comment = f"""
158161
{self.COMMENT_TAG}
159162
{team_mention}
160-
163+
161164
<details>
162165
<summary>Changes</summary>
163166
{body}
164167
--
165168
{patch_link}
169+
166170
{diff_stats}
171+
172+
<pre lang="diff">
173+
{patch}
174+
</pre>
167175
</details>
168176
"""
169177

0 commit comments

Comments
 (0)