Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import sys
import webbrowser

import requests
import stamina
from gidgethub import sansio
import urllib3

from . import __version__

Expand Down Expand Up @@ -462,7 +461,12 @@ def create_gh_pr(self, base_branch, head_branch, *, commit_message, gh_auth):
"""
Create PR in GitHub
"""
request_headers = sansio.create_headers(self.username, oauth_token=gh_auth)
request_headers = {
"User-Agent": self.username,
"Accept": "application/vnd.github+json",
"Authorization": f"token {gh_auth}",
"X-GitHub-Api-Version": "2022-11-28",
}
title, body = normalize_commit_message(commit_message)
if not self.prefix_commit:
title = remove_commit_prefix(title)
Expand All @@ -477,15 +481,15 @@ def create_gh_pr(self, base_branch, head_branch, *, commit_message, gh_auth):
}
url = CREATE_PR_URL_TEMPLATE.format(config=self.config)
try:
response = requests.post(
url, headers=request_headers, json=data, timeout=30
response = urllib3.request(
"POST", url, headers=request_headers, json=data, timeout=30
)
except requests.exceptions.RequestException as req_exc:
except urllib3.exceptions.HTTPError as req_exc:
raise GitHubException(f"Creating PR on GitHub failed: {req_exc}")
else:
sc = response.status_code
txt = response.text
if sc != requests.codes.created:
sc = response.status
if sc != 201:
txt = response.data.decode("utf-8", errors="replace")
raise GitHubException(
f"Unexpected response ({sc}) when creating PR on GitHub: {txt}"
)
Expand Down
21 changes: 12 additions & 9 deletions cherry_picker/test_cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,11 +1331,10 @@ def test_cli_invoked():


@pytest.mark.parametrize("draft_pr", (True, False))
@mock.patch("requests.post")
@mock.patch("gidgethub.sansio.create_headers")
@mock.patch("urllib3.request")
@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
mock_username, mock_request, monkeypatch, draft_pr, config
):
config["draft_pr"] = draft_pr
mock_username.return_value = "username"
Expand All @@ -1344,15 +1343,13 @@ def test_create_gh_pr_draft_states(
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.status = 201
mock_response.json.return_value = {
"html_url": "https://github.com/octocat/Hello-World/pull/1347",
"number": 1347,
}
mock_post.return_value = mock_response
mock_request.return_value = mock_response

base_branch = "main"
head_branch = "feature-branch"
Expand All @@ -1363,9 +1360,15 @@ def test_create_gh_pr_draft_states(
base_branch, head_branch, commit_message=commit_message, gh_auth=gh_auth
)

mock_post.assert_called_once_with(
mock_request.assert_called_once_with(
"POST",
"https://api.github.com/repos/python/cpython/pulls",
headers={"Authorization": "token gh-token"},
headers={
"User-Agent": "username",
"Accept": "application/vnd.github+json",
"Authorization": "token gh_auth",
"X-GitHub-Api-Version": "2022-11-28",
},
json={
"title": "Commit message",
"body": "",
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ classifiers = [
]
dynamic = [ "version" ]
dependencies = [
"gidgethub",
"requests",
"stamina",
"tomli>=1.1; python_version<'3.11'",
"urllib3>=2",
]
optional-dependencies.dev = [
"pytest",
Expand Down