Skip to content

Commit 273d0f2

Browse files
[BitBucket] Add webhooks to Cloud (#1346)
1 parent 893ee20 commit 273d0f2

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

atlassian/bitbucket/cloud/repositories/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .issues import Issues
66
from .branchRestrictions import BranchRestrictions
77
from .commits import Commits
8+
from .hooks import Hooks
89
from .defaultReviewers import DefaultReviewers
910
from .deploymentEnvironments import DeploymentEnvironments
1011
from .groupPermissions import GroupPermissions
@@ -259,6 +260,11 @@ def __init__(self, data, *args, **kwargs):
259260
data={"links": {"commit": {"href": "{}/commit".format(self.url)}}},
260261
**self._new_session_args
261262
) # fmt: skip
263+
self.__hooks = Hooks(
264+
"{}/hooks".format(self.url),
265+
data={"links": {"hooks": {"href": "{}/hooks".format(self.url)}}},
266+
**self._new_session_args,
267+
)
262268
self.__default_reviewers = DefaultReviewers("{}/default-reviewers".format(self.url), **self._new_session_args)
263269
self.__deployment_environments = DeploymentEnvironments(
264270
"{}/environments".format(self.url), **self._new_session_args
@@ -385,6 +391,11 @@ def commits(self):
385391
"""The repository commits."""
386392
return self.__commits
387393

394+
@property
395+
def hooks(self):
396+
"""The repository hooks."""
397+
return self.__hooks
398+
388399
@property
389400
def default_reviewers(self):
390401
"""The repository default reviewers"""
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# coding=utf-8
2+
3+
from ..base import BitbucketCloudBase
4+
5+
6+
class Hooks(BitbucketCloudBase):
7+
"""Bitbucket Cloud webhooks."""
8+
9+
def __init__(self, url, *args, **kwargs):
10+
super(Hooks, self).__init__(url, *args, **kwargs)
11+
12+
def __get_object(self, data):
13+
return Hook(data, **self._new_session_args)
14+
15+
def create(
16+
self,
17+
url,
18+
description,
19+
events,
20+
active=True,
21+
):
22+
"""
23+
Creates a new webhook for the current repository
24+
25+
param: url: string: Url that will receive event requests
26+
param: description: string: Details about the webhook
27+
param: events: [string] List of event types that requests will generate for
28+
param: active: boolean: Enables/Disables the webhook
29+
30+
:return: Hook Object
31+
"""
32+
33+
data = {"url": url, "description": description, "active": active, "events": events}
34+
35+
return self.__get_object(self.post(None, data))
36+
37+
def each(self):
38+
"""
39+
Return the list of webhooks in this repository.
40+
41+
:return: A generator for the Webhook objects
42+
"""
43+
for hook in self._get_paged(None):
44+
yield self.__get_object(hook)
45+
46+
def get(self, id):
47+
"""
48+
Return the hook with the requested hook uuid in this repository.
49+
50+
:param id: string: The id of the webhook
51+
52+
:return: The requested hook object
53+
"""
54+
return self.__get_object(
55+
super(Hooks, self).get(
56+
self.url_joiner(self.get_link("hooks"), id),
57+
absolute=True,
58+
)
59+
)
60+
61+
62+
class Hook(BitbucketCloudBase):
63+
"""
64+
Bitbucket Cloud hook endpoint.
65+
"""
66+
67+
def __init__(self, data, *args, **kwargs):
68+
super(Hook, self).__init__(None, *args, data=data, expected_type="webhook_subscription", **kwargs)
69+
70+
def uuid(self):
71+
"""hook uuid."""
72+
return self.get_data("uuid")
73+
74+
def webhook_url(self):
75+
"""webhook url."""
76+
return self.get_data("url")
77+
78+
def description(self):
79+
"""webhook description."""
80+
return self.get_data("description")
81+
82+
def active(self):
83+
"""is webhook active?"""
84+
return self.get_data("active")
85+
86+
def events(self):
87+
"""events that the webhook is triggered by"""
88+
return self.get_data("events")
89+
90+
def update(self, **kwargs):
91+
"""
92+
Update a webhook
93+
94+
Valid keywords:
95+
param: url: string: Url that will receive event requests
96+
param: description: string: Details about the webhook
97+
param: events: [string] List of event types that requests will generate for
98+
param: active: boolean: Enables/Disables the webhook
99+
"""
100+
101+
payload = {
102+
"url": self.webhook_url(),
103+
"description": self.description(),
104+
"events": self.events(),
105+
"active": self.active(),
106+
}
107+
108+
for key in payload.keys() and kwargs.keys():
109+
payload[key] = kwargs[key]
110+
111+
return self._update_data(self.put(None, data=payload))
112+
113+
def delete(self):
114+
"""
115+
Delete the webhook.
116+
"""
117+
return super(Hook, self).delete(None)

docs/bitbucket.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,21 @@ Bitbucket Cloud
413413
# Delete repository_variable
414414
repository_variable.delete()
415415
416+
# Get a list of hooks from a repository
417+
repository.hooks.each():
418+
419+
# Create a hook for a repository
420+
hook = repo.hooks.create(url="endpoint-url", description="description", active=True, events=["a-repository-event"])
421+
422+
# Get a single hook for a repository
423+
hook = repo.hooks.get("a-webhook-id")
424+
425+
# Update a specific hook for a repository
426+
hook.update(url="endpoint-url", description="description", active=True, events=["a-repository-event"])
427+
428+
# Delete a speicifc hook for a repository
429+
hook.delete()
430+
416431
# Get a list of workspace members
417432
workplace.members.each()
418433

0 commit comments

Comments
 (0)