diff options
-rw-r--r-- | docs/repos.rst | 3 | ||||
-rw-r--r-- | pygithub3/requests/repos/hooks.py | 48 | ||||
-rw-r--r-- | pygithub3/resources/repos.py | 7 | ||||
-rw-r--r-- | pygithub3/services/base.py | 3 | ||||
-rw-r--r-- | pygithub3/services/repos/__init__.py | 2 | ||||
-rw-r--r-- | pygithub3/services/repos/commits.py | 2 | ||||
-rw-r--r-- | pygithub3/services/repos/hooks.py | 116 | ||||
-rw-r--r-- | pygithub3/tests/services/test_repos.py | 45 | ||||
-rw-r--r-- | setup.py | 2 |
9 files changed, 225 insertions, 3 deletions
diff --git a/docs/repos.rst b/docs/repos.rst index 2b58f32..79e016a 100644 --- a/docs/repos.rst +++ b/docs/repos.rst @@ -133,6 +133,9 @@ Watchers Hooks --------- +.. autoclass:: pygithub3.services.repos.Hooks + :members: + .. _github repos doc: http://developer.github.com/v3/repos .. _github collaborators doc: http://developer.github.com/v3/repos/collaborators .. _github commits doc: http://developer.github.com/v3/repos/commits diff --git a/pygithub3/requests/repos/hooks.py b/pygithub3/requests/repos/hooks.py new file mode 100644 index 0000000..28fbc18 --- /dev/null +++ b/pygithub3/requests/repos/hooks.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from . import Request +from pygithub3.resources.repos import Hook + + +class List(Request): + + uri = 'repos/{user}/{repo}/hooks' + resource = Hook + + +class Get(Request): + + uri = 'repos/{user}/{repo}/hooks/{id}' + resource = Hook + + +class Create(Request): + + uri = 'repos/{user}/{repo}/hooks' + resource = Hook + body_schema = { + 'schema': ('name', 'config', 'events', 'active'), + 'required': ('name', 'config'), + } + + +class Update(Request): + + uri = 'repos/{user}/{repo}/hooks/{id}' + resource = Hook + body_schema = { + 'schema': ('name', 'config', 'events', 'add_events', 'remove_events', + 'active'), + 'required': (), + } + + +class Test(Request): + + uri = 'repos/{user}/{repo}/hooks/{id}/test' + + +class Delete(Request): + + uri = 'repos/{user}/{repo}/hooks/{id}' diff --git a/pygithub3/resources/repos.py b/pygithub3/resources/repos.py index 99a6d4b..9d8ebf6 100644 --- a/pygithub3/resources/repos.py +++ b/pygithub3/resources/repos.py @@ -115,3 +115,10 @@ class Download(Resource): 'Filename': self.name, 'AWSAccessKeyId': self.accesskeyid, 'Policy': self.policy, 'Signature': self.signature, 'Content-Type': self.mime_type}) + +class Hook(Resource): + + _dates = ('created_at', 'pushed_at') + + def __str__(self): + return '<Hook (%s)>' % getattr(self, 'name', '') diff --git a/pygithub3/services/base.py b/pygithub3/services/base.py index 9f7279a..84379b0 100644 --- a/pygithub3/services/base.py +++ b/pygithub3/services/base.py @@ -83,6 +83,9 @@ class Service(object): kwargs['repo'] = kwargs['repo'] or self.get_repo() return self.request_builder(request, **kwargs) + def _request(self, verb, request, **kwargs): + self._client.request(verb, request, **kwargs) + def _bool(self, request, **kwargs): try: self._client.head(request, **kwargs) diff --git a/pygithub3/services/repos/__init__.py b/pygithub3/services/repos/__init__.py index 6695265..df029e8 100644 --- a/pygithub3/services/repos/__init__.py +++ b/pygithub3/services/repos/__init__.py @@ -8,6 +8,7 @@ from .downloads import Downloads from .forks import Forks from .keys import Keys from .watchers import Watchers +from .hooks import Hooks class Repos(Service): @@ -20,6 +21,7 @@ class Repos(Service): self.forks = Forks(**config) self.keys = Keys(**config) self.watchers = Watchers(**config) + self.hooks = Hooks(**config) super(Repos, self).__init__(**config) def list(self, user=None, type='all'): diff --git a/pygithub3/services/repos/commits.py b/pygithub3/services/repos/commits.py index 8eec3e7..fd29fc0 100644 --- a/pygithub3/services/repos/commits.py +++ b/pygithub3/services/repos/commits.py @@ -9,7 +9,7 @@ class Commits(Service, MimeTypeMixin): <http://developer.github.com/v3/repos/commits>`_ .. note:: - This service support :ref:`mimetypes` configuration + This service support :ref:`mimetypes-section` configuration """ #TODO: Pagination structure differs from usual diff --git a/pygithub3/services/repos/hooks.py b/pygithub3/services/repos/hooks.py new file mode 100644 index 0000000..59cf6e1 --- /dev/null +++ b/pygithub3/services/repos/hooks.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from . import Service + + +class Hooks(Service): + """ Consume `Hooks API + <http://developer.github.com/v3/repos/hooks>`_ + + .. warning:: + You must be authenticated and have repository's admin-permission + """ + + def list(self, user=None, repo=None): + """ Get repository's hooks + + :param str user: Username + :param str repo: Repository + :returns: A :doc:`result` + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('repos.hooks.list', user=user, repo=repo) + return self._get_result(request) + + def get(self, hook_id, user=None, repo=None): + """ Get a single hook + + :param int hook_id: Hook id + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('repos.hooks.get', + id=hook_id, user=user, repo=repo) + return self._get(request) + + def create(self, data, user=None, repo=None): + """ Create a hook + + :param dict data: Input. See `github hooks doc`_ + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + + :: + + data = { + "name": "acunote", + "active": True, + "config": { + 'token': 'AAA...', + }, + "events": ['push', 'issues'], + } + hooks_service.create(data, user='octocat', repo='oct_repo') + """ + request = self.make_request('repos.hooks.create', + user=user, repo=repo, body=data) + return self._post(request) + + def update(self, hook_id, data, user=None, repo=None): + """ Update a single hook + + :param int hook_id: Hook id + :param dict data: Input. See `github hooks doc`_ + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + + :: + + hooks_service.update(42, dict(active=False), user='octocat', + repo='oct_repo') + """ + request = self.make_request('repos.hooks.update', + id=hook_id, user=user, repo=repo, body=data) + return self._patch(request) + + def test(self, hook_id, user=None, repo=None): + """ Test a hook + + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + + This will trigger the hook with the latest push to the current + repository. + """ + request = self.make_request('repos.hooks.test', + id=hook_id, user=user, repo=repo) + self._request('post', request) + + def delete(self, hook_id, user=None, repo=None): + """ Delete a single hook + + :param int hook_id: Hook id + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('repos.hooks.delete', + id=hook_id, user=user, repo=repo) + self._delete(request) diff --git a/pygithub3/tests/services/test_repos.py b/pygithub3/tests/services/test_repos.py index 1993340..bc5ed52 100644 --- a/pygithub3/tests/services/test_repos.py +++ b/pygithub3/tests/services/test_repos.py @@ -6,7 +6,7 @@ from mock import patch, Mock from pygithub3.tests.utils.core import TestCase from pygithub3.services.repos import (Repos, Collaborators, Commits, Downloads, - Forks, Keys, Watchers) + Forks, Keys, Watchers, Hooks) from pygithub3.resources.base import json from pygithub3.tests.utils.base import (mock_response, mock_response_result, mock_json) @@ -367,3 +367,46 @@ class TestWatchersService(TestCase): self.ws.unwatch() self.assertEqual(request_method.call_args[0], ('delete', _('user/watched/oct/re_oct'))) + + +@patch.object(requests.sessions.Session, 'request') +class TestHooksService(TestCase): + + def setUp(self): + self.hs = Hooks(user='oct', repo='re_oct') + + def test_LIST(self, request_method): + request_method.return_value = mock_response_result() + self.hs.list().all() + self.assertEqual(request_method.call_args[0], + ('get', _('repos/oct/re_oct/hooks'))) + + def test_GET(self, request_method): + request_method.return_value = mock_response() + self.hs.get(1) + self.assertEqual(request_method.call_args[0], + ('get', _('repos/oct/re_oct/hooks/1'))) + + def test_CREATE(self, request_method): + request_method.return_value = mock_response('post') + self.hs.create(dict(name='acunote', config={'usr': 'http...'})) + self.assertEqual(request_method.call_args[0], + ('post', _('repos/oct/re_oct/hooks'))) + + def test_UPDATE(self, request_method): + request_method.return_value = mock_response('patch') + self.hs.update(1, dict(events=['push'])) + self.assertEqual(request_method.call_args[0], + ('patch', _('repos/oct/re_oct/hooks/1'))) + + def test_TEST(self, request_method): + request_method.return_value = mock_response('post') + self.hs.test(1) + self.assertEqual(request_method.call_args[0], + ('post', _('repos/oct/re_oct/hooks/1/test'))) + + def test_DELETE(self, request_method): + request_method.return_value = mock_response('delete') + self.hs.delete(1) + self.assertEqual(request_method.call_args[0], + ('delete', _('repos/oct/re_oct/hooks/1'))) @@ -15,7 +15,7 @@ setup( license='ISC', packages=find_packages(exclude=['*tests*']), install_requires=map(str.strip, open('requirements.txt')), - include_package_data = True, + include_package_data=True, classifiers=( 'Programming Language :: Python', 'Programming Language :: Python :: 2.6', |