Skip to content

Commit 7346d8a

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 7346d8a

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

atlassian/bitbucket/cloud/repositories/__init__.py

Lines changed: 14 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,9 @@ 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
364+
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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 = {
32+
"name": name,
33+
"target": { "hash": commit }
34+
}
35+
36+
return self._get_object(self.post(None, data))
37+
38+
def each(self, q=None, sort=None):
39+
"""
40+
Returns the list of refs in this repository.
41+
42+
:param q: string: Query string to narrow down the response.
43+
See https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering for details.
44+
:param sort: string: Name of a response property to sort results.
45+
See https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering for details.
46+
47+
:return: A generator for the Ref objects
48+
"""
49+
params = {}
50+
if sort is not None:
51+
params["sort"] = sort
52+
if q is not None:
53+
params["q"] = q
54+
for ref in self._get_paged(None, trailing=True, params=params):
55+
yield self._get_object(super(Refs, self).get(ref.get("name")))
56+
57+
return
58+
59+
def get(self, name):
60+
"""
61+
Returns the Ref with the requested name in the repository.
62+
63+
:param name: string: The requested name
64+
65+
:return: The requested Ref object
66+
"""
67+
return self._get_object(super(Refs, self).get(name))
68+
69+
70+
class Branches(Refs):
71+
"""
72+
BitBucket Cloud branches endpoint.
73+
74+
See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-branches-get
75+
"""
76+
77+
def _get_object(self, data):
78+
return Branch(data, **self._new_session_args)
79+
80+
class Tags(Refs):
81+
"""
82+
BitBucket Cloud tags endpoint.
83+
84+
See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-tags-get
85+
"""
86+
87+
def _get_object(self, data):
88+
return Tag(data, **self._new_session_args)
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+
super(Branch, self).__init__(None, *args, data=data, expected_type="branch", **kwargs)
115+
116+
@property
117+
def author(self):
118+
"""User object of the author."""
119+
return User(None, self.get_data("author"))
120+
121+
122+
class Tag(Ref):
123+
"""
124+
Bitbucket Cloud tags endpoint.
125+
126+
See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-tags-name-get
127+
"""
128+
129+
def __init__(self, data, *args, **kwargs):
130+
super(Tag, self).__init__(None, *args, data=data, expected_type="tag", **kwargs)
131+
132+
@property
133+
def author(self):
134+
"""User object of the author."""
135+
return User(None, self.get_data("tagger"))
136+
137+
138+

0 commit comments

Comments
 (0)