Skip to content

Commit 794eacc

Browse files
committed
Resolve conflicts
1 parent 3780ba6 commit 794eacc

File tree

6 files changed

+359
-163
lines changed

6 files changed

+359
-163
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from ..base import BitbucketCloudBase
2+
3+
4+
class Build(BitbucketCloudBase):
5+
STATE_FAILED = "FAILED"
6+
STATE_INPROGRESS = "INPROGRESS"
7+
STATE_STOPPED = "STOPPED"
8+
STATE_SUCCESSFUL = "SUCCESSFUL"
9+
10+
def __init__(self, data, *args, **kwargs):
11+
super(Build, self).__init__(None, None, *args, data=data, expected_type="build", **kwargs)
12+
13+
@property
14+
def key(self):
15+
"""Key of the build."""
16+
return self.get_data("key")
17+
18+
@property
19+
def name(self):
20+
"""Name of the build."""
21+
return self.get_data("name")
22+
23+
@property
24+
def description(self):
25+
"""Build description."""
26+
return self.get_data("description")
27+
28+
@property
29+
def failed(self):
30+
"""True if the build failed."""
31+
return self.get_data("state") == self.STATE_FAILED
32+
33+
@property
34+
def inprogress(self):
35+
"""True if the build is in progress."""
36+
return self.get_data("state") == self.STATE_INPROGRESS
37+
38+
@property
39+
def successful(self):
40+
"""True if the build was successful."""
41+
return self.get_data("state") == self.STATE_SUCCESSFUL
42+
43+
@property
44+
def stopped(self):
45+
"""True if the build was stopped."""
46+
return self.get_data("state") == self.STATE_STOPPED
47+
48+
@property
49+
def created_on(self):
50+
"""Time of creation."""
51+
return self.get_time("created_on")
52+
53+
@property
54+
def updated_on(self):
55+
"""Time of last update."""
56+
return self.get_time("updated_on")
57+
58+
@property
59+
def commit(self):
60+
"""Return the hash key of the commit."""
61+
return self.get_data("commit")["hash"]
62+
63+
@property
64+
def website(self):
65+
"""Return the url to the builds webpage.
66+
67+
This url points to the build's frontend website (Pipelines, Jenkins ...)
68+
"""
69+
return self.get_data("url")
70+
71+
@property
72+
def refname(self):
73+
"""Return the refname.
74+
75+
This can be used to indicate on which branch/tag the
76+
build happened.
77+
"""
78+
return self.get_data("refname")
79+
80+
def update(self, **kwargs):
81+
"""Update build status.
82+
83+
See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commit-statuses/#api-repositories-workspace-repo-slug-commit-commit-statuses-build-key-put
84+
"""
85+
return self._update_data(self.put(None, data=kwargs))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from ..base import BitbucketCloudBase
2+
from ..common.users import User
3+
4+
5+
class Comment(BitbucketCloudBase):
6+
def __init__(self, data, *args, **kwargs):
7+
super(Comment, self).__init__(None, None, *args, data=data, expected_type="pullrequest_comment", **kwargs)
8+
9+
@property
10+
def raw(self):
11+
"""The raw comment."""
12+
return self.get_data("content")["raw"]
13+
14+
@property
15+
def html(self):
16+
"""The html comment."""
17+
return self.get_data("content")["html"]
18+
19+
@property
20+
def markup(self):
21+
"""The markup type."""
22+
return self.get_data("content")["markup"]
23+
24+
@property
25+
def user(self):
26+
"""User object with user information of the comment."""
27+
return User(None, self.get_data("user"), **self._new_session_args)
28+
29+
def update(self, **kwargs):
30+
"""Update the comment properties. Fields not present in the request body are ignored.
31+
32+
:param kwargs: dict: The data to update.
33+
34+
:return: The updated comment
35+
"""
36+
return self._update_data(self.put(None, data=kwargs))
37+
38+
def delete(self):
39+
"""Delete the comment.
40+
41+
:return: The response on success
42+
"""
43+
return super(Comment, self).delete(None)

atlassian/bitbucket/cloud/common/users.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,46 @@ def uuid(self):
2929
def avatar(self):
3030
""" URL to user avatar on Bitbucket Cloud """
3131
return self.get_data("links")["avatar"]["href"]
32+
33+
34+
class Participant(BitbucketCloudBase):
35+
ROLE_REVIEWER = "REVIEWER"
36+
ROLE_PARTICIPANT = "PARTICIPANT"
37+
CHANGES_REQUESTED = "changes_requested"
38+
39+
def __init__(self, data, *args, **kwargs):
40+
super(Participant, self).__init__(None, None, *args, data=data, expected_type="participant", **kwargs)
41+
42+
@property
43+
def user(self):
44+
"""User object with user information of the participant."""
45+
return User(None, self.get_data("user"), **self._new_session_args)
46+
47+
@property
48+
def is_participant(self):
49+
"""True if the user is a pull request participant."""
50+
return self.get_data("role") == self.ROLE_PARTICIPANT
51+
52+
@property
53+
def is_reviewer(self):
54+
"""True if the user is a pull request reviewer."""
55+
return self.get_data("role") == self.ROLE_REVIEWER
56+
57+
@property
58+
def is_default_reviewer(self):
59+
"""True if the user is a default reviewer."""
60+
61+
@property
62+
def has_changes_requested(self):
63+
"""True if user requested changes."""
64+
return str(self.get_data("state")) == self.CHANGES_REQUESTED
65+
66+
@property
67+
def has_approved(self):
68+
"""True if user approved the pull request."""
69+
return self.get_data("approved")
70+
71+
@property
72+
def participated_on(self):
73+
"""Time of last participation."""
74+
return self.get_time("participated_on")

atlassian/bitbucket/cloud/repositories/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ..base import BitbucketCloudBase
55
from .issues import Issues
66
from .branchRestrictions import BranchRestrictions
7+
from .commits import Commits
78
from .defaultReviewers import DefaultReviewers
89
from .pipelines import Pipelines
910
from .pullRequests import PullRequests
@@ -227,6 +228,11 @@ def __init__(self, data, *args, **kwargs):
227228
"{}/branch-restrictions".format(self.url), **self._new_session_args
228229
)
229230
self.__branches = Branches("{}/refs/branches".format(self.url), **self._new_session_args)
231+
self.__commits = Commits(
232+
"{}/commits".format(self.url),
233+
data={"links": {"commit": {"href": "{}/commit".format(self.url)}}},
234+
**self._new_session_args
235+
)
230236
self.__default_reviewers = DefaultReviewers("{}/default-reviewers".format(self.url), **self._new_session_args)
231237
self.__issues = Issues("{}/issues".format(self.url), **self._new_session_args)
232238
self.__pipelines = Pipelines("{}/pipelines".format(self.url), **self._new_session_args)
@@ -337,6 +343,11 @@ def branches(self):
337343
"""The repository branches."""
338344
return self.__branches
339345

346+
@property
347+
def commits(self):
348+
"""The repository commits."""
349+
return self.__commits
350+
340351
@property
341352
def default_reviewers(self):
342353
"""The repository default reviewers"""
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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

Comments
 (0)