Skip to content

Commit 24bb17b

Browse files
authored
feat: Tooling fixes (#598)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed --------- Co-authored-by: kopekC <[email protected]>
1 parent 410ee85 commit 24bb17b

File tree

6 files changed

+87
-23
lines changed

6 files changed

+87
-23
lines changed

src/codegen/extensions/langchain/tools.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,7 @@ class GithubCreatePRReviewCommentInput(BaseModel):
493493
body: str = Field(..., description="The comment text")
494494
commit_sha: str = Field(..., description="The commit SHA to attach the comment to")
495495
path: str = Field(..., description="The file path to comment on")
496-
line: int | None = Field(None, description="The line number to comment on")
497-
side: str | None = Field(None, description="Which version of the file to comment on ('LEFT' or 'RIGHT')")
496+
line: int = Field(..., description="The line number to comment on use the indices from the diff")
498497
start_line: int | None = Field(None, description="For multi-line comments, the starting line")
499498

500499

@@ -515,8 +514,7 @@ def _run(
515514
body: str,
516515
commit_sha: str,
517516
path: str,
518-
line: int | None = None,
519-
side: str | None = None,
517+
line: int,
520518
start_line: int | None = None,
521519
) -> str:
522520
result = create_pr_review_comment(
@@ -526,8 +524,6 @@ def _run(
526524
commit_sha=commit_sha,
527525
path=path,
528526
line=line,
529-
side=side,
530-
start_line=start_line,
531527
)
532528
return result.render()
533529

src/codegen/extensions/tools/github/create_pr_review_comment.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ class PRReviewCommentObservation(Observation):
1515
pr_number: int = Field(
1616
description="PR number the comment was added to",
1717
)
18+
body: str = Field(
19+
description="Content of the comment",
20+
)
21+
commit_sha: str = Field(
22+
description="Commit SHA the comment was added to",
23+
)
1824
path: str = Field(
1925
description="File path the comment was added to",
2026
)
21-
line: Optional[int] = Field(
22-
default=None,
27+
line: int = Field(
2328
description="Line number the comment was added to",
2429
)
25-
body: str = Field(
26-
description="Content of the comment",
27-
)
28-
2930
str_template: ClassVar[str] = "Added review comment to PR #{pr_number} at {path}:{line}"
3031

3132

@@ -35,8 +36,7 @@ def create_pr_review_comment(
3536
body: str,
3637
commit_sha: str,
3738
path: str,
38-
line: Optional[int] = None,
39-
side: Optional[str] = None,
39+
line: int,
4040
start_line: Optional[int] = None,
4141
) -> PRReviewCommentObservation:
4242
"""Create an inline review comment on a specific line in a pull request.
@@ -48,8 +48,6 @@ def create_pr_review_comment(
4848
commit_sha: The commit SHA to attach the comment to
4949
path: The file path to comment on
5050
line: The line number to comment on
51-
side: Which version of the file to comment on ('LEFT' or 'RIGHT')
52-
start_line: For multi-line comments, the starting line
5351
"""
5452
try:
5553
codebase.create_pr_review_comment(
@@ -58,15 +56,15 @@ def create_pr_review_comment(
5856
commit_sha=commit_sha,
5957
path=path,
6058
line=line,
61-
side=side,
62-
start_line=start_line,
59+
side="RIGHT",
6360
)
6461
return PRReviewCommentObservation(
6562
status="success",
6663
pr_number=pr_number,
6764
path=path,
6865
line=line,
6966
body=body,
67+
commit_sha=commit_sha,
7068
)
7169
except Exception as e:
7270
return PRReviewCommentObservation(
@@ -76,4 +74,5 @@ def create_pr_review_comment(
7674
path=path,
7775
line=line,
7876
body=body,
77+
commit_sha=commit_sha,
7978
)

src/codegen/extensions/tools/github/view_pr.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class ViewPRObservation(Observation):
1818
patch: str = Field(
1919
description="The PR's patch/diff content",
2020
)
21+
file_commit_sha: dict[str, str] = Field(
22+
description="Commit SHAs for each file in the PR",
23+
)
2124

2225
str_template: ClassVar[str] = "PR #{pr_id}"
2326

@@ -30,12 +33,13 @@ def view_pr(codebase: Codebase, pr_id: int) -> ViewPRObservation:
3033
pr_id: Number of the PR to get the contents for
3134
"""
3235
try:
33-
modified_symbols, patch = codebase.get_modified_symbols_in_pr(pr_id)
36+
patch, file_commit_sha = codebase.get_modified_symbols_in_pr(pr_id)
3437

3538
return ViewPRObservation(
3639
status="success",
3740
pr_id=pr_id,
3841
patch=patch,
42+
file_commit_sha=file_commit_sha,
3943
)
4044

4145
except Exception as e:
@@ -44,4 +48,5 @@ def view_pr(codebase: Codebase, pr_id: int) -> ViewPRObservation:
4448
error=f"Failed to view PR: {e!s}",
4549
pr_id=pr_id,
4650
patch="",
51+
file_commit_sha={},
4752
)

src/codegen/git/repo_operator/repo_operator.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def create_pr_review_comment(
735735
commit_sha: str,
736736
path: str,
737737
line: int | None = None,
738-
side: str | None = None,
738+
side: str = "RIGHT",
739739
start_line: int | None = None,
740740
) -> None:
741741
"""Create an inline review comment on a specific line in a pull request.
@@ -760,7 +760,6 @@ def create_pr_review_comment(
760760
path=path,
761761
line=line,
762762
side=side,
763-
start_line=start_line,
764763
)
765764

766765
def get_pull_request(self, pr_number: int) -> PullRequest | None:

src/codegen/git/utils/pr_review.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,45 @@ def overlaps(range1: range, range2: range) -> bool:
7373
return max(range1.start, range2.start) < min(range1.stop, range2.stop)
7474

7575

76+
def get_file_to_commit_sha(op: RepoOperator, pull: PullRequest) -> dict[str, str]:
77+
"""Gets a mapping of file paths to their latest commit SHA in the PR.
78+
79+
Args:
80+
op (RepoOperator): The repository operator
81+
pull (PullRequest): The pull request object
82+
83+
Returns:
84+
dict[str, str]: A dictionary mapping file paths to their latest commit SHA
85+
"""
86+
if not op.remote_git_repo:
87+
msg = "GitHub API client is required to get PR commit information"
88+
raise ValueError(msg)
89+
90+
file_to_commit = {}
91+
92+
# Get all commits in the PR
93+
commits = list(pull.get_commits())
94+
95+
# Get all modified files
96+
files = pull.get_files()
97+
98+
# For each file, find its latest commit
99+
for file in files:
100+
# Look through commits in reverse order to find the latest one that modified this file
101+
for commit in reversed(commits):
102+
# Get the files modified in this commit
103+
files_in_commit = commit.files
104+
if any(f.filename == file.filename for f in files_in_commit):
105+
file_to_commit[file.filename] = commit.sha
106+
break
107+
108+
# If we didn't find a commit (shouldn't happen), use the head SHA
109+
if file.filename not in file_to_commit:
110+
file_to_commit[file.filename] = pull.head.sha
111+
112+
return file_to_commit
113+
114+
76115
class CodegenPR:
77116
"""Wrapper around PRs - enables codemods to interact with them"""
78117

@@ -143,3 +182,11 @@ def get_pr_diff(self) -> str:
143182
else:
144183
# If diff_url not available, get the patch directly
145184
return self._gh_pr.get_patch()
185+
186+
def get_commit_sha(self) -> str:
187+
"""Get the commit SHA of the PR"""
188+
return self._gh_pr.head.sha
189+
190+
def get_file_commit_shas(self) -> dict[str, str]:
191+
"""Get a mapping of file paths to their latest commit SHA in the PR"""
192+
return get_file_to_commit_sha(op=self._op, pull=self._gh_pr)

src/codegen/sdk/core/codebase.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,17 +1311,35 @@ def from_repo(
13111311
logger.exception(f"Failed to initialize codebase: {e}")
13121312
raise
13131313

1314-
def get_modified_symbols_in_pr(self, pr_id: int) -> tuple[list[Symbol], str]:
1314+
def get_modified_symbols_in_pr(self, pr_id: int) -> tuple[str, dict[str, str]]:
13151315
"""Get all modified symbols in a pull request"""
13161316
pr = self._op.get_pull_request(pr_id)
13171317
cg_pr = CodegenPR(self._op, self, pr)
13181318
patch = cg_pr.get_pr_diff()
1319-
return cg_pr.modified_symbols, patch
1319+
commit_sha = cg_pr.get_file_commit_shas()
1320+
return patch, commit_sha
13201321

13211322
def create_pr_comment(self, pr_number: int, body: str) -> None:
13221323
"""Create a comment on a pull request"""
13231324
return self._op.create_pr_comment(pr_number, body)
13241325

1326+
def create_pr_review_comment(self, pr_number: int, body: str, commit_sha: str, path: str, line: int | None = None, side: str = "RIGHT", start_line: int | None = None) -> None:
1327+
"""Create a review comment on a pull request.
1328+
1329+
Args:
1330+
pr_number: The number of the pull request
1331+
body: The body of the comment
1332+
commit_sha: The SHA of the commit to comment on
1333+
path: The path of the file to comment on
1334+
line: The line number to comment on
1335+
side: The side of the comment to create
1336+
start_line: The start line number to comment on
1337+
1338+
Returns:
1339+
None
1340+
"""
1341+
return self._op.create_pr_review_comment(pr_number, body, commit_sha, path, line, side, start_line)
1342+
13251343

13261344
# The last 2 lines of code are added to the runner. See codegen-backend/cli/generate/utils.py
13271345
# Type Aliases

0 commit comments

Comments
 (0)