Skip to content

Commit 512a646

Browse files
committed
github-automation: Use a single comment for team mentions on pull requests
This will reduce the number of notifications created when a pull request label is added. Each team will only get a notification when their team's label is added and not when other teams' labels are added.
1 parent c6a33ff commit 512a646

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

.github/workflows/pr-subscriber.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ on:
99
permissions:
1010
contents: read
1111

12+
concurrency:
13+
# Ideally, we would use the PR number in the concurrency group, but we don't
14+
# have access to it here. We need to ensure only one job is running for
15+
# each PR at a time, because there is a potential race condition when
16+
# updating the issue comment.
17+
group: "PR Subscriber"
18+
cancel-in-progress: false
19+
1220
jobs:
1321
auto-subscribe:
1422
runs-on: ubuntu-latest

llvm/utils/git/github-automation.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import argparse
1212
from git import Repo # type: ignore
13+
import html
1314
import github
1415
import os
1516
import re
@@ -97,6 +98,13 @@ def __init__(self, token: str, repo: str, pr_number: int, label_name: str):
9798
self._team_name = "pr-subscribers-{}".format(
9899
label_name.replace("+", "x")
99100
).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
100108

101109
def run(self) -> bool:
102110
patch = None
@@ -121,7 +129,7 @@ def run(self) -> bool:
121129

122130
# Get the diff
123131
try:
124-
patch = requests.get(self.pr.diff_url).text
132+
patch = html.escape(requests.get(self.pr.diff_url).text)
125133
except:
126134
patch = ""
127135
diff_stats += "\n<pre>\n" + patch
@@ -131,22 +139,35 @@ def run(self) -> bool:
131139
patch_link = f"Full diff: {self.pr.diff_url}\n"
132140
if len(patch) > DIFF_LIMIT:
133141
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"
134-
diff_stats = diff_stats[0:DIFF_LIMIT] + "...\n<truncated>\n"
142+
diff_stats = html.escape(diff_stats[0:DIFF_LIMIT]) + "...\n<truncated>\n"
135143
diff_stats += "</pre>"
144+
team_mention = "@llvm/{}".format(team.slug)
136145

137146
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+
"""
148159

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+
)
150171
return True
151172

152173
def _get_curent_team(self) -> Optional[github.Team.Team]:

0 commit comments

Comments
 (0)