Skip to content
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

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

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/pr-subscriber.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 34 additions & 13 deletions llvm/utils/git/github-automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import argparse
from git import Repo # type: ignore
import html
import github
import os
import re
Expand Down Expand Up @@ -97,6 +98,13 @@ def __init__(self, token: str, repo: str, pr_number: int, label_name: str):
self._team_name = "pr-subscribers-{}".format(
label_name.replace("+", "x")
).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
Expand All @@ -121,7 +129,7 @@ def run(self) -> bool:

# Get the diff
try:
patch = requests.get(self.pr.diff_url).text
patch = html.escape(requests.get(self.pr.diff_url).text)
except:
patch = ""
diff_stats += "\n<pre>\n" + patch
Expand All @@ -131,22 +139,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
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.

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]:
Expand Down