Skip to content

Commit da2e12f

Browse files
committed
fix: report malformed GitHub PR responses as GitOperationError
1 parent 3757504 commit da2e12f

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

pr_split/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ErrorMsg(StrEnum):
2222
PR_CREATE_FAILED = "Failed to create PR for group '{group}': {detail}"
2323
MERGE_FAILED = "Merge of '{source}' into '{target}' failed: {detail}"
2424
PR_NOT_FOUND = "PR #{number} not found"
25+
PR_RESPONSE_INVALID = "Unexpected response from GitHub for PR #{number}: {detail}"
2526
PR_NOT_FROM_FORK = (
2627
"PR #{number} is not from a fork; pass its head branch name instead of the PR number"
2728
)

pr_split/git_ops/prs.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ def fetch_fork_pr(pr_number: int) -> ForkPRInfo:
9696
except GitOperationError as exc:
9797
raise GitOperationError(ErrorMsg.PR_NOT_FOUND(number=pr_number)) from exc
9898

99-
pr_data: dict[str, object] = json.loads(raw)
100-
head = pr_data["head"]
101-
base = pr_data["base"]
99+
try:
100+
pr_data = json.loads(raw)
101+
head = pr_data["head"]
102+
base = pr_data["base"]
103+
except (json.JSONDecodeError, KeyError, TypeError) as exc:
104+
raise GitOperationError(
105+
ErrorMsg.PR_RESPONSE_INVALID(number=pr_number, detail=str(exc))
106+
) from exc
102107

103108
if not isinstance(head, dict) or not isinstance(base, dict):
104109
raise GitOperationError(ErrorMsg.PR_NOT_FOUND(number=pr_number))

tests/test_git_prs_coverage.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,16 @@ def test_same_repo_pr_gets_specific_message(self, mock_gh: MagicMock) -> None:
183183
mock_gh.return_value = json.dumps(pr_data)
184184
with pytest.raises(GitOperationError, match="PR #42 is not from a fork"):
185185
fetch_fork_pr(42)
186+
187+
188+
class TestFetchForkPrMalformedResponse:
189+
@pytest.mark.parametrize(
190+
"raw", ["not json", "[]", '{"head": {}}'], ids=["text", "list", "no-base"]
191+
)
192+
@patch("pr_split.git_ops.prs._run_gh")
193+
def test_malformed_response_is_a_git_operation_error(
194+
self, mock_gh: MagicMock, raw: str
195+
) -> None:
196+
mock_gh.return_value = raw
197+
with pytest.raises(GitOperationError, match="Unexpected response from GitHub for PR #42"):
198+
fetch_fork_pr(42)

0 commit comments

Comments
 (0)