Skip to content

Commit 7960aa9

Browse files
authored
fix: fixes branch creation for create-pr tool (#495)
1 parent 95e5ab0 commit 7960aa9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import uuid
44
from typing import Any
55

6+
from github import GithubException
7+
68
from codegen import Codebase
79

810

@@ -18,12 +20,25 @@ def create_pr(codebase: Codebase, title: str, body: str) -> dict[str, Any]:
1820
Dict containing PR info, or error information if operation fails
1921
"""
2022
try:
23+
# Check for uncommitted changes and commit them
24+
if len(codebase.get_diff()) == 0:
25+
return {"error": "No changes to create a PR."}
26+
27+
# TODO: this is very jank. We should ideally check out the branch before
28+
# making the changes, but it looks like `codebase.checkout` blows away
29+
# all of your changes
30+
codebase.git_commit(".")
31+
2132
# If on default branch, create a new branch
2233
if codebase._op.git_cli.active_branch.name == codebase._op.default_branch:
2334
codebase.checkout(branch=f"{uuid.uuid4()}", create_if_missing=True)
2435

2536
# Create the PR
26-
pr = codebase.create_pr(title=title, body=body)
37+
try:
38+
pr = codebase.create_pr(title=title, body=body)
39+
except GithubException as e:
40+
print(e)
41+
return {"error": "Failed to create PR. Check if the PR already exists."}
2742
return {
2843
"status": "success",
2944
"url": pr.html_url,

src/codegen/sdk/core/codebase.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ def get_relative_path(self, from_file: str, to_file: str) -> str:
740740
####################################################################################################################
741741

742742
def git_commit(self, message: str, *, verify: bool = False) -> GitCommit | None:
743-
"""Commits all staged changes to the codebase and git.
743+
"""Stages + commits all changes to the codebase and git.
744744
745745
Args:
746746
message (str): The commit message
@@ -753,6 +753,8 @@ def git_commit(self, message: str, *, verify: bool = False) -> GitCommit | None:
753753
if self._op.stage_and_commit_all_changes(message, verify):
754754
logger.info(f"Commited repository to {self._op.head_commit} on {self._op.get_active_branch_or_commit()}")
755755
return self._op.head_commit
756+
else:
757+
logger.info("No changes to commit")
756758
return None
757759

758760
def commit(self, sync_graph: bool = True) -> None:

0 commit comments

Comments
 (0)