Skip to content

chore: pr-review-tooling #434

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 5 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
10 changes: 3 additions & 7 deletions codegen-examples/examples/pr_review_bot/run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import codegen
from codegen import Codebase
from codegen.sdk.enums import ProgrammingLanguage
from codegen.sdk.codebase.config import CodebaseConfig
from codegen.shared.enums.programming_language import ProgrammingLanguage
from codegen.sdk.codebase.config import CodebaseConfig, Secrets
import json

from codegen.sdk.secrets import Secrets
Expand All @@ -20,13 +20,9 @@ def run(codebase: Codebase):
modified_symbols = codebase.get_modified_symbols_in_pr(pr_number)
for symbol in modified_symbols:
# Get direct dependencies
deps = codebase.get_symbol_dependencies(symbol, max_depth=2)
deps = symbol.dependencies(max_depth=2)
context_symbols.update(deps)

# Get reverse dependencies (symbols that depend on this one)
rev_deps = codebase.get_symbol_dependents(symbol, max_depth=2)
context_symbols.update(rev_deps)

# Prepare context for LLM
context = {
"modified_symbols": [
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/git/repo_operator/local_repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
####################################################################################################################

@property
def remote_git_repo(self) -> GitRepoClient | None:

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

View workflow job for this annotation

GitHub Actions / mypy

error: Signature of "remote_git_repo" incompatible with supertype "RepoOperator" [override]
"""Get the remote GitRepoClient object for the current local repo."""
if not self.access_token:
msg = "Must initialize with access_token to get remote"
Expand Down Expand Up @@ -183,7 +183,7 @@
if repo is None:
logger.warning("GitHub API key is required to fetch pull requests")
return None
return repo.get_pull(pr_number)
return repo.get_pull_safe(pr_number)
except Exception as e:
logger.warning(f"Failed to get PR {pr_number}: {e!s}")
return None
8 changes: 8 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 @@ -600,3 +600,11 @@

def stash_pop(self) -> None:
self.git_cli.git.stash("pop")

####################################################################################################################
# PR UTILITIES
####################################################################################################################

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]
17 changes: 17 additions & 0 deletions src/codegen/git/utils/pr_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,20 @@ def modified_symbols(self) -> list["Symbol"]:
if self.is_modified(symbol):
all_modified.append(symbol)
return all_modified

def get_pr_diff(self) -> str:
"""Get the full diff of the PR"""
if not self._op.remote_git_repo:
msg = "GitHub API client is required to get PR diffs"
raise ValueError(msg)

# Get the diff directly from the PR
diff_url = self._gh_pr.raw_data.get("diff_url")
if diff_url:
# Fetch the diff content from the URL
response = requests.get(diff_url)
response.raise_for_status()
return response.text
else:
# If diff_url not available, get the patch directly
return self._gh_pr.get_patch()
5 changes: 3 additions & 2 deletions src/codegen/sdk/core/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,11 +1259,12 @@ def from_repo(
logger.exception(f"Failed to initialize codebase: {e}")
raise

def get_modified_symbols_in_pr(self, pr_id: int) -> list[Symbol]:
def get_modified_symbols_in_pr(self, pr_id: int) -> tuple[list[Symbol], str]:
"""Get all modified symbols in a pull request"""
pr = self._op.get_pull_request(pr_id)
cg_pr = CodegenPR(self._op, self, pr)
return cg_pr.modified_symbols
patch = cg_pr.get_pr_diff()
return cg_pr.modified_symbols, patch


# The last 2 lines of code are added to the runner. See codegen-backend/cli/generate/utils.py
Expand Down
Loading