|
| 1 | +# coding=utf-8 |
| 2 | + |
| 3 | +from ..base import BitbucketCloudBase |
| 4 | +from ..common.builds import Build |
| 5 | +from ..common.comments import Comment |
| 6 | +from ..common.users import User, Participant |
| 7 | + |
| 8 | + |
| 9 | +class Commits(BitbucketCloudBase): |
| 10 | + """Bitbucket Cloud commits.""" |
| 11 | + |
| 12 | + def __init__(self, url, *args, **kwargs): |
| 13 | + super(Commits, self).__init__(url, *args, **kwargs) |
| 14 | + |
| 15 | + def __get_object(self, data): |
| 16 | + return Commit(data, **self._new_session_args) |
| 17 | + |
| 18 | + def each(self, top=None, q=None, sort=None): |
| 19 | + """ |
| 20 | + Return the list of commits in this repository. |
| 21 | +
|
| 22 | + :param top: string: Hash of commit to get the history for. |
| 23 | + :param q: string: Query string to narrow down the response. |
| 24 | + See https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering for details. |
| 25 | + :param sort: string: Name of a response property to sort results. |
| 26 | + See https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering for details. |
| 27 | +
|
| 28 | + :return: A generator for the Commit objects |
| 29 | +
|
| 30 | + API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commits-get |
| 31 | + """ |
| 32 | + params = {} |
| 33 | + if sort is not None: |
| 34 | + params["sort"] = sort |
| 35 | + if q is not None: |
| 36 | + params["q"] = q |
| 37 | + for commit in self._get_paged(top, trailing=True, params=params): |
| 38 | + yield self.__get_object(super(Commits, self).get(commit.get("hash"))) |
| 39 | + |
| 40 | + def get(self, commit_hash): |
| 41 | + """ |
| 42 | + Return the commit with the requested commit id in this repository. |
| 43 | +
|
| 44 | + :param commit_hash: str: The requested commit id |
| 45 | +
|
| 46 | + :return: The requested Commit object |
| 47 | +
|
| 48 | + API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commit-commit-get |
| 49 | + """ |
| 50 | + return self.__get_object( |
| 51 | + super(Commits, self).get(self.url_joiner(self.get_link("commit"), commit_hash), absolute=True) |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +class Commit(BitbucketCloudBase): |
| 56 | + """ |
| 57 | + Bitbucket Cloud commit endpoint. |
| 58 | +
|
| 59 | + See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commit-commit-get |
| 60 | + """ |
| 61 | + |
| 62 | + def __init__(self, data, *args, **kwargs): |
| 63 | + super(Commit, self).__init__(None, *args, data=data, expected_type="commit", **kwargs) |
| 64 | + |
| 65 | + @property |
| 66 | + def hash(self): |
| 67 | + """Commit id.""" |
| 68 | + return self.get_data("hash") |
| 69 | + |
| 70 | + @property |
| 71 | + def message(self): |
| 72 | + """Commit message.""" |
| 73 | + return self.get_data("title") |
| 74 | + |
| 75 | + @property |
| 76 | + def date(self): |
| 77 | + """Commit date.""" |
| 78 | + return self.get_time("date") |
| 79 | + |
| 80 | + @property |
| 81 | + def author(self): |
| 82 | + """User object of the author.""" |
| 83 | + return User(None, self.get_data("author")) |
| 84 | + |
| 85 | + def parents(self): |
| 86 | + """Return a generator object of parent commits.""" |
| 87 | + for commit in self.get_data("parents"): |
| 88 | + yield Commit(commit, **self._new_session_args) |
| 89 | + |
| 90 | + def statuses(self): |
| 91 | + """ |
| 92 | + Return generator object of the statuses endpoint. |
| 93 | +
|
| 94 | + API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commit-statuses/#api-repositories-workspace-repo-slug-commit-commit-statuses-get |
| 95 | + """ |
| 96 | + return self._get_paged("statuses") |
| 97 | + |
| 98 | + def participants(self): |
| 99 | + """Return a generator object of participants.""" |
| 100 | + for participant in self.get_data("participants"): |
| 101 | + yield Participant(participant, **self._new_session_args) |
| 102 | + |
| 103 | + def builds(self): |
| 104 | + """Return the Build objects for the commit.""" |
| 105 | + builds = [b for b in self.statuses() if b["type"] == "build"] |
| 106 | + for build in builds: |
| 107 | + yield Build(build, **self._new_session_args) |
| 108 | + |
| 109 | + def add_build(self, key, url=None, description=None, refname=None, state=Build.STATE_INPROGRESS): |
| 110 | + """ |
| 111 | + Add new build status to commit. |
| 112 | +
|
| 113 | + API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commit-statuses/#api-repositories-workspace-repo-slug-commit-commit-statuses-build-post |
| 114 | + """ |
| 115 | + data = { |
| 116 | + "key": key, |
| 117 | + "state": state, |
| 118 | + "description": description, |
| 119 | + "url": url, |
| 120 | + "refname": refname, |
| 121 | + } |
| 122 | + |
| 123 | + return self.post("statuses/build", data) |
| 124 | + |
| 125 | + def get_build(self, key): |
| 126 | + """ |
| 127 | + Return a specific build for the commit. |
| 128 | +
|
| 129 | + API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commit-statuses/#api-repositories-workspace-repo-slug-commit-commit-statuses-build-key-get |
| 130 | + """ |
| 131 | + return Build(super(Commit, self).get(self.url_joiner("statuses/build", key)), **self._new_session_args) |
| 132 | + |
| 133 | + def comments(self): |
| 134 | + """ |
| 135 | + Return generator object of the comments endpoint. |
| 136 | +
|
| 137 | + API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commit-commit-comments-get |
| 138 | + """ |
| 139 | + for comment in self._get_paged("comments"): |
| 140 | + yield Comment(comment, **self._new_session_args) |
| 141 | + |
| 142 | + def comment(self, raw_message): |
| 143 | + """ |
| 144 | + Add a comment to the pull request in raw format. |
| 145 | +
|
| 146 | + API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commit-commit-comments-post |
| 147 | + """ |
| 148 | + if not raw_message: |
| 149 | + raise ValueError("No message set") |
| 150 | + |
| 151 | + data = { |
| 152 | + "content": { |
| 153 | + "raw": raw_message, |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return self.post("comments", data) |
| 158 | + |
| 159 | + def approve(self): |
| 160 | + """ |
| 161 | + Approve a commit. |
| 162 | +
|
| 163 | + API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commit-commit-approve-post |
| 164 | + """ |
| 165 | + data = {"approved": True} |
| 166 | + return self.post("approve", data) |
| 167 | + |
| 168 | + def unapprove(self): |
| 169 | + """ |
| 170 | + Unapprove a commit. |
| 171 | +
|
| 172 | + API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commit-commit-approve-delete |
| 173 | + """ |
| 174 | + return super(BitbucketCloudBase, self).delete("approve") |
0 commit comments