Skip to content

Commit

Permalink
add draft config option to create pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
gopidesupavan committed Dec 15, 2024
1 parent b8f7d16 commit bf25e7c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ check_sha = "f382b5ffc445e45a110734f5396728da7914aeb6"
fix_commit_msg = false
default_branch = "devel"
require_version_in_branch_name = false
draft_pr = false
```

Available config options:
Expand Down Expand Up @@ -169,6 +170,9 @@ default_branch Project's default branch name,
require_version_in_branch_name Allow backporting to branches whose names don't contain
something that resembles a version number
(i.e. at least two dot-separated numbers).
draft_pr Create PR as draft
(false by default)
```

To customize the tool for used by other project:
Expand Down
2 changes: 2 additions & 0 deletions cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"fix_commit_msg": True,
"default_branch": "main",
"require_version_in_branch_name": True,
"draft_pr": False,
}
)

Expand Down Expand Up @@ -454,6 +455,7 @@ def create_gh_pr(self, base_branch, head_branch, *, commit_message, gh_auth):
"head": f"{self.username}:{head_branch}",
"base": base_branch,
"maintainer_can_modify": True,
"draft": self.config["draft_pr"],
}
url = CREATE_PR_URL_TEMPLATE.format(config=self.config)
response = requests.post(url, headers=request_headers, json=data, timeout=10)
Expand Down
52 changes: 52 additions & 0 deletions cherry_picker/test_cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import warnings
from collections import ChainMap
from unittest import mock
from unittest.mock import MagicMock

import click
import pytest
Expand Down Expand Up @@ -491,6 +492,7 @@ def test_load_full_config(tmp_git_repo_dir, git_add, git_commit):
"fix_commit_msg": True,
"default_branch": "devel",
"require_version_in_branch_name": True,
"draft_pr": False,
},
)

Expand All @@ -515,6 +517,7 @@ def test_load_partial_config(tmp_git_repo_dir, git_add, git_commit):
"fix_commit_msg": True,
"default_branch": "main",
"require_version_in_branch_name": True,
"draft_pr": False,
},
)

Expand Down Expand Up @@ -544,6 +547,7 @@ def test_load_config_no_head_sha(tmp_git_repo_dir, git_add, git_commit):
"fix_commit_msg": True,
"default_branch": "devel",
"require_version_in_branch_name": True,
"draft_pr": False,
},
)

Expand Down Expand Up @@ -1332,3 +1336,51 @@ def test_abort_cherry_pick_success(

def test_cli_invoked():
subprocess.check_call("cherry_picker --help".split())


@pytest.mark.parametrize("draft_pr", (True, False))
@mock.patch("requests.post")
@mock.patch("gidgethub.sansio.create_headers")
@mock.patch.object(CherryPicker, "username", new_callable=mock.PropertyMock)
def test_create_gh_pr_draft_states(
mock_username, mock_create_headers, mock_post, monkeypatch, draft_pr, config
):
config["draft_pr"] = draft_pr
mock_username.return_value = "username"
monkeypatch.setenv("GH_AUTH", "True")
with mock.patch("cherry_picker.cherry_picker.validate_sha", return_value=True):
cherry_picker = CherryPicker(
"origin", "xxx", [], prefix_commit=True, config=config
)
mock_create_headers.return_value = {"Authorization": "token gh-token"}

mock_response = MagicMock()
mock_response.status_code = 201
mock_response.json.return_value = {
"html_url": "https://github.com/octocat/Hello-World/pull/1347",
"number": 1347,
}
mock_post.return_value = mock_response

base_branch = "main"
head_branch = "feature-branch"
commit_message = "Commit message"
gh_auth = "gh_auth"

cherry_picker.create_gh_pr(
base_branch, head_branch, commit_message=commit_message, gh_auth=gh_auth
)

mock_post.assert_called_once_with(
"https://api.github.com/repos/python/cpython/pulls",
headers={"Authorization": "token gh-token"},
json={
"title": "Commit message",
"body": "",
"head": "username:feature-branch",
"base": "main",
"maintainer_can_modify": True,
"draft": draft_pr,
},
timeout=10,
)

0 comments on commit bf25e7c

Please sign in to comment.