|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
| 4 | +import json |
| 5 | +import shlex |
| 6 | +import subprocess |
| 7 | +import tempfile |
4 | 8 | #
|
5 | 9 | # Licensed to the Apache Software Foundation (ASF) under one or more
|
6 | 10 | # contributor license agreements. See the NOTICE file distributed with
|
@@ -35,6 +39,42 @@ def prompt_for_user():
|
35 | 39 | return clean_input
|
36 | 40 |
|
37 | 41 |
|
| 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 | + |
38 | 78 | if __name__ == "__main__":
|
39 | 79 | print("Utility to help generate 'Reviewers' string for Pull Requests. Use Ctrl+D or Ctrl+C to exit")
|
40 | 80 |
|
@@ -87,9 +127,12 @@ def prompt_for_user():
|
87 | 127 | continue
|
88 | 128 |
|
89 | 129 | 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") |
95 | 133 |
|
| 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