Skip to content

Commit 65185cb

Browse files
authored
Commit Object addition to Pull Requests - #1251 (#1264)
* PullRequests to return the commits too - #1251 * examples for PR commits added - #1251 * TestCase added for PR commits - #1251 * temporary commit to trigger pipeline for error display
1 parent 7821e83 commit 65185cb

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

atlassian/bitbucket/cloud/repositories/pullRequests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from ..base import BitbucketCloudBase
44
from .diffstat import DiffStat
5+
from ...cloud.repositories.commits import Commit
56
from ..common.builds import Build
67
from ..common.comments import Comment
78
from ..common.users import User, Participant
@@ -311,6 +312,12 @@ def comment(self, raw_message):
311312

312313
return self.post("comments", data)
313314

315+
@property
316+
def commits(self):
317+
"""Returns generator object for the Commits in the PullRequest"""
318+
for commit in self._get_paged("commits"):
319+
yield Commit(commit, **self._new_session_args)
320+
314321
def tasks(self):
315322
"""
316323
Returns generator object of the tasks endpoint
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from atlassian.bitbucket import Cloud
2+
3+
bitbucket = Cloud(url='https://api.bitbucket.org/', token='random_string_token')
4+
5+
pr_id = 1
6+
7+
if __name__ == "__main__":
8+
data = bitbucket.repositories.get(workspace="workspace1", repo_slug="repository1")
9+
pr = data.pullrequests.get(pr_id)
10+
print(f"For Pull Request ID = {pr.id}")
11+
for pr_commit in pr.commits:
12+
print(f"Commit Id={pr_commit.hash}, Message={pr_commit.message}")

temp_file.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
responses[None] = {
2+
"values": [
3+
{
4+
"type": "commit",
5+
"hash": "3a8d6c7910f7bf68f7cf9cc1964998006fa8894b",
6+
"date": "2023-10-08T17:31:20+00:00",
7+
"author": {
8+
"type": "author",
9+
"raw": "user1Name <user@mail.com>",
10+
"user": {
11+
"display_name": "user1Name",
12+
"links": {
13+
"self": {
14+
"href": "https://api.bitbucket.org/2.0/users/%7B34318d8b-6c8a-40a7-aea2-572fdd183917%7D"
15+
},
16+
"avatar": {
17+
"href": "https://secure.gravatar.com/avatar/679c684730037c1250b46c1781701f98?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FIP-3.png"
18+
},
19+
"html": {
20+
"href": "https://bitbucket.org/%7B34318d8b-6c8a-40a7-aea2-572fdd183917%7D/"
21+
}
22+
},
23+
"type": "user",
24+
"uuid": "{34318d8b-6c8a-40a7-aea2-572fdd183917}",
25+
"account_id": "557058:e7042a01-54b7-447f-9da8-4ad1b1e4afa5",
26+
"nickname": "user1Name"
27+
}
28+
},
29+
"message": "src created online with Bitbucket",
30+
"summary": {
31+
"type": "rendered",
32+
"raw": "src created online with Bitbucket",
33+
"markup": "markdown",
34+
"html": "<p>src created online with Bitbucket</p>"
35+
},
36+
"links": {
37+
"self": {
38+
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/commit/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b"
39+
},
40+
"html": {
41+
"href": "https://bitbucket.org/TestWorkspace1/testrepository1/commits/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b"
42+
},
43+
"diff": {
44+
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/diff/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b"
45+
},
46+
"approve": {
47+
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/commit/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b/approve"
48+
},
49+
"comments": {
50+
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/commit/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b/comments"
51+
},
52+
"statuses": {
53+
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/commit/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b/statuses"
54+
},
55+
"patch": {
56+
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/patch/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b"
57+
}
58+
},
59+
"parents": [
60+
{
61+
"hash": "08b01c378b0c67999528bdb5d364d1960409cd99",
62+
"links": {
63+
"self": {
64+
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/commit/08b01c378b0c67999528bdb5d364d1960409cd99"
65+
},
66+
"html": {
67+
"href": "https://bitbucket.org/TestWorkspace1/testrepository1/commits/08b01c378b0c67999528bdb5d364d1960409cd99"
68+
}
69+
},
70+
"type": "commit"
71+
}
72+
],
73+
"repository": {
74+
"type": "repository",
75+
"full_name": "TestWorkspace1/testrepository1",
76+
"links": {
77+
"self": {
78+
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1"
79+
},
80+
"html": {
81+
"href": "https://bitbucket.org/TestWorkspace1/testrepository1"
82+
},
83+
"avatar": {
84+
"href": "https://bytebucket.org/ravatar/%7Bc4f5c559-de42-4b98-8502-4d73ee76c393%7D?ts=default"
85+
}
86+
},
87+
"name": "testrepository1",
88+
"uuid": "{n789754-de42-4b98-8502-4d73ee76c393}"
89+
}
90+
}
91+
],
92+
"pagelen": 10,
93+
"page": 1
94+
}

tests/test_bitbucket_cloud_oo.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from atlassian import Bitbucket
88
from atlassian.bitbucket import Cloud
99
from atlassian.bitbucket.cloud.common.users import User
10-
from atlassian.bitbucket.cloud.repositories.pullRequests import Comment, Participant, PullRequest, Build, Task
10+
from atlassian.bitbucket.cloud.repositories.pullRequests import Comment, Commit, Participant, PullRequest, Build, Task
1111

1212
BITBUCKET = None
1313
try:
@@ -173,6 +173,10 @@ def tc1(self):
173173
def tc2(self):
174174
return CLOUD.workspaces.get("TestWorkspace1").repositories.get("testrepository1").pullrequests
175175

176+
@pytest.fixture(scope="module")
177+
def tc3(self):
178+
return CLOUD.workspaces.get("TestWorkspace1").repositories.get("testrepository1").pullrequests.get(1).commits
179+
176180
def test_id(self, tc1):
177181
assert tc1.id == 1
178182

@@ -263,6 +267,14 @@ def test_reviewers(self, tc1):
263267
assert len(reviewers) == 3
264268
assert isinstance(reviewers[0], User)
265269

270+
def test_commits(self, tc1):
271+
pr_commits = list(tc1.commits)
272+
assert len(pr_commits) == 1
273+
commit_1 = pr_commits[0]
274+
assert isinstance(commit_1, Commit)
275+
assert commit_1.message == 'src created online with Bitbucket'
276+
277+
266278
def test_comment(self, tc1):
267279
msg = "hello world"
268280
com = tc1.comment(msg)

0 commit comments

Comments
 (0)