Skip to content

Commit cbabdf3

Browse files
authoredDec 31, 2020
create a pull request (#669)
·
4.0.42.4.0
1 parent 61affb0 commit cbabdf3

File tree

3 files changed

+306
-6
lines changed

3 files changed

+306
-6
lines changed
 

‎atlassian/bitbucket/cloud/repositories/pullRequests.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,43 @@ def get(self, id):
5050
"""
5151
return self.__get_object(super(PullRequests, self).get(id))
5252

53-
# def add(self):
54-
# TODO add a new pull request
55-
# TODO https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests#post
56-
# return PullRequest object
53+
def create(
54+
self,
55+
title,
56+
source_branch,
57+
destination_branch=None,
58+
description=None,
59+
close_source_branch=None,
60+
reviewers=None,
61+
):
62+
"""
63+
Creates a new pull requests for a given source branch
64+
Be careful, adding this mulitple times for the same source branch updates the pull request!
65+
66+
:param title: string: pull request title
67+
:param source_branch: string: name of the source branch
68+
:param destination_branch: string: name of the destination branch, if None the repository main branch is used
69+
:param description: string: pull request description
70+
:param close_source_branch: bool: specifies if the source branch should be closed upon merging
71+
:param reviewers: list: list of user uuids in curly brackets
72+
:return: Pull Request Object
73+
74+
API docs: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests#post
75+
"""
76+
77+
rv = [{"uuid": x} for x in reviewers] if reviewers else []
78+
data = {
79+
"title": title,
80+
"source": {"branch": {"name": source_branch}},
81+
"description": description,
82+
"close_source_branch": close_source_branch,
83+
"reviewers": rv,
84+
}
85+
if destination_branch:
86+
data["destination"] = {"branch": {"name": destination_branch}}
87+
response = self.post(self.url, data, absolute=True)
88+
89+
return PullRequest(response["links"]["self"], response, **self._new_session_args)
5790

5891

5992
class PullRequest(BitbucketCloudBase):
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
responses[
2+
'{"title": "PRTitle", "source": {"branch": {"name": "feature/test-branch"}}, "description": "PRDescription", "close_source_branch": true, "reviewers": [{"uuid": "{User04UUID}"}, {"uuid": "{User02UUID}"}, {"uuid": "{User01UUID}"}], "destination": {"branch": {"name": "master"}}}'
3+
] = {
4+
"rendered": {
5+
"description": {
6+
"raw": "PRDescription",
7+
"markup": "markdown",
8+
"html": "<p>PRDescription</p>",
9+
"type": "rendered",
10+
},
11+
"title": {"raw": "PRTitle", "markup": "markdown", "html": "<p>PRTitle</p>", "type": "rendered"},
12+
},
13+
"type": "pullrequest",
14+
"description": "PRDescription",
15+
"links": {
16+
"decline": {"href": "repositories/TestWorkspace1/testrepository1/pullrequests/1/decline"},
17+
"diffstat": {
18+
"href": "repositories/TestWorkspace1/testrepository1/diffstat/TestWorkspace1/testrepository1:16182c4698fb%0D1fbd047cd123?from_pullrequest_id=1"
19+
},
20+
"commits": {"href": "repositories/TestWorkspace1/testrepository1/pullrequests/1/commits"},
21+
"self": {"href": "repositories/TestWorkspace1/testrepository1/pullrequests/1"},
22+
"comments": {"href": "repositories/TestWorkspace1/testrepository1/pullrequests/1/comments"},
23+
"merge": {"href": "repositories/TestWorkspace1/testrepository1/pullrequests/1/merge"},
24+
"html": {"href": "https://bitbucket.org/TestWorkspace1/testrepository1/pull-requests/1"},
25+
"activity": {"href": "repositories/TestWorkspace1/testrepository1/pullrequests/1/activity"},
26+
"request-changes": {"href": "repositories/TestWorkspace1/testrepository1/pullrequests/1/request-changes"},
27+
"diff": {
28+
"href": "repositories/TestWorkspace1/testrepository1/diff/TestWorkspace1/testrepository1:16182c4698fb%0D1fbd047cd123?from_pullrequest_id=1"
29+
},
30+
"approve": {"href": "repositories/TestWorkspace1/testrepository1/pullrequests/1/approve"},
31+
"statuses": {"href": "repositories/TestWorkspace1/testrepository1/pullrequests/1/statuses"},
32+
},
33+
"title": "PRTitle",
34+
"close_source_branch": true,
35+
"reviewers": [
36+
{
37+
"display_name": "User04DisplayName",
38+
"uuid": "{User04UUID}",
39+
"links": {
40+
"self": {"href": "users/%7BUser04UUID%7D"},
41+
"html": {"href": "https://bitbucket.org/%7BUser04UUID%7D/"},
42+
"avatar": {
43+
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU4-2.png"
44+
},
45+
},
46+
"nickname": "User04Nickname",
47+
"type": "user",
48+
"account_id": "User04AccountID",
49+
},
50+
{
51+
"display_name": "User02DisplayName",
52+
"uuid": "{User02UUID}",
53+
"links": {
54+
"self": {"href": "users/%7BUser02UUID%7D"},
55+
"html": {"href": "https://bitbucket.org/%7BUser02UUID%7D/"},
56+
"avatar": {
57+
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU2-3.png"
58+
},
59+
},
60+
"nickname": "User02Nickname",
61+
"type": "user",
62+
"account_id": "User02AccountID",
63+
},
64+
{
65+
"display_name": "User01DisplayName",
66+
"uuid": "{User01UUID}",
67+
"links": {
68+
"self": {"href": "users/%7BUser01UUID%7D"},
69+
"html": {"href": "https://bitbucket.org/%7BUser01UUID%7D/"},
70+
"avatar": {
71+
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU1-3.png"
72+
},
73+
},
74+
"nickname": "User01Nickname",
75+
"type": "user",
76+
"account_id": "User01AccountID",
77+
},
78+
],
79+
"id": 1,
80+
"destination": {
81+
"commit": {
82+
"hash": "1fbd047cd99a",
83+
"type": "commit",
84+
"links": {
85+
"self": {"href": "repositories/TestWorkspace1/testrepository1/commit/1fbd047cd99a"},
86+
"html": {"href": "https://bitbucket.org/TestWorkspace1/testrepository1/commits/1fbd047cd99a"},
87+
},
88+
},
89+
"repository": {
90+
"links": {
91+
"self": {"href": "repositories/TestWorkspace1/testrepository1"},
92+
"html": {"href": "https://bitbucket.org/TestWorkspace1/testrepository1"},
93+
"avatar": {"href": "https://bytebucket.org/ravatar/%7BRepoUUID%7D?ts=default"},
94+
},
95+
"type": "repository",
96+
"name": "testrepository1",
97+
"full_name": "TestWorkspace1/testrepository1",
98+
"uuid": "{RepoUUID}",
99+
},
100+
"branch": {"name": "master"},
101+
},
102+
"created_on": "2020-03-19T12:00:03.494356+00:00",
103+
"summary": {"raw": "PRDescription", "markup": "markdown", "html": "<p>PRDescription</p>", "type": "rendered"},
104+
"source": {
105+
"commit": {
106+
"hash": "16182c4123fb",
107+
"type": "commit",
108+
"links": {
109+
"self": {"href": "repositories/TestWorkspace1/testrepository1/commit/16182c4123fb"},
110+
"html": {"href": "https://bitbucket.org/TestWorkspace1/testrepository1/commits/16182c4123fb"},
111+
},
112+
},
113+
"repository": {
114+
"links": {
115+
"self": {"href": "repositories/TestWorkspace1/testrepository1"},
116+
"html": {"href": "https://bitbucket.org/TestWorkspace1/testrepository1"},
117+
"avatar": {"href": "https://bytebucket.org/ravatar/%7BRepoUUID%7D?ts=default"},
118+
},
119+
"type": "repository",
120+
"name": "testrepository1",
121+
"full_name": "TestWorkspace1/testrepository1",
122+
"uuid": "{RepoUUID}",
123+
},
124+
"branch": {"name": "feature/test-branch"},
125+
},
126+
"comment_count": 5,
127+
"state": "OPEN",
128+
"task_count": 0,
129+
"participants": [
130+
{
131+
"participated_on": "2020-03-19T14:29:24.928709+00:00",
132+
"state": null,
133+
"role": "PARTICIPANT",
134+
"user": {
135+
"display_name": "User05DisplayName",
136+
"uuid": "{User05UUID}",
137+
"links": {
138+
"self": {"href": "users/%7BUser05UUID%7D"},
139+
"html": {"href": "https://bitbucket.org/%7BUser05UUID%7D/"},
140+
"avatar": {
141+
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2U5-5.png"
142+
},
143+
},
144+
"nickname": "User05Nickname",
145+
"type": "user",
146+
"account_id": "User05AccountID",
147+
},
148+
"type": "participant",
149+
"approved": false,
150+
},
151+
{
152+
"participated_on": "2020-07-09T07:00:54.416331+00:00",
153+
"state": null,
154+
"role": "PARTICIPANT",
155+
"user": {
156+
"display_name": "User03DisplayName",
157+
"uuid": "{User03UUID}",
158+
"links": {
159+
"self": {"href": "users/%7BUser03UUID%7D"},
160+
"html": {"href": "https://bitbucket.org/%7BUser03UUID%7D/"},
161+
"avatar": {
162+
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU3-0.png"
163+
},
164+
},
165+
"nickname": "User03Nickname",
166+
"type": "user",
167+
"account_id": "User03AccountID",
168+
},
169+
"type": "participant",
170+
"approved": false,
171+
},
172+
{
173+
"participated_on": "2020-12-27T14:09:14.660262+00:00",
174+
"state": "approved",
175+
"role": "REVIEWER",
176+
"user": {
177+
"display_name": "User04DisplayName",
178+
"uuid": "{User04UUID}",
179+
"links": {
180+
"self": {"href": "users/%7BUser04UUID%7D"},
181+
"html": {"href": "https://bitbucket.org/%7BUser04UUID%7D/"},
182+
"avatar": {
183+
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU4-2.png"
184+
},
185+
},
186+
"nickname": "User04Nickname",
187+
"type": "user",
188+
"account_id": "User04AccountID",
189+
},
190+
"type": "participant",
191+
"approved": true,
192+
},
193+
{
194+
"participated_on": "2020-12-27T14:05:58.999476+00:00",
195+
"state": "changes_requested",
196+
"role": "REVIEWER",
197+
"user": {
198+
"display_name": "User01DisplayName",
199+
"uuid": "{User01UUID}",
200+
"links": {
201+
"self": {"href": "users/%7BUser01UUID%7D"},
202+
"html": {"href": "https://bitbucket.org/%7BUser01UUID%7D/"},
203+
"avatar": {
204+
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU1-3.png"
205+
},
206+
},
207+
"nickname": "User01Nickname",
208+
"type": "user",
209+
"account_id": "User01AccountID",
210+
},
211+
"type": "participant",
212+
"approved": false,
213+
},
214+
{
215+
"participated_on": null,
216+
"state": null,
217+
"role": "REVIEWER",
218+
"user": {
219+
"display_name": "User02DisplayName",
220+
"uuid": "{User02UUID}",
221+
"links": {
222+
"self": {"href": "users/%7BUser02UUID%7D"},
223+
"html": {"href": "https://bitbucket.org/%7BUser02UUID%7D/"},
224+
"avatar": {
225+
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU2-3.png"
226+
},
227+
},
228+
"nickname": "User02Nickname",
229+
"type": "user",
230+
"account_id": "User02AccountID",
231+
},
232+
"type": "participant",
233+
"approved": false,
234+
},
235+
],
236+
"reason": "",
237+
"updated_on": "2020-12-27T14:09:14.660262+00:00",
238+
"author": {
239+
"display_name": "User03DisplayName",
240+
"uuid": "{User03UUID}",
241+
"links": {
242+
"self": {"href": "users/%7BUser03UUID%7D"},
243+
"html": {"href": "https://bitbucket.org/%7BUser03UUID%7D/"},
244+
"avatar": {
245+
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU3-0.png"
246+
},
247+
},
248+
"nickname": "User03Nickname",
249+
"type": "user",
250+
"account_id": "User03AccountID",
251+
},
252+
"merge_commit": null,
253+
"closed_by": null,
254+
}

‎tests/test_bitbucket_cloud.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def tc1(self):
166166

167167
@pytest.fixture(scope="module")
168168
def tc2(self):
169-
return CLOUD.workspaces.get("TestWorkspace1").repositories.get("testrepository1").pullrequests.each()
169+
return CLOUD.workspaces.get("TestWorkspace1").repositories.get("testrepository1").pullrequests
170170

171171
def test_id(self, tc1):
172172
assert tc1.id == 1
@@ -293,8 +293,21 @@ def test_merge(self, tc1):
293293
assert merge["merge_commit"]["hash"] == "36bb9607a8c9e0c6222342486e3393ae154b46c0"
294294

295295
def test_each(self, tc2):
296-
prs = list(tc2)
296+
prs = list(tc2.each())
297297
assert len(prs) == 2
298298
assert isinstance(prs[0], PullRequest)
299299
assert prs[0].id == 1
300300
assert prs[1].id == 25
301+
302+
def test_create(self, tc2):
303+
reviewers = ["{User04UUID}", "{User02UUID}", "{User01UUID}"]
304+
pr = tc2.create(
305+
title="PRTitle",
306+
source_branch="feature/test-branch",
307+
destination_branch="master",
308+
description="PRDescription",
309+
close_source_branch=True,
310+
reviewers=reviewers,
311+
)
312+
assert pr.id == 1
313+
assert len(list(pr.reviewers())) == 3

0 commit comments

Comments
 (0)
Please sign in to comment.