Skip to content

chore: pr review commenting tooling #449

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 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
97 changes: 97 additions & 0 deletions src/codegen/extensions/langchain/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,100 @@ def _run(self, title: str, body: str) -> str:
return pr.html_url


class GetPRContentsInput(BaseModel):
"""Input for getting PR contents."""

pr_id: int = Field(..., description="Number of the PR to get the contents for")


class GetPRcontentsTool(BaseTool):
"""Tool for getting PR data."""

name: ClassVar[str] = "get_pr_contents"
description: ClassVar[str] = "Get the diff and modified symbols of a PR along with the dependencies of the modified symbols"
args_schema: ClassVar[type[BaseModel]] = GetPRContentsInput
codebase: Codebase = Field(exclude=True)

def __init__(self, codebase: Codebase) -> None:
super().__init__(codebase=codebase)

def _run(self, pr_id: int) -> str:
modified_symbols, patch = self.codebase.get_modified_symbols_in_pr(pr_id)

# Convert modified_symbols set to list for JSON serialization
result = {"modified_symbols": list(modified_symbols), "patch": patch}

return json.dumps(result, indent=2)


class CreatePRCommentInput(BaseModel):
"""Input for creating a PR comment"""

pr_number: int = Field(..., description="The PR number to comment on")
body: str = Field(..., description="The comment text")


class CreatePRCommentTool(BaseTool):
"""Tool for creating a general PR comment."""

name: ClassVar[str] = "create_pr_comment"
description: ClassVar[str] = "Create a general comment on a pull request"
args_schema: ClassVar[type[BaseModel]] = CreatePRCommentInput
codebase: Codebase = Field(exclude=True)

def __init__(self, codebase: Codebase) -> None:
super().__init__(codebase=codebase)

def _run(self, pr_number: int, body: str) -> str:
self.codebase.create_pr_comment(pr_number=pr_number, body=body)
return "Comment created successfully"


class CreatePRReviewCommentInput(BaseModel):
"""Input for creating an inline PR review comment"""

pr_number: int = Field(..., description="The PR number to comment on")
body: str = Field(..., description="The comment text")
commit_sha: str = Field(..., description="The commit SHA to attach the comment to")
path: str = Field(..., description="The file path to comment on")
line: int | None = Field(None, description="The line number to comment on")
side: str | None = Field(None, description="Which version of the file to comment on ('LEFT' or 'RIGHT')")
start_line: int | None = Field(None, description="For multi-line comments, the starting line")


class CreatePRReviewCommentTool(BaseTool):
"""Tool for creating inline PR review comments."""

name: ClassVar[str] = "create_pr_review_comment"
description: ClassVar[str] = "Create an inline review comment on a specific line in a pull request"
args_schema: ClassVar[type[BaseModel]] = CreatePRReviewCommentInput
codebase: Codebase = Field(exclude=True)

def __init__(self, codebase: Codebase) -> None:
super().__init__(codebase=codebase)

def _run(
self,
pr_number: int,
body: str,
commit_sha: str,
path: str,
line: int | None = None,
side: str | None = None,
start_line: int | None = None,
) -> str:
self.codebase.create_pr_review_comment(
pr_number=pr_number,
body=body,
commit_sha=commit_sha,
path=path,
line=line,
side=side,
start_line=start_line,
)
return "Review comment created successfully"


def get_workspace_tools(codebase: Codebase) -> list["BaseTool"]:
"""Get all workspace tools initialized with a codebase.

Expand All @@ -372,8 +466,11 @@ def get_workspace_tools(codebase: Codebase) -> list["BaseTool"]:
CommitTool(codebase),
CreateFileTool(codebase),
CreatePRTool(codebase),
CreatePRCommentTool(codebase),
CreatePRReviewCommentTool(codebase),
DeleteFileTool(codebase),
EditFileTool(codebase),
GetPRcontentsTool(codebase),
ListDirectoryTool(codebase),
RevealSymbolTool(codebase),
SearchTool(codebase),
Expand Down
46 changes: 46 additions & 0 deletions src/codegen/git/repo_operator/repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
@property
def remote_git_repo(self) -> GitRepoClient:
if not self._remote_git_repo:
self._remote_git_repo = GitRepoClient(self.repo_config, access_token=self.access_token)

Check failure on line 68 in src/codegen/git/repo_operator/repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument "access_token" to "GitRepoClient" has incompatible type "str | None"; expected "str" [arg-type]
return self._remote_git_repo

@property
Expand Down Expand Up @@ -111,7 +111,7 @@
email_level = None
levels = ["system", "global", "user", "repository"]
for level in levels:
with git_cli.config_reader(level) as reader:

Check failure on line 114 in src/codegen/git/repo_operator/repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "config_reader" of "Repo" has incompatible type "str"; expected "Literal['system', 'global', 'user', 'repository'] | None" [arg-type]
if reader.has_option("user", "name") and not username:
username = reader.get("user", "name")
user_level = level
Expand Down Expand Up @@ -472,7 +472,7 @@
return content
except UnicodeDecodeError:
print(f"Warning: Unable to decode file {file_path}. Skipping.")
return None

Check failure on line 475 in src/codegen/git/repo_operator/repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "None", expected "str") [return-value]

def write_file(self, relpath: str, content: str) -> None:
"""Writes file content to disk"""
Expand Down Expand Up @@ -517,7 +517,7 @@

# Iterate through files and yield contents
for rel_filepath in filepaths:
rel_filepath: str

Check failure on line 520 in src/codegen/git/repo_operator/repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Name "rel_filepath" already defined on line 519 [no-redef]
filepath = os.path.join(self.repo_path, rel_filepath)

# Filter by subdirectory (includes full filenames)
Expand Down Expand Up @@ -548,7 +548,7 @@
list_files = []

for rel_filepath in self.git_cli.git.ls_files().split("\n"):
rel_filepath: str

Check failure on line 551 in src/codegen/git/repo_operator/repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Name "rel_filepath" already defined on line 550 [no-redef]
if subdirs and not any(d in rel_filepath for d in subdirs):
continue
if extensions is None or any(rel_filepath.endswith(e) for e in extensions):
Expand All @@ -572,7 +572,7 @@

def get_modified_files_in_last_n_days(self, days: int = 1) -> tuple[list[str], list[str]]:
"""Returns a list of files modified and deleted in the last n days"""
modified_files = []

Check failure on line 575 in src/codegen/git/repo_operator/repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Need type annotation for "modified_files" (hint: "modified_files: list[<type>] = ...") [var-annotated]
deleted_files = []
allowed_extensions = [".py"]

Expand All @@ -588,9 +588,9 @@
if file in modified_files:
modified_files.remove(file)
else:
if file not in modified_files and file[-3:] in allowed_extensions:

Check failure on line 591 in src/codegen/git/repo_operator/repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Value of type "str | PathLike[str]" is not indexable [index]
modified_files.append(file)
return modified_files, deleted_files

Check failure on line 593 in src/codegen/git/repo_operator/repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "tuple[list[str | PathLike[str]], list[str | PathLike[str]]]", expected "tuple[list[str], list[str]]") [return-value]

@abstractmethod
def base_url(self) -> str | None: ...
Expand All @@ -607,4 +607,50 @@

def get_pr_data(self, pr_number: int) -> dict:
"""Returns the data associated with a PR"""
return self.remote_git_repo.get_pr_data(pr_number)

Check failure on line 610 in src/codegen/git/repo_operator/repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: "GitRepoClient" has no attribute "get_pr_data" [attr-defined]

def create_pr_comment(self, pr_number: int, body: str) -> None:
"""Create a general comment on a pull request.

Args:
pr_number (int): The PR number to comment on
body (str): The comment text
"""
pr = self.remote_git_repo.get_pull_safe(pr_number)
if pr:
self.remote_git_repo.create_issue_comment(pr, body)

def create_pr_review_comment(
self,
pr_number: int,
body: str,
commit_sha: str,
path: str,
line: int | None = None,
side: str | None = None,
start_line: int | None = None,
) -> None:
"""Create an inline review comment on a specific line in a pull request.

Args:
pr_number (int): The PR number to comment on
body (str): The comment text
commit_sha (str): The commit SHA to attach the comment to
path (str): The file path to comment on
line (int | None, optional): The line number to comment on. Defaults to None.
side (str | None, optional): Which version of the file to comment on ('LEFT' or 'RIGHT'). Defaults to None.
start_line (int | None, optional): For multi-line comments, the starting line. Defaults to None.
"""
pr = self.remote_git_repo.get_pull_safe(pr_number)
if pr:
commit = self.remote_git_repo.get_commit_safe(commit_sha)
if commit:
self.remote_git_repo.create_review_comment(
pull=pr,
body=body,
commit=commit,
path=path,
line=line,

Check failure on line 653 in src/codegen/git/repo_operator/repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument "line" to "create_review_comment" of "GitRepoClient" has incompatible type "int | None"; expected "int | _NotSetType" [arg-type]
side=side,
start_line=start_line,
)