Skip to content

Commit e6d2e80

Browse files
committed
Bitbucket Cloud: Add branches and tags repository endpoints
Note that this currently does not support the generic refs endpoint. Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
1 parent f99f094 commit e6d2e80

File tree

2 files changed

+147
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)