Skip to content

Commit

Permalink
Commit Object addition to Pull Requests - #1251 (#1264)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
isaac-philip authored Dec 6, 2023
1 parent 7821e83 commit 65185cb
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
7 changes: 7 additions & 0 deletions atlassian/bitbucket/cloud/repositories/pullRequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ..base import BitbucketCloudBase
from .diffstat import DiffStat
from ...cloud.repositories.commits import Commit
from ..common.builds import Build
from ..common.comments import Comment
from ..common.users import User, Participant
Expand Down Expand Up @@ -311,6 +312,12 @@ def comment(self, raw_message):

return self.post("comments", data)

@property
def commits(self):
"""Returns generator object for the Commits in the PullRequest"""
for commit in self._get_paged("commits"):
yield Commit(commit, **self._new_session_args)

def tasks(self):
"""
Returns generator object of the tasks endpoint
Expand Down
12 changes: 12 additions & 0 deletions examples/bitbucket/bitbucket_pr_commits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from atlassian.bitbucket import Cloud

bitbucket = Cloud(url='https://api.bitbucket.org/', token='random_string_token')

pr_id = 1

if __name__ == "__main__":
data = bitbucket.repositories.get(workspace="workspace1", repo_slug="repository1")
pr = data.pullrequests.get(pr_id)
print(f"For Pull Request ID = {pr.id}")
for pr_commit in pr.commits:
print(f"Commit Id={pr_commit.hash}, Message={pr_commit.message}")
Empty file added temp_file.md
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
responses[None] = {
"values": [
{
"type": "commit",
"hash": "3a8d6c7910f7bf68f7cf9cc1964998006fa8894b",
"date": "2023-10-08T17:31:20+00:00",
"author": {
"type": "author",
"raw": "user1Name <[email protected]>",
"user": {
"display_name": "user1Name",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/users/%7B34318d8b-6c8a-40a7-aea2-572fdd183917%7D"
},
"avatar": {
"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"
},
"html": {
"href": "https://bitbucket.org/%7B34318d8b-6c8a-40a7-aea2-572fdd183917%7D/"
}
},
"type": "user",
"uuid": "{34318d8b-6c8a-40a7-aea2-572fdd183917}",
"account_id": "557058:e7042a01-54b7-447f-9da8-4ad1b1e4afa5",
"nickname": "user1Name"
}
},
"message": "src created online with Bitbucket",
"summary": {
"type": "rendered",
"raw": "src created online with Bitbucket",
"markup": "markdown",
"html": "<p>src created online with Bitbucket</p>"
},
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/commit/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b"
},
"html": {
"href": "https://bitbucket.org/TestWorkspace1/testrepository1/commits/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b"
},
"diff": {
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/diff/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b"
},
"approve": {
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/commit/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b/approve"
},
"comments": {
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/commit/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b/comments"
},
"statuses": {
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/commit/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b/statuses"
},
"patch": {
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/patch/3a8d6c7910f7bf68f7cf9cc1964998006fa8894b"
}
},
"parents": [
{
"hash": "08b01c378b0c67999528bdb5d364d1960409cd99",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1/commit/08b01c378b0c67999528bdb5d364d1960409cd99"
},
"html": {
"href": "https://bitbucket.org/TestWorkspace1/testrepository1/commits/08b01c378b0c67999528bdb5d364d1960409cd99"
}
},
"type": "commit"
}
],
"repository": {
"type": "repository",
"full_name": "TestWorkspace1/testrepository1",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/TestWorkspace1/testrepository1"
},
"html": {
"href": "https://bitbucket.org/TestWorkspace1/testrepository1"
},
"avatar": {
"href": "https://bytebucket.org/ravatar/%7Bc4f5c559-de42-4b98-8502-4d73ee76c393%7D?ts=default"
}
},
"name": "testrepository1",
"uuid": "{n789754-de42-4b98-8502-4d73ee76c393}"
}
}
],
"pagelen": 10,
"page": 1
}
14 changes: 13 additions & 1 deletion tests/test_bitbucket_cloud_oo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from atlassian import Bitbucket
from atlassian.bitbucket import Cloud
from atlassian.bitbucket.cloud.common.users import User
from atlassian.bitbucket.cloud.repositories.pullRequests import Comment, Participant, PullRequest, Build, Task
from atlassian.bitbucket.cloud.repositories.pullRequests import Comment, Commit, Participant, PullRequest, Build, Task

BITBUCKET = None
try:
Expand Down Expand Up @@ -173,6 +173,10 @@ def tc1(self):
def tc2(self):
return CLOUD.workspaces.get("TestWorkspace1").repositories.get("testrepository1").pullrequests

@pytest.fixture(scope="module")
def tc3(self):
return CLOUD.workspaces.get("TestWorkspace1").repositories.get("testrepository1").pullrequests.get(1).commits

def test_id(self, tc1):
assert tc1.id == 1

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

def test_commits(self, tc1):
pr_commits = list(tc1.commits)
assert len(pr_commits) == 1
commit_1 = pr_commits[0]
assert isinstance(commit_1, Commit)
assert commit_1.message == 'src created online with Bitbucket'


def test_comment(self, tc1):
msg = "hello world"
com = tc1.comment(msg)
Expand Down

0 comments on commit 65185cb

Please sign in to comment.