Skip to content

Commit 4085cb3

Browse files
authored
Improve the pull-request subcription notification format by adding the description and files statistics (#65828)
1 parent 67635b6 commit 4085cb3

File tree

1 file changed

+62
-16
lines changed

1 file changed

+62
-16
lines changed

llvm/utils/git/github-automation.py

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ def run(self) -> bool:
6565
return False
6666

6767

68+
def human_readable_size(size, decimal_places=2):
69+
for unit in ["B", "KiB", "MiB", "GiB", "TiB", "PiB"]:
70+
if size < 1024.0 or unit == "PiB":
71+
break
72+
size /= 1024.0
73+
return f"{size:.{decimal_places}f} {unit}"
74+
75+
6876
class PRSubscriber:
6977
@property
7078
def team_name(self) -> str:
@@ -77,23 +85,62 @@ def __init__(self, token: str, repo: str, pr_number: int, label_name: str):
7785
self._team_name = "pr-subscribers-{}".format(label_name).lower()
7886

7987
def run(self) -> bool:
80-
for team in self.org.get_teams():
81-
if self.team_name != team.name.lower():
82-
continue
83-
try:
84-
# GitHub limits comments to 65,536 characters, let's limit our comments to 20,000.
85-
patch = requests.get(self.pr.diff_url).text[0:20000]
86-
except:
87-
patch = ""
88-
comment = (
89-
"@llvm/{}".format(team.slug)
90-
+ "\n\n<details><summary>Changes</summary><pre>\n"
91-
+ patch
92-
+ "\n</pre></details>"
93-
)
94-
self.pr.as_issue().create_comment(comment)
88+
patch = None
89+
team = self._get_curent_team()
90+
if not team:
91+
print(f"couldn't find team named {self.team_name}")
92+
return False
93+
94+
# Get statistics for each file
95+
diff_stats = f"{self.pr.changed_files} Files Affected:\n\n"
96+
for file in self.pr.get_files():
97+
diff_stats += f"- ({file.status}) {file.filename} ("
98+
if file.additions:
99+
diff_stats += f"+{file.additions}"
100+
if file.deletions:
101+
diff_stats += f"-{file.deletions}"
102+
diff_stats += ") "
103+
if file.status == "renamed":
104+
print(f"(from {file.previous_filename})")
105+
diff_stats += "\n"
106+
diff_stats += "\n"
107+
108+
# Get the diff
109+
try:
110+
patch = requests.get(self.pr.diff_url).text
111+
except:
112+
patch = ""
113+
diff_stats += "\n<pre>\n" + patch
114+
115+
# GitHub limits comments to 65,536 characters, let's limit the diff to 20kB.
116+
DIFF_LIMIT = 20 * 1024
117+
patch_link = f"Full diff: {self.pr.diff_url}\n"
118+
if len(patch) > DIFF_LIMIT:
119+
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"
120+
diff_stats = diff_stats[0:DIFF_LIMIT] + "...\n<truncated>\n"
121+
diff_stats += "</pre>"
122+
123+
body = self.pr.body
124+
comment = (
125+
"@llvm/{}".format(team.slug)
126+
+ "\n\n<details>\n"
127+
+ f"<summary>Changes</summary>\n\n"
128+
+ f"{body}\n--\n"
129+
+ patch_link
130+
+ "\n"
131+
+ f"{diff_stats}\n\n"
132+
+ "</details>"
133+
)
134+
135+
self.pr.as_issue().create_comment(comment)
95136
return True
96137

138+
def _get_curent_team(self) -> Optional[github.Team.Team]:
139+
for team in self.org.get_teams():
140+
if self.team_name == team.name.lower():
141+
return team
142+
return None
143+
97144

98145
def setup_llvmbot_git(git_dir="."):
99146
"""
@@ -199,7 +246,6 @@ def extract_commit_hash(arg: str):
199246

200247

201248
class ReleaseWorkflow:
202-
203249
CHERRY_PICK_FAILED_LABEL = "release:cherry-pick-failed"
204250

205251
"""

0 commit comments

Comments
 (0)