Skip to content

Commit d964574

Browse files
authored
KAFKA-18942: Add reviewers to PR body with committer-tools (apache#19168)
Allow `reviewers.py` to set the "Reviewers:" line in a PR body. Reviewers: Chia-Ping Tsai <[email protected]>, David Arthur <[email protected]>
1 parent c07c59a commit d964574

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

committer-tools/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ See: https://cli.github.com/
4646
brew install gh
4747
```
4848

49-
## Find Reviewers
49+
## Find Reviewers and Update to PR body
5050

5151
The reviewers.py script is used to simplify the process of producing our "Reviewers:"
52-
Git trailer. It parses the Git log to gather a set of "Authors" and "Reviewers".
53-
Some simple string prefix matching is done to find candidates.
52+
Git trailer to PR body. It parses the Git log to gather a set of "Authors" and "Reviewers".
53+
Some simple string prefix matching is done to find candidates.
54+
After entering the pull request number, the script updates the "Reviewers:" trailer accordingly.
55+
If the PR body already contains a "Reviewers:" trailer, the script replaces it with the updated list of reviewers.
5456

5557
Usage:
5658

committer-tools/reviewers.py

+48-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4+
import json
5+
import shlex
6+
import subprocess
7+
import tempfile
48
#
59
# Licensed to the Apache Software Foundation (ASF) under one or more
610
# contributor license agreements. See the NOTICE file distributed with
@@ -35,6 +39,42 @@ def prompt_for_user():
3539
return clean_input
3640

3741

42+
def update_trailers(body, trailer):
43+
with tempfile.NamedTemporaryFile() as fp:
44+
fp.write(body.encode())
45+
fp.flush()
46+
cmd = f"git interpret-trailers --if-exists replace --trailer '{trailer}' {fp.name} "
47+
p = subprocess.run(shlex.split(cmd), capture_output=True, text=True)
48+
fp.close()
49+
50+
return p.stdout
51+
52+
53+
def append_message_to_pr_body(pr: int , message: str):
54+
try:
55+
pr_url = f"https://github.com/apache/kafka/pull/{pr}"
56+
cmd_get_pr = f"gh pr view {pr_url} --json title,body"
57+
result = subprocess.run(shlex.split(cmd_get_pr), capture_output=True, text=True, check=True)
58+
current_pr_body = json.loads(result.stdout).get("body", {}).strip() + "\n"
59+
pr_title = json.loads(result.stdout).get("title", {})
60+
updated_pr_body = update_trailers(current_pr_body, message)
61+
except subprocess.CalledProcessError as e:
62+
print("Failed to retrieve PR body:", e.stderr)
63+
return
64+
65+
print(f"""New PR body will be:\n\n---\n{updated_pr_body}---\n""")
66+
choice = input(f'Update the body of "{pr_title}"? (y/n): ').strip().lower()
67+
if choice in ['n', 'no']:
68+
return
69+
70+
try:
71+
cmd_edit_body = f"gh pr edit {pr_url} --body {shlex.quote(updated_pr_body)}"
72+
subprocess.run(shlex.split(cmd_edit_body), check=True)
73+
print("PR body updated successfully!")
74+
except subprocess.CalledProcessError as e:
75+
print("Failed to update PR body:", e.stderr)
76+
77+
3878
if __name__ == "__main__":
3979
print("Utility to help generate 'Reviewers' string for Pull Requests. Use Ctrl+D or Ctrl+C to exit")
4080

@@ -87,9 +127,12 @@ def prompt_for_user():
87127
continue
88128

89129
if selected_reviewers:
90-
out = "\n\nReviewers: "
91-
out += ", ".join([f"{name} <{email}>" for name, email, _ in selected_reviewers])
92-
out += "\n"
93-
print(out)
94-
130+
reviewer_message = "Reviewers: "
131+
reviewer_message += ", ".join([f"{name} <{email}>" for name, email, _ in selected_reviewers])
132+
print(f"\n\n{reviewer_message}\n")
95133

134+
try:
135+
pr_number = int(input("\nPull Request (Ctrl+D or Ctrl+C to skip): "))
136+
append_message_to_pr_body(pr_number, reviewer_message)
137+
except (EOFError, KeyboardInterrupt):
138+
exit(0)

0 commit comments

Comments
 (0)