[llvm] github-automation: Use a single comment for team mentions on pull requests (PR #66037)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 12 10:45:33 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-github-workflow
            
<details>
<summary>Changes</summary>
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.
--
Full diff: https://github.com/llvm/llvm-project/pull/66037.diff

2 Files Affected:

- (modified) .github/workflows/pr-subscriber.yml (+8) 
- (modified) llvm/utils/git/github-automation.py (+33-12) 


<pre>
diff --git a/.github/workflows/pr-subscriber.yml b/.github/workflows/pr-subscriber.yml
index 3b18c8b35e97d1a..60c884cb172c2a2 100644
--- a/.github/workflows/pr-subscriber.yml
+++ b/.github/workflows/pr-subscriber.yml
@@ -9,6 +9,14 @@ on:
 permissions:
   contents: read
 
+concurrency:
+  # Ideally, we would use the PR number in the concurrency group, but we don't
+  # have access to it here.  We need to ensure only one job is running for
+  # each PR at a time, because there is a potential race condition when
+  # updating the issue comment.
+  group: "PR Subscriber"
+  cancel-in-progress: false
+
 jobs:
   auto-subscribe:
     runs-on: ubuntu-latest
diff --git a/llvm/utils/git/github-automation.py b/llvm/utils/git/github-automation.py
index c4c4848fbc5f8bc..cbad9bd829beffb 100755
--- a/llvm/utils/git/github-automation.py
+++ b/llvm/utils/git/github-automation.py
@@ -10,6 +10,7 @@
 
 import argparse
 from git import Repo  # type: ignore
+import html
 import github
 import os
 import re
@@ -95,6 +96,13 @@ def __init__(self, token: str, repo: str, pr_number: int, label_name: str):
         self.org = github.Github(token).get_organization(self.repo.organization.login)
         self.pr = self.repo.get_issue(pr_number).as_pull_request()
         self._team_name = "pr-subscribers-{}".format(label_name).lower()
+        self.COMMENT_TAG = "<!--LLVM PR SUMMARY COMMENT-->\n"
+
+    def get_summary_comment(self) -> github.IssueComment.IssueComment:
+        for comment in self.pr.as_issue().get_comments():
+            if self.COMMENT_TAG in comment.body:
+                return comment
+        return None
 
     def run(self) -> bool:
         patch = None
@@ -129,22 +137,35 @@ def run(self) -> bool:
         patch_link = f"Full diff: {self.pr.diff_url}\n"
         if len(patch) > DIFF_LIMIT:
             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"
-            diff_stats = diff_stats[0:DIFF_LIMIT] + "...\n<truncated>\n"
+            diff_stats = html.escape(diff_stats[0:DIFF_LIMIT]) + "...\n<truncated>\n"
         diff_stats += "</pre>"
+        team_mention = "@llvm/{}".format(team.slug)
 
         body = self.pr.body
-        comment = (
-            "@llvm/{}".format(team.slug)
-            + "\n\n<details>\n"
-            + f"<summary>Changes</summary>\n\n"
-            + f"{body}\n--\n"
-            + patch_link
-            + "\n"
-            + f"{diff_stats}\n\n"
-            + "</details>"
-        )
+        comment = f"""
+{self.COMMENT_TAG}
+{team_mention}
+            
+<details>
+<summary>Changes</summary>
+{body}
+--
+{patch_link}
+{diff_stats}
+</details>
+"""
 
-        self.pr.as_issue().create_comment(comment)
+        summary_comment = self.get_summary_comment()
+        if not summary_comment:
+            self.pr.as_issue().create_comment(comment)
+        elif team_mention + "\n" in summary_comment.body:
+            print("Team {} already mentioned.".format(team.slug))
+        else:
+            summary_comment.edit(
+                summary_comment.body.replace(
+                    self.COMMENT_TAG, self.COMMENT_TAG + team_mention + "\n"
+                )
+            )
         return True
 
     def _get_curent_team(self) -> Optional[github.Team.Team]:
</pre>
</details>


https://github.com/llvm/llvm-project/pull/66037


More information about the llvm-commits mailing list