Skip to content

Commit 06661db

Browse files
chore: raise exception if user is on default branch + tries to create the PR (#455)
# 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
1 parent 4798d01 commit 06661db

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/codegen/sdk/core/codebase.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,9 @@ def create_pr(self, title: str, body: str) -> PullRequest:
902902
if self._op.git_cli.head.is_detached:
903903
msg = "Cannot make a PR from a detached HEAD"
904904
raise ValueError(msg)
905+
if self._op.git_cli.active_branch.name == self._op.default_branch:
906+
msg = "Cannot make a PR from the default branch"
907+
raise ValueError(msg)
905908
self._op.stage_and_commit_all_changes(message=title)
906909
self._op.push_changes()
907910
return self._op.remote_git_repo.create_pull(head_branch_name=self._op.git_cli.active_branch.name, base_branch_name=self._op.default_branch, title=title, body=body)

tests/integration/codegen/git/codebase/test_codebase_create_pr.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
def test_codebase_create_pr_active_branch(codebase: Codebase):
99
head = f"test-create-pr-{uuid.uuid4()}"
1010
codebase.checkout(branch=head, create_if_missing=True)
11-
codebase.files[0].remove()
11+
file = codebase.files[0]
12+
file.remove()
1213
codebase.commit()
1314
pr = codebase.create_pr(title="test-create-pr title", body="test-create-pr body")
1415
assert pr.title == "test-create-pr title"
@@ -17,10 +18,21 @@ def test_codebase_create_pr_active_branch(codebase: Codebase):
1718
assert pr.state == "open"
1819
assert pr.head.ref == head
1920
assert pr.base.ref == "main"
21+
assert pr.get_files().totalCount == 1
22+
assert pr.get_files()[0].filename == file.file_path
2023

2124

2225
def test_codebase_create_pr_detached_head(codebase: Codebase):
2326
codebase.checkout(commit=codebase._op.git_cli.head.commit) # move to detached head state
2427
with pytest.raises(ValueError) as exc_info:
2528
codebase.create_pr(title="test-create-pr title", body="test-create-pr body")
2629
assert "Cannot make a PR from a detached HEAD" in str(exc_info.value)
30+
31+
32+
def test_codebase_create_pr_active_branch_is_default_branch(codebase: Codebase):
33+
codebase.checkout(branch=codebase._op.default_branch)
34+
codebase.files[0].remove()
35+
codebase.commit()
36+
with pytest.raises(ValueError) as exc_info:
37+
codebase.create_pr(title="test-create-pr title", body="test-create-pr body")
38+
assert "Cannot make a PR from the default branch" in str(exc_info.value)

0 commit comments

Comments
 (0)