Skip to content

Add script to fetch PR review comments #1722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
da2efa9
feat: Add script to fetch PR review comments
google-labs-jules[bot] Jun 7, 2025
0678049
feat: Enhance PR comment script with context and filters
google-labs-jules[bot] Jun 7, 2025
e84b02d
fix: Correct IndentationError in get_pr_review_comments.py
google-labs-jules[bot] Jun 7, 2025
5948b96
fix: Correct --context-lines behavior for non-line-specific comments
google-labs-jules[bot] Jun 7, 2025
565eed2
feat: Simplify diff hunk display and add comment filters
google-labs-jules[bot] Jun 7, 2025
24a03ea
refactor: Update script description and format diff hunks
google-labs-jules[bot] Jun 7, 2025
7e182aa
fix: Adjust 'next command' timestamp increment to 2 seconds
google-labs-jules[bot] Jun 7, 2025
599845b
docs: Minor textual cleanups in PR comments script
google-labs-jules[bot] Jun 7, 2025
77d1ed2
feat: Format output as Markdown for improved readability
google-labs-jules[bot] Jun 7, 2025
9cb8d42
style: Adjust Markdown headings for structure and conciseness
google-labs-jules[bot] Jun 7, 2025
203e88f
style: Adjust default context lines and Markdown spacing
google-labs-jules[bot] Jun 7, 2025
b900c7f
feat: Refactor comment filtering with new status terms and flags
google-labs-jules[bot] Jun 7, 2025
5a4010f
feat: Improve context display and suggested command robustness
google-labs-jules[bot] Jun 7, 2025
94417e7
style: Refactor hunk printing to use join for conciseness
google-labs-jules[bot] Jun 7, 2025
9312a0c
fix: Align 'since' filter and next command with observed API behavior…
google-labs-jules[bot] Jun 7, 2025
07d06bb
style: Condense printing of trailing hunk lines
google-labs-jules[bot] Jun 7, 2025
7c7a269
chore: Remove specific stale developer comments
google-labs-jules[bot] Jun 9, 2025
91bfae6
fix: Ensure removal of specific stale developer comments
google-labs-jules[bot] Jun 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions scripts/gha/firebase_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,37 @@ def get_reviews(token, pull_number):
return results


def get_pull_request_review_comments(token, pull_number):
"""https://docs.github.com/en/rest/pulls/comments#list-review-comments-on-a-pull-request"""
url = f'{GITHUB_API_URL}/pulls/{pull_number}/comments'
headers = {'Accept': 'application/vnd.github.v3+json', 'Authorization': f'token {token}'}
page = 1
per_page = 100
results = []
keep_going = True
while keep_going:
params = {'per_page': per_page, 'page': page}
page = page + 1
keep_going = False
# Use a try-except block to catch potential errors during the API request
try:
with requests_retry_session().get(url, headers=headers, params=params,
stream=True, timeout=TIMEOUT) as response:
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
logging.info("get_pull_request_review_comments: %s page %s response: %s", url, params.get('page'), response)
current_page_results = response.json()
if not current_page_results: # No more results
break
results.extend(current_page_results)
# If exactly per_page results were retrieved, there might be more.
keep_going = (len(current_page_results) == per_page)
except requests.exceptions.RequestException as e:
logging.error(f"Error fetching review comments page {params.get('page')-1} for PR {pull_number}: {e}")
# Optionally, re-raise the exception or handle it by returning partial results or an empty list
break # Stop trying if there's an error
return results


def create_workflow_dispatch(token, workflow_id, ref, inputs):
"""https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event"""
url = f'{GITHUB_API_URL}/actions/workflows/{workflow_id}/dispatches'
Expand Down
123 changes: 123 additions & 0 deletions scripts/gha/get_pr_review_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/env python3
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Fetches and formats review comments from a GitHub Pull Request."""

import argparse
import os
import sys
import firebase_github # Assumes firebase_github.py is in the same directory or python path

# Attempt to configure logging for firebase_github if absl is available
try:
from absl import logging as absl_logging
# Set verbosity for absl logging if you want to see logs from firebase_github
# absl_logging.set_verbosity(absl_logging.INFO)
except ImportError:
# If absl is not used, standard logging can be configured if needed
# import logging as std_logging
# std_logging.basicConfig(level=std_logging.INFO)
pass # firebase_github.py uses absl.logging.info, so this won't redirect.


def main():
# Default owner and repo from firebase_github, ensuring it's loaded.
default_owner = firebase_github.OWNER
default_repo = firebase_github.REPO

parser = argparse.ArgumentParser(
description="Fetch review comments from a GitHub PR and format for use with Jules.",
formatter_class=argparse.RawTextHelpFormatter # To preserve formatting in help text
)
parser.add_argument(
"--pull_number",
type=int,
required=True,
help="Pull request number."
)
parser.add_argument(
"--owner",
type=str,
default=default_owner,
help=f"Repository owner. Defaults to '{default_owner}' (from firebase_github.py)."
)
parser.add_argument(
"--repo",
type=str,
default=default_repo,
help=f"Repository name. Defaults to '{default_repo}' (from firebase_github.py)."
)
parser.add_argument(
"--token",
type=str,
default=os.environ.get("GITHUB_TOKEN"),
help="GitHub token. Can also be set via GITHUB_TOKEN environment variable."
)

args = parser.parse_args()

if not args.token:
sys.stderr.write("Error: GitHub token not provided. Set GITHUB_TOKEN environment variable or use --token argument.\n")
sys.exit(1)

# Update the repository details in firebase_github module if different from default
if args.owner != firebase_github.OWNER or args.repo != firebase_github.REPO:
repo_url = f"https://github.com/{args.owner}/{args.repo}"
if not firebase_github.set_repo_url(repo_url):
sys.stderr.write(f"Error: Invalid repository URL format for {args.owner}/{args.repo}. Expected format: https://github.com/owner/repo\n")
sys.exit(1)
# Using print to stderr for info, as absl logging might not be configured here for this script's own messages.
print(f"Targeting repository: {firebase_github.OWNER}/{firebase_github.REPO}", file=sys.stderr)


print(f"Fetching review comments for PR #{args.pull_number} from {firebase_github.OWNER}/{firebase_github.REPO}...", file=sys.stderr)

comments = firebase_github.get_pull_request_review_comments(args.token, args.pull_number)

if not comments: # This will be true if list is empty (no comments or error in fetching first page)
print(f"No review comments found for PR #{args.pull_number}, or an error occurred during fetching.", file=sys.stderr)
# If firebase_github.py's get_pull_request_review_comments logs errors, those might provide more details.
return # Exit gracefully if no comments

# Output actual data to stdout
print("\n--- Review Comments ---")
for comment in comments:
user = comment.get("user", {}).get("login", "Unknown user")
path = comment.get("path", "N/A")
line = comment.get("line", "N/A")
body = comment.get("body", "").strip() # Strip whitespace from comment body
diff_hunk = comment.get("diff_hunk", "N/A")
html_url = comment.get("html_url", "N/A")

# Only print comments that have a body
if not body:
continue

print(f"Comment by: {user}")
print(f"File: {path}")
# The 'line' field in GitHub's API for PR review comments refers to the line number in the diff.
# 'original_line' refers to the line number in the file at the time the comment was made.
# 'start_line' and 'original_start_line' for multi-line comments.
# For simplicity, we use 'line'.
print(f"Line in diff: {line}")
print(f"URL: {html_url}")
print("--- Diff Hunk ---")
print(diff_hunk)
print("--- Comment ---")
print(body)
print("----------------------------------------\n")

if __name__ == "__main__":
main()
Loading