Skip to content

Commit f1bdd3a

Browse files
author
Costin Bleotu
committed
Create, update and delete hooks.
1 parent bd7ac3a commit f1bdd3a

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

gogs_client/_implementation/http_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ def options(self, relative_path, params=None, **kwargs):
3636
return self.session.options(self.absolute_url(relative_path), params=params, **kwargs)
3737

3838
def patch(self, relative_path, data=None, **kwargs):
39-
return self.session.patch(self.absolute_url(relative_path), data=data, **kwargs)
39+
return self.session.patch(self.absolute_url(relative_path), json=data, **kwargs)
4040

4141
def post(self, relative_path, data=None, **kwargs):
42-
return self.session.post(self.absolute_url(relative_path), data=data, **kwargs)
42+
return self.session.post(self.absolute_url(relative_path), json=data, **kwargs)
4343

4444
def put(self, relative_path, params=None, data=None, **kwargs):
45-
return self.session.put(self.absolute_url(relative_path), params=params, data=data, **kwargs)
45+
return self.session.put(self.absolute_url(relative_path), params=params, json=data, **kwargs)
4646

4747

4848
def append_url(base_url, path):

gogs_client/entities.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,16 @@ def from_json(parsed_json):
260260
return GogsRepo.Hook(hook_id=hook_id, hook_type=hook_type, events=events, active=active,
261261
config=config)
262262

263+
def as_dict(self):
264+
fields = {
265+
"id": self._id,
266+
"type": self._type,
267+
"events": self._events,
268+
"config": self._config,
269+
"active": self._active,
270+
}
271+
return {k: v for (k, v) in fields.items() if v is not None}
272+
263273
@property # named hook_id to avoid conflict with built-in id
264274
def hook_id(self):
265275
"""

gogs_client/interface.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,25 @@ def create_hook(self, auth, repo_name, hook_type, config, events=["push"], organ
334334

335335
url = "/repos/{o}/{r}/hooks".format(o=organization, r=repo_name) if organization else "/repos/{r}/hooks".format(r=repo_name)
336336
response = self._post(url, auth=auth, data=data)
337-
print response.text
338337
self._check_ok(response)
339338
return GogsRepo.Hook.from_json(response.json())
340339

340+
def update_hook(self, auth, repo_name, hook_id, update, organization=None):
341+
"""
342+
Updates hook with id ``hook_id`` according to ``update``.
343+
344+
:param auth.Authentication auth: authentication object, must be admin-level
345+
:param str repo_name: repo of the hook to update
346+
:param GogsHookUpdate update: a ``GogsHookUpdate`` object describing the requested update
347+
:return: the updated hook
348+
:rtype: GogsRepo.Hook
349+
:raises NetworkFailure: if there is an error communicating with the server
350+
:raises ApiFailure: if the request cannot be serviced
351+
"""
352+
path = "/repos/{o}/{r}/hooks/{i}".format(o=organization, r=repo_name, i=hook_id) if organization else "/repos/{r}/hooks/{i}".format(r=repo_name, i=hook_id)
353+
response = self._check_ok(self._patch(path, auth=auth, data=update.as_dict()))
354+
return GogsRepo.Hook.from_json(response.json())
355+
341356
def delete_hook(self, auth, username, repo_name, hook_id):
342357
"""
343358
Deletes the hook with id ``hook_id`` for repo with name ``repo_name``

gogs_client/updates.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,19 @@ class GogsHookUpdate(object):
190190
191191
Instances should be created using the :class:`~GogsHookUpdate.Builder` class.
192192
"""
193-
def __init__(self, hook_type, events, config, active):
193+
def __init__(self, events, config, active):
194194
"""
195-
:param hook_type:
196195
:param events:
197196
:param config:
198197
:param active:
199198
"""
200199

201-
self._type = hook_type
202200
self._events = events
203201
self._config = config
204202
self._active = active
205203

206204
def as_dict(self):
207205
fields = {
208-
"type": self._type,
209206
"events": self._events,
210207
"config": self._config,
211208
"active": self._active,
@@ -218,12 +215,10 @@ def __init__(self):
218215
:param str login_name: login name for authentication source
219216
:param str email: email address of user to update
220217
"""
221-
self._type = None
222218
self._events = None
223219
self._config = None
224220
self._active = None
225221

226-
227222
def set_events(self, events):
228223
"""
229224
:param list events:
@@ -257,7 +252,6 @@ def build(self):
257252
:rtype: GogsHookUpdate
258253
"""
259254
return GogsHookUpdate(
260-
hook_type=self._type,
261255
events=self._events,
262256
config=self._config,
263257
active=self._active)

0 commit comments

Comments
 (0)