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