Skip to content

Commit 3a83b51

Browse files
flichtenheldgonchik
authored andcommitted
Bitbucket Cloud: Add branches and tags repository endpoints (#921)
Note that this currently does not support the generic refs endpoint. Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
1 parent 8df7c18 commit 3a83b51

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

atlassian/bitbucket/cloud/repositories/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .defaultReviewers import DefaultReviewers
88
from .pipelines import Pipelines
99
from .pullRequests import PullRequests
10+
from .refs import Branches, Tags
1011

1112

1213
class RepositoriesBase(BitbucketCloudBase):
@@ -225,10 +226,12 @@ def __init__(self, data, *args, **kwargs):
225226
self.__branch_restrictions = BranchRestrictions(
226227
"{}/branch-restrictions".format(self.url), **self._new_session_args
227228
)
229+
self.__branches = Branches("{}/refs/branches".format(self.url), **self._new_session_args)
228230
self.__default_reviewers = DefaultReviewers("{}/default-reviewers".format(self.url), **self._new_session_args)
229231
self.__issues = Issues("{}/issues".format(self.url), **self._new_session_args)
230232
self.__pipelines = Pipelines("{}/pipelines".format(self.url), **self._new_session_args)
231233
self.__pullrequests = PullRequests("{}/pullrequests".format(self.url), **self._new_session_args)
234+
self.__tags = Tags("{}/refs/tags".format(self.url), **self._new_session_args)
232235

233236
def update(self, **kwargs):
234237
"""
@@ -329,6 +332,11 @@ def branch_restrictions(self):
329332
"""The repository branch restrictions"""
330333
return self.__branch_restrictions
331334

335+
@property
336+
def branches(self):
337+
"""The repository branches."""
338+
return self.__branches
339+
332340
@property
333341
def default_reviewers(self):
334342
"""The repository default reviewers"""
@@ -348,3 +356,8 @@ def pipelines(self):
348356
def pullrequests(self):
349357
"""The repository pull requests"""
350358
return self.__pullrequests
359+
360+
@property
361+
def tags(self):
362+
"""The repository tags."""
363+
return self.__tags
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# coding=utf-8
2+
3+
from ..base import BitbucketCloudBase
4+
from ..common.users import User
5+
6+
7+
class Refs(BitbucketCloudBase):
8+
"""
9+
Bitbucket Cloud Refs.
10+
11+
Generic base object for any type of ref list.
12+
"""
13+
14+
def __init__(self, url, *args, **kwargs):
15+
"""See BitbucketCloudBase."""
16+
super(Refs, self).__init__(url, *args, **kwargs)
17+
18+
def create(
19+
self,
20+
name,
21+
commit,
22+
):
23+
"""
24+
Creates a ref with the given target commit
25+
26+
:param name: string: name
27+
:param commit: string: commit hash
28+
29+
:return: Ref
30+
"""
31+
32+
data = {"name": name, "target": {"hash": commit}}
33+
34+
return self._get_object(self.post(None, data))
35+
36+
def each(self, q=None, sort=None):
37+
"""
38+
Returns the list of refs in this repository.
39+
40+
:param q: string: Query string to narrow down the response.
41+
See https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering for details.
42+
:param sort: string: Name of a response property to sort results.
43+
See https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering for details.
44+
45+
:return: A generator for the Ref objects
46+
"""
47+
params = {}
48+
if sort is not None:
49+
params["sort"] = sort
50+
if q is not None:
51+
params["q"] = q
52+
for ref in self._get_paged(None, trailing=True, params=params):
53+
yield self._get_object(super(Refs, self).get(ref.get("name")))
54+
55+
return
56+
57+
def get(self, name):
58+
"""
59+
Returns the Ref with the requested name in the repository.
60+
61+
:param name: string: The requested name
62+
63+
:return: The requested Ref object
64+
"""
65+
return self._get_object(super(Refs, self).get(name))
66+
67+
68+
class Branches(Refs):
69+
"""
70+
BitBucket Cloud branches endpoint.
71+
72+
See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-branches-get
73+
"""
74+
75+
def _get_object(self, data):
76+
return Branch(data, **self._new_session_args)
77+
78+
79+
class Tags(Refs):
80+
"""
81+
BitBucket Cloud tags endpoint.
82+
83+
See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-tags-get
84+
"""
85+
86+
def _get_object(self, data):
87+
return Tag(data, **self._new_session_args)
88+
89+
90+
class Ref(BitbucketCloudBase):
91+
"""
92+
Base object for individual refs.
93+
"""
94+
95+
@property
96+
def name(self):
97+
"""Ref name."""
98+
return self.get_data("name")
99+
100+
@property
101+
def hash(self):
102+
"""Commit hash."""
103+
return self.get_data("target")["hash"]
104+
105+
106+
class Branch(Ref):
107+
"""
108+
Bitbucket Cloud branch endpoint.
109+
110+
See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-branches-name-get
111+
"""
112+
113+
def __init__(self, data, *args, **kwargs):
114+
"""See BitbucketCloudBase."""
115+
super(Branch, self).__init__(None, *args, data=data, expected_type="branch", **kwargs)
116+
117+
@property
118+
def author(self):
119+
"""User object of the author."""
120+
return User(None, self.get_data("author"))
121+
122+
123+
class Tag(Ref):
124+
"""
125+
Bitbucket Cloud tags endpoint.
126+
127+
See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-tags-name-get
128+
"""
129+
130+
def __init__(self, data, *args, **kwargs):
131+
"""See BitbucketCloudBase."""
132+
super(Tag, self).__init__(None, *args, data=data, expected_type="tag", **kwargs)
133+
134+
@property
135+
def author(self):
136+
"""User object of the author."""
137+
return User(None, self.get_data("tagger"))

0 commit comments

Comments
 (0)