|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2024 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 | +# http://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 | +"""Fetches and formats review comments from a GitHub Pull Request.""" |
| 17 | + |
| 18 | +import argparse |
| 19 | +import os |
| 20 | +import sys |
| 21 | +import firebase_github # Assumes firebase_github.py is in the same directory or python path |
| 22 | + |
| 23 | +# Attempt to configure logging for firebase_github if absl is available |
| 24 | +try: |
| 25 | + from absl import logging as absl_logging |
| 26 | + # Set verbosity for absl logging if you want to see logs from firebase_github |
| 27 | + # absl_logging.set_verbosity(absl_logging.INFO) |
| 28 | +except ImportError: |
| 29 | + # If absl is not used, standard logging can be configured if needed |
| 30 | + # import logging as std_logging |
| 31 | + # std_logging.basicConfig(level=std_logging.INFO) |
| 32 | + pass # firebase_github.py uses absl.logging.info, so this won't redirect. |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + # Default owner and repo from firebase_github, ensuring it's loaded. |
| 37 | + default_owner = firebase_github.OWNER |
| 38 | + default_repo = firebase_github.REPO |
| 39 | + |
| 40 | + parser = argparse.ArgumentParser( |
| 41 | + description="Fetch review comments from a GitHub PR and format for use with Jules.", |
| 42 | + formatter_class=argparse.RawTextHelpFormatter # To preserve formatting in help text |
| 43 | + ) |
| 44 | + parser.add_argument( |
| 45 | + "--pull_number", |
| 46 | + type=int, |
| 47 | + required=True, |
| 48 | + help="Pull request number." |
| 49 | + ) |
| 50 | + parser.add_argument( |
| 51 | + "--owner", |
| 52 | + type=str, |
| 53 | + default=default_owner, |
| 54 | + help=f"Repository owner. Defaults to '{default_owner}' (from firebase_github.py)." |
| 55 | + ) |
| 56 | + parser.add_argument( |
| 57 | + "--repo", |
| 58 | + type=str, |
| 59 | + default=default_repo, |
| 60 | + help=f"Repository name. Defaults to '{default_repo}' (from firebase_github.py)." |
| 61 | + ) |
| 62 | + parser.add_argument( |
| 63 | + "--token", |
| 64 | + type=str, |
| 65 | + default=os.environ.get("GITHUB_TOKEN"), |
| 66 | + help="GitHub token. Can also be set via GITHUB_TOKEN environment variable." |
| 67 | + ) |
| 68 | + |
| 69 | + args = parser.parse_args() |
| 70 | + |
| 71 | + if not args.token: |
| 72 | + sys.stderr.write("Error: GitHub token not provided. Set GITHUB_TOKEN environment variable or use --token argument.\n") |
| 73 | + sys.exit(1) |
| 74 | + |
| 75 | + # Update the repository details in firebase_github module if different from default |
| 76 | + if args.owner != firebase_github.OWNER or args.repo != firebase_github.REPO: |
| 77 | + repo_url = f"https://github.com/{args.owner}/{args.repo}" |
| 78 | + if not firebase_github.set_repo_url(repo_url): |
| 79 | + sys.stderr.write(f"Error: Invalid repository URL format for {args.owner}/{args.repo}. Expected format: https://github.com/owner/repo\n") |
| 80 | + sys.exit(1) |
| 81 | + # Using print to stderr for info, as absl logging might not be configured here for this script's own messages. |
| 82 | + print(f"Targeting repository: {firebase_github.OWNER}/{firebase_github.REPO}", file=sys.stderr) |
| 83 | + |
| 84 | + |
| 85 | + print(f"Fetching review comments for PR #{args.pull_number} from {firebase_github.OWNER}/{firebase_github.REPO}...", file=sys.stderr) |
| 86 | + |
| 87 | + comments = firebase_github.get_pull_request_review_comments(args.token, args.pull_number) |
| 88 | + |
| 89 | + if not comments: # This will be true if list is empty (no comments or error in fetching first page) |
| 90 | + print(f"No review comments found for PR #{args.pull_number}, or an error occurred during fetching.", file=sys.stderr) |
| 91 | + # If firebase_github.py's get_pull_request_review_comments logs errors, those might provide more details. |
| 92 | + return # Exit gracefully if no comments |
| 93 | + |
| 94 | + # Output actual data to stdout |
| 95 | + print("\n--- Review Comments ---") |
| 96 | + for comment in comments: |
| 97 | + user = comment.get("user", {}).get("login", "Unknown user") |
| 98 | + path = comment.get("path", "N/A") |
| 99 | + line = comment.get("line", "N/A") |
| 100 | + body = comment.get("body", "").strip() # Strip whitespace from comment body |
| 101 | + diff_hunk = comment.get("diff_hunk", "N/A") |
| 102 | + html_url = comment.get("html_url", "N/A") |
| 103 | + |
| 104 | + # Only print comments that have a body |
| 105 | + if not body: |
| 106 | + continue |
| 107 | + |
| 108 | + print(f"Comment by: {user}") |
| 109 | + print(f"File: {path}") |
| 110 | + # The 'line' field in GitHub's API for PR review comments refers to the line number in the diff. |
| 111 | + # 'original_line' refers to the line number in the file at the time the comment was made. |
| 112 | + # 'start_line' and 'original_start_line' for multi-line comments. |
| 113 | + # For simplicity, we use 'line'. |
| 114 | + print(f"Line in diff: {line}") |
| 115 | + print(f"URL: {html_url}") |
| 116 | + print("--- Diff Hunk ---") |
| 117 | + print(diff_hunk) |
| 118 | + print("--- Comment ---") |
| 119 | + print(body) |
| 120 | + print("----------------------------------------\n") |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main() |
0 commit comments