Skip to content

Commit 962ba5c

Browse files
add draft config option to create pull request (#151)
1 parent 014b2aa commit 962ba5c

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ check_sha = "f382b5ffc445e45a110734f5396728da7914aeb6"
138138
fix_commit_msg = false
139139
default_branch = "devel"
140140
require_version_in_branch_name = false
141+
draft_pr = false
141142
```
142143

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

174178
To customize the tool for used by other project:

cherry_picker/cherry_picker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"fix_commit_msg": True,
3434
"default_branch": "main",
3535
"require_version_in_branch_name": True,
36+
"draft_pr": False,
3637
}
3738
)
3839

@@ -454,6 +455,7 @@ def create_gh_pr(self, base_branch, head_branch, *, commit_message, gh_auth):
454455
"head": f"{self.username}:{head_branch}",
455456
"base": base_branch,
456457
"maintainer_can_modify": True,
458+
"draft": self.config["draft_pr"],
457459
}
458460
url = CREATE_PR_URL_TEMPLATE.format(config=self.config)
459461
response = requests.post(url, headers=request_headers, json=data, timeout=10)

cherry_picker/test_cherry_picker.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import warnings
88
from collections import ChainMap
99
from unittest import mock
10+
from unittest.mock import MagicMock
1011

1112
import click
1213
import pytest
@@ -495,6 +496,7 @@ def test_load_full_config(tmp_git_repo_dir, git_add, git_commit):
495496
"fix_commit_msg": True,
496497
"default_branch": "devel",
497498
"require_version_in_branch_name": True,
499+
"draft_pr": False,
498500
},
499501
)
500502

@@ -519,6 +521,7 @@ def test_load_partial_config(tmp_git_repo_dir, git_add, git_commit):
519521
"fix_commit_msg": True,
520522
"default_branch": "main",
521523
"require_version_in_branch_name": True,
524+
"draft_pr": False,
522525
},
523526
)
524527

@@ -548,6 +551,7 @@ def test_load_config_no_head_sha(tmp_git_repo_dir, git_add, git_commit):
548551
"fix_commit_msg": True,
549552
"default_branch": "devel",
550553
"require_version_in_branch_name": True,
554+
"draft_pr": False,
551555
},
552556
)
553557

@@ -1336,3 +1340,51 @@ def test_abort_cherry_pick_success(
13361340

13371341
def test_cli_invoked():
13381342
subprocess.check_call("cherry_picker --help".split())
1343+
1344+
1345+
@pytest.mark.parametrize("draft_pr", (True, False))
1346+
@mock.patch("requests.post")
1347+
@mock.patch("gidgethub.sansio.create_headers")
1348+
@mock.patch.object(CherryPicker, "username", new_callable=mock.PropertyMock)
1349+
def test_create_gh_pr_draft_states(
1350+
mock_username, mock_create_headers, mock_post, monkeypatch, draft_pr, config
1351+
):
1352+
config["draft_pr"] = draft_pr
1353+
mock_username.return_value = "username"
1354+
monkeypatch.setenv("GH_AUTH", "True")
1355+
with mock.patch("cherry_picker.cherry_picker.validate_sha", return_value=True):
1356+
cherry_picker = CherryPicker(
1357+
"origin", "xxx", [], prefix_commit=True, config=config
1358+
)
1359+
mock_create_headers.return_value = {"Authorization": "token gh-token"}
1360+
1361+
mock_response = MagicMock()
1362+
mock_response.status_code = 201
1363+
mock_response.json.return_value = {
1364+
"html_url": "https://github.com/octocat/Hello-World/pull/1347",
1365+
"number": 1347,
1366+
}
1367+
mock_post.return_value = mock_response
1368+
1369+
base_branch = "main"
1370+
head_branch = "feature-branch"
1371+
commit_message = "Commit message"
1372+
gh_auth = "gh_auth"
1373+
1374+
cherry_picker.create_gh_pr(
1375+
base_branch, head_branch, commit_message=commit_message, gh_auth=gh_auth
1376+
)
1377+
1378+
mock_post.assert_called_once_with(
1379+
"https://api.github.com/repos/python/cpython/pulls",
1380+
headers={"Authorization": "token gh-token"},
1381+
json={
1382+
"title": "Commit message",
1383+
"body": "",
1384+
"head": "username:feature-branch",
1385+
"base": "main",
1386+
"maintainer_can_modify": True,
1387+
"draft": draft_pr,
1388+
},
1389+
timeout=10,
1390+
)

0 commit comments

Comments
 (0)