|
| 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) |
0 commit comments