Skip to content

Commit bd7ac3a

Browse files
author
Costin Bleotu
committed
Webhook entity representation.
1 parent 5787c9a commit bd7ac3a

File tree

5 files changed

+218
-0
lines changed

5 files changed

+218
-0
lines changed

docs/entities.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ This pages documents classes provided by ``gogs_client`` module that represent e
1717

1818
.. autoclass:: gogs_client.entities::GogsRepo.Permissions()
1919
:members:
20+
21+
.. autoclass:: gogs_client.entities::GogsRepo.Hook()
22+
:members:

docs/updates.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ Updates
88

99
.. autoclass:: gogs_client.updates::GogsUserUpdate.Builder()
1010
:members:
11+
12+
.. autoclass:: GogsHookUpdate()
13+
:members:
14+
15+
.. autoclass:: gogs_client.updates::GogsHookUpdate.Builder()
16+
:members:

gogs_client/entities.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,70 @@ def pull(self):
240240
:rtype: bool
241241
"""
242242
return self._pull
243+
244+
class Hook(object):
245+
def __init__(self, hook_id, hook_type, events, active, config):
246+
self._id = hook_id
247+
self._type = hook_type
248+
self._events = events
249+
self._active = active
250+
self._config = config
251+
252+
@staticmethod
253+
def from_json(parsed_json):
254+
hook_id = json_get(parsed_json, "id")
255+
hook_type = json_get(parsed_json, "type")
256+
events = json_get(parsed_json, "events")
257+
active = json_get(parsed_json, "active")
258+
config = json_get(parsed_json, "config")
259+
260+
return GogsRepo.Hook(hook_id=hook_id, hook_type=hook_type, events=events, active=active,
261+
config=config)
262+
263+
@property # named hook_id to avoid conflict with built-in id
264+
def hook_id(self):
265+
"""
266+
The hook's id number
267+
268+
:rtype: int
269+
"""
270+
return self._id
271+
272+
@property # named hook_type to avoid conflict with built-in type
273+
def hook_type(self):
274+
"""
275+
The hook's type (gogs, slack, etc.)
276+
277+
:rtype: str
278+
"""
279+
return self._type
280+
281+
@property
282+
def events(self):
283+
"""
284+
The events that fires the hook
285+
286+
:rtype: list of strs
287+
"""
288+
return self._events
289+
290+
@property
291+
def active(self):
292+
"""
293+
State of the hook
294+
295+
:rtype: bool
296+
"""
297+
return self._active
298+
299+
@property
300+
def config(self):
301+
"""
302+
Config of the hook. Contains max. 3 keys:
303+
- content_type
304+
- url
305+
- secret
306+
307+
:rtype: dict
308+
"""
309+
return self._config

gogs_client/interface.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,69 @@ def delete_user(self, auth, username):
290290
path = "/admin/users/{}".format(username)
291291
self._check_ok(self._delete(path, auth=auth))
292292

293+
def get_repo_hooks(self, auth, username, repo_name):
294+
"""
295+
Returns all hooks of repository with name ``repo_name`` owned by
296+
the user with username ``username``.
297+
298+
:param auth.Authentication auth: authentication object
299+
:param str username: username of owner of repository
300+
:param str repo_name: name of repository
301+
:return: a list of hooks for the specified repository
302+
:rtype: List[GogsRepo.Hooks]
303+
:raises NetworkFailure: if there is an error communicating with the server
304+
:raises ApiFailure: if the request cannot be serviced
305+
"""
306+
path = "/repos/{u}/{r}/hooks".format(u=username, r=repo_name)
307+
response = self._check_ok(self._get(path, auth=auth))
308+
hooks = [GogsRepo.Hook.from_json(hook) for hook in response.json()]
309+
return hooks
310+
311+
def create_hook(self, auth, repo_name, hook_type, config, events=["push"], organization=None, active=False):
312+
"""
313+
Creates a new hook, and returns the created hook.
314+
315+
:param auth.Authentication auth: authentication object, must be admin-level
316+
:param str repo_name: the name of the repo for which we create the hook
317+
:param str hook_type: The type of webhook, either "gogs" or "slack"
318+
:param dict config: Key/value pairs to provide settings for this hook ("url", "content_type", "secret")
319+
:param list events: Determines what events the hook is triggered for. Default: ["push"]
320+
:param str organization: (Optional) Organization of the repo
321+
:param bool active: Determines whether the hook is actually triggered on pushes. Default is false
322+
:return: a representation of the created hook
323+
:rtype: GogsRepo.Hook
324+
:raises NetworkFailure: if there is an error communicating with the server
325+
:raises ApiFailure: if the request cannot be serviced
326+
"""
327+
328+
data = {
329+
"type": hook_type,
330+
"config": config,
331+
"events": events,
332+
"active": active
333+
}
334+
335+
url = "/repos/{o}/{r}/hooks".format(o=organization, r=repo_name) if organization else "/repos/{r}/hooks".format(r=repo_name)
336+
response = self._post(url, auth=auth, data=data)
337+
print response.text
338+
self._check_ok(response)
339+
return GogsRepo.Hook.from_json(response.json())
340+
341+
def delete_hook(self, auth, username, repo_name, hook_id):
342+
"""
343+
Deletes the hook with id ``hook_id`` for repo with name ``repo_name``
344+
owned by the user with username ``username``.
345+
346+
:param auth.Authentication auth: authentication object
347+
:param str username: username of owner of repository
348+
:param str repo_name: name of repository of hook to delete
349+
:param int hook_id: id of hook to delete
350+
:raises NetworkFailure: if there is an error communicating with the server
351+
:raises ApiFailure: if the request cannot be serviced
352+
"""
353+
path = "/repos/{u}/{r}/hooks/{i}".format(u=username, r=repo_name, i=hook_id)
354+
self._check_ok(self._delete(path, auth=auth))
355+
293356
# Helper methods
294357

295358
def _delete(self, path, auth=None, **kwargs):

gogs_client/updates.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,82 @@ def build(self):
182182
admin=self._admin,
183183
allow_git_hook=self._allow_git_hook,
184184
allow_import_local=self._allow_import_local)
185+
186+
187+
class GogsHookUpdate(object):
188+
"""
189+
An immutable represention of a collection of Gogs hook attributes to update.
190+
191+
Instances should be created using the :class:`~GogsHookUpdate.Builder` class.
192+
"""
193+
def __init__(self, hook_type, events, config, active):
194+
"""
195+
:param hook_type:
196+
:param events:
197+
:param config:
198+
:param active:
199+
"""
200+
201+
self._type = hook_type
202+
self._events = events
203+
self._config = config
204+
self._active = active
205+
206+
def as_dict(self):
207+
fields = {
208+
"type": self._type,
209+
"events": self._events,
210+
"config": self._config,
211+
"active": self._active,
212+
}
213+
return {k: v for (k, v) in fields.items() if v is not None}
214+
215+
class Builder(object):
216+
def __init__(self):
217+
"""
218+
:param str login_name: login name for authentication source
219+
:param str email: email address of user to update
220+
"""
221+
self._type = None
222+
self._events = None
223+
self._config = None
224+
self._active = None
225+
226+
227+
def set_events(self, events):
228+
"""
229+
:param list events:
230+
:return: the updated builder
231+
:rtype: GogsHookUpdate.Builder
232+
"""
233+
self._events = events
234+
return self
235+
236+
def set_config(self, config):
237+
"""
238+
:param dict config:
239+
:return: the updated builder
240+
:rtype: GogsHookUpdate.Builder
241+
"""
242+
self._config = config
243+
return self
244+
245+
def set_active(self, active):
246+
"""
247+
:param bool active:
248+
:return: the updated builder
249+
:rtype: GogsHookUpdate.Builder
250+
"""
251+
self._active = active
252+
return self
253+
254+
def build(self):
255+
"""
256+
:return: A :class:`~GogsHookUpdate` instance reflecting the changes added to the builder.
257+
:rtype: GogsHookUpdate
258+
"""
259+
return GogsHookUpdate(
260+
hook_type=self._type,
261+
events=self._events,
262+
config=self._config,
263+
active=self._active)

0 commit comments

Comments
 (0)