Skip to content

github-automation: Use a single comment for team mentions on pull requests #66037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2023

Conversation

tstellar
Copy link
Collaborator

@tstellar tstellar commented Sep 12, 2023

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.

@tstellar tstellar requested a review from a team as a code owner September 12, 2023 02:09
@tstellar tstellar requested a review from jh7370 September 12, 2023 02:10
@llvmbot
Copy link
Member

llvmbot commented Sep 12, 2023

@llvm/pr-subscribers-github-workflow

Changes

…uests

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 (+19-2)
diff --git a/.github/workflows/pr-subscriber.yml b/.github/workflows/pr-subscriber.yml
index 3b18c8b35e97d1a..14e6cb33c57f9f6 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 potentaill 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 7df20ea8457155b..a7b88ed383a1238 100755
--- a/llvm/utils/git/github-automation.py
+++ b/llvm/utils/git/github-automation.py
@@ -83,6 +83,15 @@ 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 = "\n"
+
+    def get_summary_comment(self) -> github.IssueComment.IssueComment:
+        for comment in self.pr.as_issue().get_comments():
+            if not self.COMMENT_TAG in comment.body:
+                continue
+            return comment
+        return None
+
 
     def run(self) -> bool:
         patch = None
@@ -119,10 +128,12 @@ def run(self) -> bool:
             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\n"
         diff_stats += "
"
  •    team_mention = "@llvm/{}".format(team.slug)
    
       body = self.pr.body
       comment = (
    
  •        "@llvm/{}".format(team.slug)
    
  •        self.COMMENT_TAG
    
  •        + team_mention
           + "\n\n<details>\n"
           + f"<summary>Changes</summary>\n\n"
           + f"{body}\n--\n"
    

@@ -132,7 +143,13 @@ def run(self) -> bool:
+ "

"
)

  •    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]:

@joker-eph
Copy link
Collaborator

Please fix the title wrapping
(it's an annoying GitHub things, I don't think we can control it, maybe another action to write?)

@tstellar tstellar changed the title github-automation: Use a single comment for team mentions on pull req… github-automation: Use a single comment for team mentions on pull requests Sep 12, 2023
@tstellar tstellar force-pushed the single-mention-comment branch from 7e08d10 to e483769 Compare September 12, 2023 06:22
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo a minor suggestion

Copy link
Collaborator

@tru tru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, except the black formatter failing.

@cor3ntin
Copy link
Contributor

Could we replace @ in the description by something else so that people don't get extra mentions?
all the bugzilla bugs have a CC fields that ping everyone in the issue, putting that in the description is noisy for some folks

@Endilll
Copy link
Contributor

Endilll commented Sep 12, 2023

all the bugzilla bugs have a CC fields that ping everyone in the issue

Quite often it's not helpful. On Clang side it's not helpful at all.

@cor3ntin
Copy link
Contributor

cor3ntin commented Sep 12, 2023

Test @Endilll

@tstellar
Copy link
Collaborator Author

Could we replace @ in the description by something else so that people don't get extra mentions? all the bugzilla bugs have a CC fields that ping everyone in the issue, putting that in the description is noisy for some folks

This is the only way that I know of to subscribe someone to a pull request. However, I've tested this out and people only get notifications for the first edit that mentions a team they are on. So if you have already been mentioned and another team gets mentioned, you won't see a notification for that.

@cor3ntin
Copy link
Contributor

@tstellar the issue is that people get mentioned twice because of the content of details, not because of the intentional @llvm/ mention. I'm working on fixing that

diff_stats += "</pre>"
team_mention = "@llvm/{}".format(team.slug)

body = self.pr.body
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
body = self.pr.body
body = self.pr.body.replace('@', '@<!-- -->')

From github/markup#1168

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed on Discord, I don't think this is an issue for pull requests.

@llvm llvm deleted a comment from llvmbot Sep 12, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 12, 2023

@llvm/pr-subscribers-github-workflow

Changes 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//pull/66037.diff

2 Files Affected:

  • (modified) .github/workflows/pr-subscriber.yml (+8)
  • (modified) llvm/utils/git/github-automation.py (+33-12)
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]:

@tstellar
Copy link
Collaborator Author

@cor3ntin Thanks for the feedback, this looks really good now. I'll push this later today if there are no other comments.

@cor3ntin
Copy link
Contributor

Thanks, lgtm!

…uests

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.
@tstellar tstellar force-pushed the single-mention-comment branch from b6a7841 to 512a646 Compare September 12, 2023 20:12
@tstellar tstellar merged commit 64751ea into llvm:main Sep 12, 2023
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
…uests (llvm#66037)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants