diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 000000000..f44fa1f9a --- /dev/null +++ b/CHANGES.md @@ -0,0 +1,13 @@ +# Changes + +This PR adds support for handling repositories that don't support draft PRs. When attempting to create a draft PR in a repository that doesn't support this feature, the code will now gracefully fall back to creating a regular PR instead of failing. + +## Implementation Details + +1. Modified the `create_pull` method in the `GitRepoClient` class to: + - First attempt to create a draft PR when requested + - Catch GitHub exceptions with status code 422 that mention "draft" in the error message + - Fall back to creating a regular PR when draft PRs aren't supported + - Log appropriate warning messages + +This change ensures that the PR creation process is more robust and works across all GitHub repositories, including older GitHub Enterprise Server instances that may not support draft PRs. diff --git a/src/codegen/git/clients/git_repo_client.py b/src/codegen/git/clients/git_repo_client.py index e90735639..1325f7639 100644 --- a/src/codegen/git/clients/git_repo_client.py +++ b/src/codegen/git/clients/git_repo_client.py @@ -200,10 +200,31 @@ def create_pull( if base_branch_name is None: base_branch_name = self.default_branch try: - pr = self.repo.create_pull(title=title or f"Draft PR for {head_branch_name}", body=body or "", head=head_branch_name, base=base_branch_name, draft=draft) - logger.info(f"Created pull request for head branch: {head_branch_name} at {pr.html_url}") - # NOTE: return a read-only copy to prevent people from editing it - return self.repo.get_pull(pr.number) + # First attempt to create with draft=True if requested + if draft: + try: + pr = self.repo.create_pull(title=title or f"Draft PR for {head_branch_name}", body=body or "", head=head_branch_name, base=base_branch_name, draft=True) + logger.info(f"Created draft pull request for head branch: {head_branch_name} at {pr.html_url}") + # NOTE: return a read-only copy to prevent people from editing it + return self.repo.get_pull(pr.number) + except GithubException as ge: + # Check if the error is related to draft PR support (422 Unprocessable Entity) + if "422" in str(ge) and "draft" in str(ge).lower(): + logger.warning("Repository doesn't support draft PRs. Creating regular PR instead.") + # Fall back to non-draft PR + pr = self.repo.create_pull(title=title or f"PR for {head_branch_name}", body=body or "", head=head_branch_name, base=base_branch_name, draft=False) + logger.info(f"Created regular pull request for head branch: {head_branch_name} at {pr.html_url}") + # NOTE: return a read-only copy to prevent people from editing it + return self.repo.get_pull(pr.number) + else: + # Re-raise if it's not a draft PR support issue + raise + else: + # If draft is not requested, create a regular PR + pr = self.repo.create_pull(title=title or f"PR for {head_branch_name}", body=body or "", head=head_branch_name, base=base_branch_name, draft=False) + logger.info(f"Created pull request for head branch: {head_branch_name} at {pr.html_url}") + # NOTE: return a read-only copy to prevent people from editing it + return self.repo.get_pull(pr.number) except GithubException as ge: logger.warning(f"Failed to create PR got GithubException\n\t{ge}") except Exception as e: