|
| 1 | +# |
| 2 | +# Copyright 2026 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +import json |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | +from datetime import datetime |
| 21 | + |
| 22 | +def run_command(command): |
| 23 | + try: |
| 24 | + result = subprocess.run( |
| 25 | + command, |
| 26 | + check=True, |
| 27 | + capture_output=True, |
| 28 | + text=True, |
| 29 | + shell=True |
| 30 | + ) |
| 31 | + return result.stdout.strip() |
| 32 | + except subprocess.CalledProcessError as e: |
| 33 | + # Check if it's the specific "no pull requests" error |
| 34 | + if "no pull requests found" in e.stderr: |
| 35 | + print("No pull request found for the current branch.", file=sys.stderr) |
| 36 | + sys.exit(0) # Exit gracefully with 0 or 1? Standard practice is 1 if it failed to do what was asked. |
| 37 | + # But for a skill finding "nothing", maybe 0 is okay? |
| 38 | + # Let's stick to 1 but with a clean message. |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + print(f"Error running command: {command}", file=sys.stderr) |
| 42 | + print(f"Stderr: {e.stderr}", file=sys.stderr) |
| 43 | + sys.exit(1) |
| 44 | + |
| 45 | +def format_date(iso_date): |
| 46 | + try: |
| 47 | + dt = datetime.fromisoformat(iso_date.replace("Z", "+00:00")) |
| 48 | + return dt.strftime("%Y-%m-%d %H:%M") |
| 49 | + except Exception: |
| 50 | + return iso_date |
| 51 | + |
| 52 | +def main(): |
| 53 | + # Fetch PR data including comments and reviews |
| 54 | + cmd = "gh pr view --json title,url,number,state,comments,reviews,latestReviews" |
| 55 | + |
| 56 | + # We rely on run_command to handle the subprocess exit |
| 57 | + json_output = run_command(cmd) |
| 58 | + |
| 59 | + try: |
| 60 | + pr_data = json.loads(json_output) |
| 61 | + except json.JSONDecodeError: |
| 62 | + print("Failed to decode JSON from gh output.", file=sys.stderr) |
| 63 | + sys.exit(1) |
| 64 | + |
| 65 | + print(f"# PR #{pr_data.get('number')}: {pr_data.get('title')}") |
| 66 | + print(f"**URL:** {pr_data.get('url')}") |
| 67 | + print(f"**State:** {pr_data.get('state')}\n") |
| 68 | + |
| 69 | + print("## Reviews") |
| 70 | + reviews = pr_data.get('latestReviews', []) |
| 71 | + if not reviews: |
| 72 | + print("No reviews found.") |
| 73 | + else: |
| 74 | + for review in reviews: |
| 75 | + state = review.get('state') |
| 76 | + author = review.get('author', {}).get('login', 'Unknown') |
| 77 | + date = format_date(review.get('submittedAt', '')) |
| 78 | + print(f"- **{author}**: {state} ({date})") |
| 79 | + print("\n" + "-"*40 + "\n") |
| 80 | + |
| 81 | + print("## Comments") |
| 82 | + |
| 83 | + comments = pr_data.get('comments', []) |
| 84 | + if not comments: |
| 85 | + print("No comments found.") |
| 86 | + else: |
| 87 | + for comment in comments: |
| 88 | + author = comment.get('author', {}).get('login', 'Unknown') |
| 89 | + body = comment.get('body', '').strip() |
| 90 | + date = format_date(comment.get('createdAt', '')) |
| 91 | + url = comment.get('url', '') |
| 92 | + |
| 93 | + print(f"### {author} at {date}") |
| 94 | + print(f"[Link]({url})\n") |
| 95 | + print(body) |
| 96 | + print("\n" + "-"*20 + "\n") |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments