From 482e2f14e88f273394e74a61d6a0e495b434ac49 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Fri, 21 Mar 2025 18:09:40 +0000 Subject: [PATCH 1/2] CG-1234: Add comments and reviews to GithubViewPRTool --- .../extensions/tools/github/view_pr.py | 18 +++++-- src/codegen/sdk/core/codebase.py | 52 ++++++++++++++++++- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/codegen/extensions/tools/github/view_pr.py b/src/codegen/extensions/tools/github/view_pr.py index 00c20f7bb..e7dc7fa9c 100644 --- a/src/codegen/extensions/tools/github/view_pr.py +++ b/src/codegen/extensions/tools/github/view_pr.py @@ -1,6 +1,6 @@ """Tool for viewing PR contents and modified symbols.""" -from typing import ClassVar +from typing import ClassVar, Dict, List, Any from pydantic import Field @@ -24,6 +24,14 @@ class ViewPRObservation(Observation): modified_symbols: list[str] = Field( description="Names of modified symbols in the PR", ) + comments: List[Dict[str, Any]] = Field( + description="Comments on the PR", + default_factory=list, + ) + reviews: List[Dict[str, Any]] = Field( + description="Reviews on the PR", + default_factory=list, + ) str_template: ClassVar[str] = "PR #{pr_id}" @@ -36,14 +44,16 @@ def view_pr(codebase: Codebase, pr_id: int) -> ViewPRObservation: pr_id: Number of the PR to get the contents for """ try: - patch, file_commit_sha, moddified_symbols = codebase.get_modified_symbols_in_pr(pr_id) + patch, file_commit_sha, modified_symbols, comments, reviews = codebase.get_modified_symbols_in_pr(pr_id) return ViewPRObservation( status="success", pr_id=pr_id, patch=patch, file_commit_sha=file_commit_sha, - modified_symbols=moddified_symbols, + modified_symbols=modified_symbols, + comments=comments, + reviews=reviews, ) except Exception as e: @@ -54,4 +64,6 @@ def view_pr(codebase: Codebase, pr_id: int) -> ViewPRObservation: patch="", file_commit_sha={}, modified_symbols=[], + comments=[], + reviews=[], ) diff --git a/src/codegen/sdk/core/codebase.py b/src/codegen/sdk/core/codebase.py index 01950a7a2..f20c37c40 100644 --- a/src/codegen/sdk/core/codebase.py +++ b/src/codegen/sdk/core/codebase.py @@ -1527,13 +1527,61 @@ def from_files( logger.info("Codebase initialization complete") return codebase - def get_modified_symbols_in_pr(self, pr_id: int) -> tuple[str, dict[str, str], list[str], str]: + def get_modified_symbols_in_pr(self, pr_id: int) -> tuple[str, dict[str, str], list[str], list[dict], list[dict]]: """Get all modified symbols in a pull request""" pr = self._op.get_pull_request(pr_id) cg_pr = CodegenPR(self._op, self, pr) patch = cg_pr.get_pr_diff() commit_sha = cg_pr.get_file_commit_shas() - return patch, commit_sha, cg_pr.modified_symbols, pr.head.ref + + # Get comments and reviews + comments = [] + reviews = [] + + try: + # Get PR comments (issue comments) + issue_comments = pr.get_issue_comments() + for comment in issue_comments: + comments.append({ + "id": comment.id, + "user": comment.user.login, + "body": comment.body, + "created_at": comment.created_at.isoformat(), + "updated_at": comment.updated_at.isoformat() if comment.updated_at else None, + "type": "issue_comment" + }) + + # Get PR review comments (comments on specific lines) + review_comments = pr.get_comments() + for comment in review_comments: + comments.append({ + "id": comment.id, + "user": comment.user.login, + "body": comment.body, + "created_at": comment.created_at.isoformat(), + "updated_at": comment.updated_at.isoformat() if comment.updated_at else None, + "path": comment.path, + "position": comment.position, + "commit_id": comment.commit_id, + "type": "review_comment" + }) + + # Get PR reviews + pr_reviews = pr.get_reviews() + for review in pr_reviews: + reviews.append({ + "id": review.id, + "user": review.user.login, + "body": review.body, + "state": review.state, + "submitted_at": review.submitted_at.isoformat() if review.submitted_at else None, + "commit_id": review.commit_id, + "type": "review" + }) + except Exception as e: + print(f"Error fetching PR comments or reviews: {e}") + + return patch, commit_sha, cg_pr.modified_symbols, comments, reviews def create_pr_comment(self, pr_number: int, body: str) -> None: """Create a comment on a pull request""" From 2d5b789592e59c04f571fbd71a4df37b45e7a492 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Fri, 21 Mar 2025 18:10:23 +0000 Subject: [PATCH 2/2] Automated pre-commit update --- .../extensions/tools/github/view_pr.py | 6 +- src/codegen/sdk/core/codebase.py | 72 ++++++++++--------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/codegen/extensions/tools/github/view_pr.py b/src/codegen/extensions/tools/github/view_pr.py index e7dc7fa9c..edfaa5899 100644 --- a/src/codegen/extensions/tools/github/view_pr.py +++ b/src/codegen/extensions/tools/github/view_pr.py @@ -1,6 +1,6 @@ """Tool for viewing PR contents and modified symbols.""" -from typing import ClassVar, Dict, List, Any +from typing import Any, ClassVar from pydantic import Field @@ -24,11 +24,11 @@ class ViewPRObservation(Observation): modified_symbols: list[str] = Field( description="Names of modified symbols in the PR", ) - comments: List[Dict[str, Any]] = Field( + comments: list[dict[str, Any]] = Field( description="Comments on the PR", default_factory=list, ) - reviews: List[Dict[str, Any]] = Field( + reviews: list[dict[str, Any]] = Field( description="Reviews on the PR", default_factory=list, ) diff --git a/src/codegen/sdk/core/codebase.py b/src/codegen/sdk/core/codebase.py index f20c37c40..f36081cc2 100644 --- a/src/codegen/sdk/core/codebase.py +++ b/src/codegen/sdk/core/codebase.py @@ -1533,54 +1533,60 @@ def get_modified_symbols_in_pr(self, pr_id: int) -> tuple[str, dict[str, str], l cg_pr = CodegenPR(self._op, self, pr) patch = cg_pr.get_pr_diff() commit_sha = cg_pr.get_file_commit_shas() - + # Get comments and reviews comments = [] reviews = [] - + try: # Get PR comments (issue comments) issue_comments = pr.get_issue_comments() for comment in issue_comments: - comments.append({ - "id": comment.id, - "user": comment.user.login, - "body": comment.body, - "created_at": comment.created_at.isoformat(), - "updated_at": comment.updated_at.isoformat() if comment.updated_at else None, - "type": "issue_comment" - }) - + comments.append( + { + "id": comment.id, + "user": comment.user.login, + "body": comment.body, + "created_at": comment.created_at.isoformat(), + "updated_at": comment.updated_at.isoformat() if comment.updated_at else None, + "type": "issue_comment", + } + ) + # Get PR review comments (comments on specific lines) review_comments = pr.get_comments() for comment in review_comments: - comments.append({ - "id": comment.id, - "user": comment.user.login, - "body": comment.body, - "created_at": comment.created_at.isoformat(), - "updated_at": comment.updated_at.isoformat() if comment.updated_at else None, - "path": comment.path, - "position": comment.position, - "commit_id": comment.commit_id, - "type": "review_comment" - }) - + comments.append( + { + "id": comment.id, + "user": comment.user.login, + "body": comment.body, + "created_at": comment.created_at.isoformat(), + "updated_at": comment.updated_at.isoformat() if comment.updated_at else None, + "path": comment.path, + "position": comment.position, + "commit_id": comment.commit_id, + "type": "review_comment", + } + ) + # Get PR reviews pr_reviews = pr.get_reviews() for review in pr_reviews: - reviews.append({ - "id": review.id, - "user": review.user.login, - "body": review.body, - "state": review.state, - "submitted_at": review.submitted_at.isoformat() if review.submitted_at else None, - "commit_id": review.commit_id, - "type": "review" - }) + reviews.append( + { + "id": review.id, + "user": review.user.login, + "body": review.body, + "state": review.state, + "submitted_at": review.submitted_at.isoformat() if review.submitted_at else None, + "commit_id": review.commit_id, + "type": "review", + } + ) except Exception as e: print(f"Error fetching PR comments or reviews: {e}") - + return patch, commit_sha, cg_pr.modified_symbols, comments, reviews def create_pr_comment(self, pr_number: int, body: str) -> None: