Browse files

Service repos done

+repos.hooks
  • Loading branch information...
1 parent c525087 commit 221aae624d8a32e801268d2ea5866e885bd3b042 @copitux committed Mar 3, 2012
View
3 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
View
48 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}'
View
7 pygithub3/resources/repos.py
@@ -115,3 +115,10 @@ def ball_to_upload(self):
'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', '')
View
3 pygithub3/services/base.py
@@ -83,6 +83,9 @@ def make_request(self, request, **kwargs):
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)
View
2 pygithub3/services/repos/__init__.py
@@ -8,6 +8,7 @@
from .forks import Forks
from .keys import Keys
from .watchers import Watchers
+from .hooks import Hooks
class Repos(Service):
@@ -20,6 +21,7 @@ def __init__(self, **config):
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'):
View
2 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
View
116 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)
View
45 pygithub3/tests/services/test_repos.py
@@ -6,7 +6,7 @@
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 @@ def test_UNWATCH(self, request_method):
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')))
View
2 setup.py
@@ -15,7 +15,7 @@
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',

0 comments on commit 221aae6

Please sign in to comment.