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