From d57f54556be8aa3e01806aaa815cc63e1a70957d Mon Sep 17 00:00:00 2001 From: codegen-bot Date: Thu, 13 Mar 2025 00:27:11 +0000 Subject: [PATCH 1/2] Handle repositories that don't support draft PRs --- CHANGES.md | 13 ++++++ src/codegen/git/clients/git_repo_client.py | 49 +++++++++++++++++++--- 2 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 CHANGES.md diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 000000000..4932bf333 --- /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. \ No newline at end of file diff --git a/src/codegen/git/clients/git_repo_client.py b/src/codegen/git/clients/git_repo_client.py index e90735639..e55782c4f 100644 --- a/src/codegen/git/clients/git_repo_client.py +++ b/src/codegen/git/clients/git_repo_client.py @@ -200,10 +200,49 @@ 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(f"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: @@ -441,4 +480,4 @@ def search_issues(self, query: str, **kwargs) -> list[Issue]: return self.gh_client.client.search_issues(query, **kwargs) def search_prs(self, query: str, **kwargs) -> list[PullRequest]: - return self.gh_client.client.search_issues(query, **kwargs) + return self.gh_client.client.search_issues(query, **kwargs) \ No newline at end of file From c558b420d63b135ec8cb1ef4218f1084952bd7a0 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Thu, 13 Mar 2025 00:28:02 +0000 Subject: [PATCH 2/2] Automated pre-commit update --- CHANGES.md | 2 +- src/codegen/git/clients/git_repo_client.py | 28 ++++------------------ 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 4932bf333..f44fa1f9a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,4 +10,4 @@ This PR adds support for handling repositories that don't support draft PRs. Whe - 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. \ No newline at end of file +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 e55782c4f..1325f7639 100644 --- a/src/codegen/git/clients/git_repo_client.py +++ b/src/codegen/git/clients/git_repo_client.py @@ -203,28 +203,16 @@ def create_pull( # 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 - ) + 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(f"Repository doesn't support draft PRs. Creating regular PR instead.") + 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 - ) + 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) @@ -233,13 +221,7 @@ def create_pull( 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 - ) + 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) @@ -480,4 +462,4 @@ def search_issues(self, query: str, **kwargs) -> list[Issue]: return self.gh_client.client.search_issues(query, **kwargs) def search_prs(self, query: str, **kwargs) -> list[PullRequest]: - return self.gh_client.client.search_issues(query, **kwargs) \ No newline at end of file + return self.gh_client.client.search_issues(query, **kwargs)