From 2ca15bb8e847735f566ee0cb896f071b3b5ca056 Mon Sep 17 00:00:00 2001 From: Nat Williams Date: Tue, 17 Apr 2012 14:39:12 -0500 Subject: let request objects specify custom body validations --- pygithub3/requests/base.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'pygithub3/requests') diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py index 03b0f8a..582a73f 100644 --- a/pygithub3/requests/base.py +++ b/pygithub3/requests/base.py @@ -17,10 +17,11 @@ ABS_IMPORT_PREFIX = 'pygithub3.requests' class Body(object): - def __init__(self, content, schema, required): + def __init__(self, content, valid_body, validate_body=None): self.content = content - self.schema = schema - self.required = required + self.schema = valid_body['schema'] + self.required = valid_body['required'] + self.validate_body = validate_body or (lambda x: None) def dumps(self): if not self.schema: @@ -33,6 +34,7 @@ class Body(object): % self.__class__.__name__) parsed = dict([(key, self.content[key]) for key in self.schema if key in self.content]) + self.validate_body(parsed) for attr_required in self.required: if attr_required not in parsed: raise ValidationError("'%s' attribute is required" % @@ -58,7 +60,8 @@ class Request(object): def clean(self): self.uri = self.clean_uri() or self.uri - self.body = Body(self.clean_body(), **self.clean_valid_body()) + self.body = Body(self.clean_body(), self.clean_valid_body(), + self.validate_body) def clean_body(self): return self.body @@ -94,6 +97,9 @@ class Request(object): def get_body(self): return self.body.dumps() + def validate_body(self, *args): + pass + class Factory(object): """ Request builder """ -- cgit v1.2.3-59-g8ed1b From 17649c939901573af53beeeeb9d7be08102d1503 Mon Sep 17 00:00:00 2001 From: Nat Williams Date: Tue, 17 Apr 2012 14:42:57 -0500 Subject: more specific exception for missing Request classes --- pygithub3/exceptions.py | 2 +- pygithub3/requests/base.py | 12 ++++++------ pygithub3/tests/requests/test_core.py | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'pygithub3/requests') diff --git a/pygithub3/exceptions.py b/pygithub3/exceptions.py index a467256..20cf058 100644 --- a/pygithub3/exceptions.py +++ b/pygithub3/exceptions.py @@ -8,7 +8,7 @@ class InvalidBodySchema(Exception): pass -class DoesNotExists(Exception): +class RequestDoesNotExist(Exception): """ Raised when `Request` factory can't find the subclass """ pass diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py index 582a73f..d3e18a3 100644 --- a/pygithub3/requests/base.py +++ b/pygithub3/requests/base.py @@ -7,8 +7,8 @@ try: except ImportError: import json -from pygithub3.exceptions import (DoesNotExists, UriInvalid, ValidationError, - InvalidBodySchema) +from pygithub3.exceptions import (RequestDoesNotExist, UriInvalid, + ValidationError, InvalidBodySchema) from pygithub3.resources.base import Raw from pygithub3.core.utils import import_module @@ -126,11 +126,11 @@ class Factory(object): % (ABS_IMPORT_PREFIX, module_chunk)) request = getattr(module, request_chunk) except ImportError: - raise DoesNotExists("'%s' module does not exists" - % module_chunk) + raise RequestDoesNotExist("'%s' module does not exist" + % module_chunk) except AttributeError: - raise DoesNotExists( - "'%s' request doesn't exists into '%s' module" + raise RequestDoesNotExist( + "'%s' request does not exist in '%s' module" % (request_chunk, module_chunk)) return func(self, request, **kwargs) return wrapper diff --git a/pygithub3/tests/requests/test_core.py b/pygithub3/tests/requests/test_core.py index 9fcec10..4632056 100644 --- a/pygithub3/tests/requests/test_core.py +++ b/pygithub3/tests/requests/test_core.py @@ -6,8 +6,8 @@ from nose.tools import raises from pygithub3.tests.utils.core import TestCase from pygithub3.requests.base import Factory, Body, json, Request -from pygithub3.exceptions import (UriInvalid, DoesNotExists, ValidationError, - InvalidBodySchema) +from pygithub3.exceptions import (UriInvalid, RequestDoesNotExist, + ValidationError, InvalidBodySchema) from pygithub3.tests.utils.base import (mock_json, DummyRequest, DummyRequestValidation) from pygithub3.tests.utils.requests import ( @@ -29,8 +29,8 @@ class TestFactory(TestCase): self.assertRaises(UriInvalid, self.f, '.invalid') def test_BUILDER_with_fake_action(self): - self.assertRaises(DoesNotExists, self.f, 'users.fake') - self.assertRaises(DoesNotExists, self.f, 'fake.users') + self.assertRaises(RequestDoesNotExist, self.f, 'users.fake') + self.assertRaises(RequestDoesNotExist, self.f, 'fake.users') def test_BUILDER_builds_users(self): """ Users.get as real test because it wouldn't be useful mock -- cgit v1.2.3-59-g8ed1b From 4539f80aa44e500422c5071d8e3d74de321b6225 Mon Sep 17 00:00:00 2001 From: Nat Williams Date: Tue, 17 Apr 2012 17:35:31 -0500 Subject: add pull request API there are a few little issues remaining. Mostly regarding handling meaningful non-20x response codes --- pygithub3/core/client.py | 2 +- pygithub3/exceptions.py | 2 +- pygithub3/github.py | 9 ++ pygithub3/requests/pull_requests/__init__.py | 62 ++++++++ pygithub3/requests/pull_requests/comments.py | 43 ++++++ pygithub3/resources/pull_requests.py | 20 +++ pygithub3/services/pull_requests/__init__.py | 129 ++++++++++++++++ pygithub3/services/pull_requests/comments.py | 73 +++++++++ pygithub3/tests/services/test_pull_requests.py | 201 +++++++++++++++++++++++++ pygithub3/tests/utils/base.py | 2 +- 10 files changed, 540 insertions(+), 3 deletions(-) create mode 100644 pygithub3/requests/pull_requests/__init__.py create mode 100644 pygithub3/requests/pull_requests/comments.py create mode 100644 pygithub3/resources/pull_requests.py create mode 100644 pygithub3/services/pull_requests/__init__.py create mode 100644 pygithub3/services/pull_requests/comments.py create mode 100644 pygithub3/tests/services/test_pull_requests.py (limited to 'pygithub3/requests') diff --git a/pygithub3/core/client.py b/pygithub3/core/client.py index ee7c97f..eadb18e 100644 --- a/pygithub3/core/client.py +++ b/pygithub3/core/client.py @@ -81,7 +81,7 @@ class Client(object): def get(self, request, **kwargs): response = self.request('get', request, **kwargs) - assert response.status_code == 200 + # there are valid GET responses that != 200 return response def post(self, request, **kwargs): diff --git a/pygithub3/exceptions.py b/pygithub3/exceptions.py index 20cf058..9ee2597 100644 --- a/pygithub3/exceptions.py +++ b/pygithub3/exceptions.py @@ -37,6 +37,6 @@ class UnprocessableEntity(Exception): class NotFound(Exception): """ Raised when server response is 404 - Catched with a pygithub3-exception to `services.base.Service._bool` method + Caught with a pygithub3-exception to `services.base.Service._bool` method """ pass diff --git a/pygithub3/github.py b/pygithub3/github.py index 0b302a1..0f15adf 100644 --- a/pygithub3/github.py +++ b/pygithub3/github.py @@ -17,9 +17,11 @@ class Github(object): from pygithub3.services.users import User from pygithub3.services.repos import Repo from pygithub3.services.gists import Gist + from pygithub3.services.pull_requests import PullRequests self._users = User(**config) self._repos = Repo(**config) self._gists = Gist(**config) + self._pull_requests = PullRequests(**config) @property def remaining_requests(self): @@ -47,3 +49,10 @@ class Github(object): :ref:`Gists service ` """ return self._gists + + @property + def pull_requests(self): + """ + :ref:`Pull Requests service ' % getattr(self, 'title', '') + + +class File(Resource): + def __str__(self): + return '' % getattr(self, 'filename', '') + + +class Comment(Resource): + _dates = ('created_at', 'updated_at') + + def __str__(self): + return '' % getattr(self, 'id', '') diff --git a/pygithub3/services/pull_requests/__init__.py b/pygithub3/services/pull_requests/__init__.py new file mode 100644 index 0000000..2197f60 --- /dev/null +++ b/pygithub3/services/pull_requests/__init__.py @@ -0,0 +1,129 @@ +from pygithub3.exceptions import BadRequest, NotFound +from pygithub3.services.base import Service, MimeTypeMixin +from .comments import Comments + + +class PullRequests(Service, MimeTypeMixin): + """Consume `Pull Request API `_""" + + def __init__(self, **config): + self.comments = Comments(**config) + super(PullRequests, self).__init__(**config) + + def list(self, user=None, repo=None): + """List all of the pull requests for a repo + + :param str user: Username + :param str repo: Repository + + """ + return self._get_result( + self.make_request('pull_requests.list', user=user, repo=repo) + ) + + def get(self, number, user=None, repo=None): + """Get a single pull request + + :param str number: The number of the pull request to get + :param str user: Username + :param str repo: Repository + + """ + return self._get( + self.make_request('pull_requests.get', number=number, user=user, + repo=repo) + ) + + def create(self, body, user=None, repo=None): + """Create a pull request + + :param dict body: Data for the new pull request + :param str user: Username + :param str repo: Repository + + """ + return self._post( + self.make_request('pull_requests.create', body=body, user=user, + repo=repo) + ) + + def update(self, number, body, user=None, repo=None): + """Update a pull request + + :param str number: The number of the the pull request to update + :param dict body: The data to update the pull request with + :param str user: Username + :param str repo: Repository + + """ + return self._patch( + self.make_request('pull_requests.update', number=number, + body=body, user=user, repo=repo) + ) + + def list_commits(self, number, user=None, repo=None): + """List the commits for a pull request + + :param str number: The number of the pull request to list commits for + :param str user: Username + :param str repo: Repository + + """ + return self._get_result( + self.make_request('pull_requests.list_commits', number=number, + user=user, repo=repo) + ) + + def list_files(self, number, user=None, repo=None): + """List the files for a pull request + + :param str number: The number of the pull request to list files for + :param str user: Username + :param str repo: Repository + + """ + return self._get_result( + self.make_request('pull_requests.list_files', number=number, + user=user, repo=repo) + ) + + def merge_status(self, number, user=None, repo=None): + """Gets whether a pull request has been merged or not. + + :param str number: The pull request to check + :param str user: Username + :param str repo: Repository + + """ + # for this to work with a proper Resource, we would need to pass the + # response's status code to the Resource constructor, and that's kind + # of scary + try: + resp = self._client.get( + self.make_request('pull_requests.merge_status', number=number, + user=user, repo=repo) + ) + except NotFound: + return False + code = resp.status_code + if code == 204: + return True + # TODO: more flexible way to return arbitrary objects based on + # response. Probably something on Request + raise BadRequest('got code %s: %s' % (code, resp.content)) + # again, I'm sorry. + + def merge(self, number, message='', user=None, repo=None): + """Merge a pull request. + + :param str number: The pull request to merge + :param str user: Username + :param str repo: Repository + + """ + # so, the API docs don't actually say what the status code will be in + # the case of a merge failure. I hope it's not a 404. + return self._put( + self.make_request('pull_requests.merge', number=number, + message=message, user=user, repo=repo) + ) diff --git a/pygithub3/services/pull_requests/comments.py b/pygithub3/services/pull_requests/comments.py new file mode 100644 index 0000000..3aa6d0e --- /dev/null +++ b/pygithub3/services/pull_requests/comments.py @@ -0,0 +1,73 @@ +from pygithub3.services.base import Service, MimeTypeMixin + + +class Comments(Service, MimeTypeMixin): + """Consume `Review Comments API + `_ + + """ + + def list(self, number, user=None, repo=None): + """List all the comments for a pull request + + :param str number: The number of the pull request + :param str user: Username + :param str repo: Repository + + """ + return self._get_result( + self.make_request('pull_requests.comments.list', number=number, + user=user, repo=repo) + ) + + def get(self, number, user=None, repo=None): + """Get a single comment + + :param str number: The comment to get + :param str user: Username + :param str repo: Repository + + """ + return self._get( + self.make_request('pull_requests.comments.get', number=number, + user=user, repo=repo) + ) + + def create(self, number, body, user=None, repo=None): + """Create a comment + + :param str number: the pull request to comment on + :param str user: Username + :param str repo: Repository + + """ + return self._post( + self.make_request('pull_requests.comments.create', number=number, + body=body, user=user, repo=repo) + ) + + def edit(self, number, body, user=None, repo=None): + """Edit a comment + + :param str number: The id of the comment to edit + :param str user: Username + :param str repo: Repository + + """ + return self._patch( + self.make_request('pull_requests.comments.edit', number=number, + body=body, user=user, repo=repo) + ) + + def delete(self, number, user=None, repo=None): + """Delete a comment + + :param str number: The comment to delete + :param str user: Username + :param str repo: Repository + + """ + return self._delete( + self.make_request('pull_requests.comments.delete', number=number, + user=user, repo=repo) + ) diff --git a/pygithub3/tests/services/test_pull_requests.py b/pygithub3/tests/services/test_pull_requests.py new file mode 100644 index 0000000..8071b09 --- /dev/null +++ b/pygithub3/tests/services/test_pull_requests.py @@ -0,0 +1,201 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +import requests +from mock import patch, Mock +from nose.tools import raises + +from pygithub3.tests.utils.core import TestCase +from pygithub3.services.pull_requests import PullRequests, Comments +from pygithub3.resources.base import json +from pygithub3.requests.base import ValidationError +from pygithub3.tests.utils.base import (mock_response, mock_response_result, + mock_json) +from pygithub3.tests.utils.services import _ + + +json.dumps = Mock(side_effect=mock_json) +json.loads = Mock(side_effect=mock_json) + + +@patch.object(requests.sessions.Session, 'request') +class TestPullRequestsService(TestCase): + def setUp(self): + self.service = PullRequests(user='user', repo='repo') + + def test_LIST(self, reqm): + reqm.return_value = mock_response_result() + self.service.list().all() + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/pulls')) + ) + + def test_GET(self, reqm): + reqm.return_value = mock_response() + self.service.get(123) + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/pulls/123')) + ) + + def test_CREATE_with_title_and_body(self, reqm): + reqm.return_value = mock_response('post') + data = { + 'title': 'this is a pull request', + 'body': 'merge me!', + 'head': 'octocat:some-feature', + 'base': 'master', + } + self.service.create(data) + self.assertEqual( + reqm.call_args[0], + ('post', _('repos/user/repo/pulls')) + ) + + def test_CREATE_with_issue(self, reqm): + reqm.return_value = mock_response('post') + data = { + 'issue': 1, + 'head': 'octocat:some-feature', + 'base': 'master', + } + self.service.create(data) + self.assertEqual( + reqm.call_args[0], + ('post', _('repos/user/repo/pulls')) + ) + + @raises(ValidationError) + def test_CREATE_with_no_title(self, reqm): + reqm.return_value = mock_response('post') + data = { + 'body': 'merge me!', + 'head': 'octocat:some-feature', + 'base': 'master', + } + self.service.create(data) + + def test_UPDATE(self, reqm): + reqm.return_value = mock_response('patch') + data = {} + self.service.update(123, data) + self.assertEqual( + reqm.call_args[0], + ('patch', _('repos/user/repo/pulls/123')) + ) + + @raises(ValidationError) + def test_UPDATE_with_invalid_state(self, reqm): + reqm.return_value = mock_response('patch') + data = {'state': 'Illinois'} + self.service.update(123, data) + + def test_LIST_COMMITS(self, reqm): + reqm.return_value = mock_response_result('get') + self.service.list_commits(123).all() + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/pulls/123/commits')) + ) + + def test_LIST_FILES(self, reqm): + reqm.return_value = mock_response_result('get') + self.service.list_files(123).all() + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/pulls/123/files')) + ) + + def test_MERGE_STATUS_true(self, reqm): + reqm.return_value = mock_response(204) + resp = self.service.merge_status(123) + self.assertEqual(True, resp) + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/pulls/123/merge')) + ) + + def test_MERGE_STATUS_false(self, reqm): + reqm.return_value = mock_response(404) + resp = self.service.merge_status(123) + self.assertEqual(False, resp) + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/pulls/123/merge')) + ) + + def test_MERGE(self, reqm): + reqm.return_value = mock_response(200) + self.service.merge(123, 'merging this') + self.assertEqual( + reqm.call_args[0], + ('put', _('repos/user/repo/pulls/123/merge')) + ) + + +@patch.object(requests.sessions.Session, 'request') +class TestPullRequestCommentsService(TestCase): + def setUp(self): + self.service = Comments(user='user', repo='repo') + + def test_LIST(self, reqm): + reqm.return_value = mock_response_result(200) + self.service.list(123).all() + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/pulls/123/comments')) + ) + + def test_GET(self, reqm): + reqm.return_value = mock_response(200) + self.service.get(1) + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/pulls/comments/1')) + ) + + def test_CREATE(self, reqm): + reqm.return_value = mock_response(201) + data = { + 'body': ':sparkles:', + 'commit_id': 'abc123', + 'path': 'foo.txt', + 'position': '2', + } + self.service.create(1, data) + self.assertEqual( + reqm.call_args[0], + ('post', _('repos/user/repo/pulls/1/comments')) + ) + + def test_CREATE_in_reply_to(self, reqm): + reqm.return_value = mock_response(201) + data = { + 'body': ':sparkles:', + 'in_reply_to': '5', + } + self.service.create(1, data) + self.assertEqual( + reqm.call_args[0], + ('post', _('repos/user/repo/pulls/1/comments')) + ) + + def test_EDIT(self, reqm): + reqm.return_value = mock_response(200) + data = { + 'body': 'something completely different', + } + self.service.edit(1, data) + self.assertEqual( + reqm.call_args[0], + ('patch', _('repos/user/repo/pulls/comments/1')) + ) + + def test_DELETE(self, reqm): + reqm.return_value = mock_response(204) + self.service.delete(1) + self.assertEqual( + reqm.call_args[0], + ('delete', _('repos/user/repo/pulls/comments/1')) + ) diff --git a/pygithub3/tests/utils/base.py b/pygithub3/tests/utils/base.py index fb519e9..b3c3b76 100644 --- a/pygithub3/tests/utils/base.py +++ b/pygithub3/tests/utils/base.py @@ -14,7 +14,7 @@ def mock_json(content): def mock_response(status_code='get', content={}): CODES = dict(get=200, patch=200, post=201, delete=204) response = Mock(name='response') - response.status_code = CODES[str(status_code).lower()] or status_code + response.status_code = CODES.get(str(status_code).lower(), status_code) response.content = content return response -- cgit v1.2.3-59-g8ed1b From efee843e8e46894ed236811c29bb9c07e9907000 Mon Sep 17 00:00:00 2001 From: Nat Williams Date: Thu, 19 Apr 2012 09:56:56 -0500 Subject: merge request won't json encode body without schema --- pygithub3/requests/pull_requests/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'pygithub3/requests') diff --git a/pygithub3/requests/pull_requests/__init__.py b/pygithub3/requests/pull_requests/__init__.py index 7e85f8f..bd03f3e 100644 --- a/pygithub3/requests/pull_requests/__init__.py +++ b/pygithub3/requests/pull_requests/__init__.py @@ -60,3 +60,7 @@ class Merge_status(Request): class Merge(Request): uri = 'repos/{user}/{repo}/pulls/{number}/merge' resource = Raw + body_schema = { + 'schema': ('commit_message',), + 'required': (), + } -- cgit v1.2.3-59-g8ed1b From 3a82e76013624e931330e3726a22bf41088eb314 Mon Sep 17 00:00:00 2001 From: David Medina Date: Sat, 12 May 2012 17:28:52 +0200 Subject: Unify json imports --- pygithub3/core/errors.py | 6 +----- pygithub3/core/utils.py | 5 +++++ pygithub3/requests/base.py | 6 +----- pygithub3/resources/base.py | 5 +---- 4 files changed, 8 insertions(+), 14 deletions(-) (limited to 'pygithub3/requests') diff --git a/pygithub3/core/errors.py b/pygithub3/core/errors.py index 4a95df0..9b84479 100644 --- a/pygithub3/core/errors.py +++ b/pygithub3/core/errors.py @@ -1,11 +1,7 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -try: - import simplejson as json -except ImportError: - import json - +from pygithub3.core.utils import json from pygithub3.exceptions import NotFound, BadRequest, UnprocessableEntity diff --git a/pygithub3/core/utils.py b/pygithub3/core/utils.py index c6dbf0a..24a3bbf 100644 --- a/pygithub3/core/utils.py +++ b/pygithub3/core/utils.py @@ -2,6 +2,11 @@ # -*- encoding: utf-8 -*- """ Utils to support python 2.6 compatibility """ +try: + import simplejson as json +except ImportError: + import json + from collections import MutableMapping diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py index d3e18a3..13472b0 100644 --- a/pygithub3/requests/base.py +++ b/pygithub3/requests/base.py @@ -2,15 +2,11 @@ # -*- encoding: utf-8 -*- import re -try: - import simplejson as json -except ImportError: - import json +from pygithub3.core.utils import import_module, json from pygithub3.exceptions import (RequestDoesNotExist, UriInvalid, ValidationError, InvalidBodySchema) from pygithub3.resources.base import Raw -from pygithub3.core.utils import import_module ABS_IMPORT_PREFIX = 'pygithub3.requests' diff --git a/pygithub3/resources/base.py b/pygithub3/resources/base.py index 4ca2aa3..7045529 100644 --- a/pygithub3/resources/base.py +++ b/pygithub3/resources/base.py @@ -1,10 +1,7 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -try: - import simplejson as json -except ImportError: - import json +from pygithub3.core.utils import json class Resource(object): -- cgit v1.2.3-59-g8ed1b From cd7e1b88c9dfa5eaa126ece0a5529e224ae297d4 Mon Sep 17 00:00:00 2001 From: David Medina Date: Sat, 12 May 2012 17:30:05 +0200 Subject: Deleted 'validate_body' behaviour 'clean_body' had been developed to that functionality --- pygithub3/requests/base.py | 52 ++++++++++++++++------------------- pygithub3/tests/requests/test_core.py | 17 ++---------- pygithub3/tests/utils/base.py | 13 +-------- 3 files changed, 26 insertions(+), 56 deletions(-) (limited to 'pygithub3/requests') diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py index 13472b0..f49a7a9 100644 --- a/pygithub3/requests/base.py +++ b/pygithub3/requests/base.py @@ -12,12 +12,12 @@ ABS_IMPORT_PREFIX = 'pygithub3.requests' class Body(object): + """ Input's request handler """ - def __init__(self, content, valid_body, validate_body=None): + def __init__(self, content, schema, required): self.content = content - self.schema = valid_body['schema'] - self.required = valid_body['required'] - self.validate_body = validate_body or (lambda x: None) + self.schema = schema + self.required = required def dumps(self): if not self.schema: @@ -25,12 +25,12 @@ class Body(object): return json.dumps(self.parse()) def parse(self): + """ Parse body with schema-required rules """ if not hasattr(self.content, 'items'): raise ValidationError("'%s' needs a content dictionary" % self.__class__.__name__) parsed = dict([(key, self.content[key]) for key in self.schema if key in self.content]) - self.validate_body(parsed) for attr_required in self.required: if attr_required not in parsed: raise ValidationError("'%s' attribute is required" % @@ -42,22 +42,34 @@ class Body(object): class Request(object): - """ """ uri = '' resource = Raw body_schema = {} def __init__(self, **kwargs): - """ """ - self.body = kwargs.pop('body', None) + self.body = kwargs.pop('body', {}) self.args = kwargs self.clean() + def __getattr__(self, name): + return self.args.get(name) + + def __str__(self): + return self.populate_uri() + + def populate_uri(self): + try: + populated_uri = self.uri.format(**self.args) + except KeyError: + raise ValidationError( + "'%s' request wasn't be able to populate the uri '%s' with " + "'%s' args" % (self.__class__.__name__, self.uri, self.args)) + return str(populated_uri).strip('/') + def clean(self): self.uri = self.clean_uri() or self.uri - self.body = Body(self.clean_body(), self.clean_valid_body(), - self.validate_body) + self.body = Body(self.clean_body(), **self._clean_valid_body()) def clean_body(self): return self.body @@ -65,7 +77,7 @@ class Request(object): def clean_uri(self): return None - def clean_valid_body(self): + def _clean_valid_body(self): schema = set(self.body_schema.get('schema', ())) required = set(self.body_schema.get('required', ())) if not required.issubset(schema): @@ -75,27 +87,9 @@ class Request(object): self.__class__.__name__, required, schema)) return dict(schema=schema, required=required) - def __getattr__(self, name): - return self.args.get(name) - - def __str__(self): - return self.populate_uri() - - def populate_uri(self): - try: - populated_uri = self.uri.format(**self.args) - except KeyError: - raise ValidationError( - "'%s' request wasn't be able to populate the uri '%s' with " - "'%s' args" % (self.__class__.__name__, self.uri, self.args)) - return str(populated_uri).strip('/') - def get_body(self): return self.body.dumps() - def validate_body(self, *args): - pass - class Factory(object): """ Request builder """ diff --git a/pygithub3/tests/requests/test_core.py b/pygithub3/tests/requests/test_core.py index 4632056..110f00e 100644 --- a/pygithub3/tests/requests/test_core.py +++ b/pygithub3/tests/requests/test_core.py @@ -2,14 +2,12 @@ # -*- encoding: utf-8 -*- from mock import Mock -from nose.tools import raises from pygithub3.tests.utils.core import TestCase from pygithub3.requests.base import Factory, Body, json, Request from pygithub3.exceptions import (UriInvalid, RequestDoesNotExist, ValidationError, InvalidBodySchema) -from pygithub3.tests.utils.base import (mock_json, DummyRequest, - DummyRequestValidation) +from pygithub3.tests.utils.base import mock_json, DummyRequest from pygithub3.tests.utils.requests import ( RequestWithArgs, RequestCleanedUri, RequestBodyInvalidSchema, RequestCleanedBody) @@ -76,7 +74,7 @@ class TestRequestBodyWithSchema(TestCase): def setUp(self): valid_body = dict(schema=('arg1', 'arg2'), required=('arg1', )) - self.b = Body({}, valid_body) + self.b = Body({}, **valid_body) def test_with_body_empty_and_schema_permissive(self): self.b.schema = ('arg1', 'arg2', '...') @@ -102,14 +100,3 @@ class TestRequestBodyWithSchema(TestCase): def test_only_valid_keys(self): self.b.content = dict(arg1='arg1', arg2='arg2', fake='test') self.assertEqual(self.b.dumps(), dict(arg1='arg1', arg2='arg2')) - - -class TestBodyValidation(TestCase): - @raises(ValidationError) - def test_with_error(self): - req = DummyRequestValidation( - body={'foo': 'bar', 'error': 'yes'}, - ) - req.body_schema = {'schema': ('foo',), - 'required': ('foo',)} - req.get_body() diff --git a/pygithub3/tests/utils/base.py b/pygithub3/tests/utils/base.py index b3c3b76..f90b5d3 100644 --- a/pygithub3/tests/utils/base.py +++ b/pygithub3/tests/utils/base.py @@ -4,7 +4,7 @@ from mock import Mock from pygithub3.resources.base import Resource -from pygithub3.requests.base import Request, ValidationError +from pygithub3.requests.base import Request def mock_json(content): @@ -37,14 +37,3 @@ DummyResource.loads = Mock(side_effect=loads_mock) class DummyRequest(Request): uri = 'dummyrequest' resource = DummyResource - - -class DummyRequestValidation(DummyRequest): - body_schema = { - 'schema': ('foo', 'error'), - 'required': ('foo',) - } - - def validate_body(self, body): - if body.get('error') == 'yes': - raise ValidationError('yes') -- cgit v1.2.3-59-g8ed1b From 29d43d19caa3c0eccfaff7005dd35d411b5dbc74 Mon Sep 17 00:00:00 2001 From: David Medina Date: Sat, 12 May 2012 17:46:06 +0200 Subject: Deleted 'dispatch' decorator. No sense --- pygithub3/requests/base.py | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) (limited to 'pygithub3/requests') diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py index f49a7a9..c4fe5cc 100644 --- a/pygithub3/requests/base.py +++ b/pygithub3/requests/base.py @@ -106,28 +106,21 @@ class Factory(object): return func(self, request_uri.lower(), **kwargs) return wrapper - def dispatch(func): - def wrapper(self, request_uri, **kwargs): - module_chunk, s, request_chunk = request_uri.rpartition('.') - request_chunk = request_chunk.capitalize() - try: - # TODO: CamelCase and under_score support, now only Class Name - module = import_module('%s.%s' - % (ABS_IMPORT_PREFIX, module_chunk)) - request = getattr(module, request_chunk) - except ImportError: - raise RequestDoesNotExist("'%s' module does not exist" - % module_chunk) - except AttributeError: - raise RequestDoesNotExist( - "'%s' request does not exist in '%s' module" - % (request_chunk, module_chunk)) - return func(self, request, **kwargs) - return wrapper - @validate - @dispatch - def __call__(self, request='', **kwargs): - request = request(**kwargs) - assert isinstance(request, Request) - return request + def __call__(self, request_uri, **kwargs): + module_chunk, s, request_chunk = request_uri.rpartition('.') + request_chunk = request_chunk.capitalize() + try: + # TODO: CamelCase and under_score support, now only Class Name + module = import_module('%s.%s' % (ABS_IMPORT_PREFIX, module_chunk)) + request_class = getattr(module, request_chunk) + request = request_class(**kwargs) + assert isinstance(request, Request) + return request + except ImportError: + raise RequestDoesNotExist("'%s' module does not exist" + % module_chunk) + except AttributeError: + raise RequestDoesNotExist("'%s' request does not exist in " + "'%s' module" % (request_chunk, + module_chunk)) -- cgit v1.2.3-59-g8ed1b From 24a3ed5dcd2264a64e234ea7bd526049fafe7616 Mon Sep 17 00:00:00 2001 From: David Medina Date: Sat, 12 May 2012 19:04:23 +0200 Subject: Some fixes/typos and 'validate_body' related --- docs/pull_requests.rst | 8 ++++-- pygithub3/core/client.py | 2 +- pygithub3/requests/pull_requests/__init__.py | 20 +++++++------ pygithub3/requests/pull_requests/comments.py | 15 ++++++---- pygithub3/resources/pull_requests.py | 2 ++ pygithub3/services/pull_requests/__init__.py | 40 ++++++++++++++++++++------ pygithub3/services/pull_requests/comments.py | 31 +++++++++++++------- pygithub3/tests/services/test_pull_requests.py | 19 +++--------- 8 files changed, 84 insertions(+), 53 deletions(-) (limited to 'pygithub3/requests') diff --git a/docs/pull_requests.rst b/docs/pull_requests.rst index 40c3435..09313eb 100644 --- a/docs/pull_requests.rst +++ b/docs/pull_requests.rst @@ -7,11 +7,10 @@ Pull Requests service from pygithub3 import Github - gh = Github() + gh = Github(user='octocat', repo='sample') pull_requests = gh.pull_requests.list().all() - for pr in pull_requests: - commits = gh.pull_requests.list_commits(pr.number).all() + pull_request_commits = gh.pull_requests.list_commits(2512).all() Pull Requests ------------- @@ -31,3 +30,6 @@ Pull Request Comments .. autoclass:: pygithub3.services.pull_requests.Comments :members: + +.. _github pullrequests doc: http://developer.github.com/v3/pulls +.. _github pullrequests comments doc: http://developer.github.com/v3/pulls/comments diff --git a/pygithub3/core/client.py b/pygithub3/core/client.py index eadb18e..ee7c97f 100644 --- a/pygithub3/core/client.py +++ b/pygithub3/core/client.py @@ -81,7 +81,7 @@ class Client(object): def get(self, request, **kwargs): response = self.request('get', request, **kwargs) - # there are valid GET responses that != 200 + assert response.status_code == 200 return response def post(self, request, **kwargs): diff --git a/pygithub3/requests/pull_requests/__init__.py b/pygithub3/requests/pull_requests/__init__.py index bd03f3e..f25572d 100644 --- a/pygithub3/requests/pull_requests/__init__.py +++ b/pygithub3/requests/pull_requests/__init__.py @@ -1,5 +1,6 @@ +# -*- encoding: utf-8 -*- + from pygithub3.requests.base import Request, ValidationError -from pygithub3.resources.base import Raw from pygithub3.resources.pull_requests import PullRequest, File from pygithub3.resources.repos import Commit @@ -22,11 +23,12 @@ class Create(Request): 'required': ('base', 'head'), } - def validate_body(self, parsed): - if (not ('title' in parsed and 'body' in parsed) and - not 'issue' in parsed): + def clean_body(self): + if (not ('title' in self.body and 'body' in self.body) and + not 'issue' in self.body): raise ValidationError('pull request creation requires either an ' 'issue number or a title and body') + return self.body class Update(Request): uri = 'repos/{user}/{repo}/pulls/{number}' @@ -36,10 +38,12 @@ class Update(Request): 'required': (), } - def validate_body(self, body): - if 'state' in body and body['state'] not in ['open', 'closed']: + def clean_body(self): + if ('state' in self.body and + self.body['state'] not in ['open', 'closed']): raise ValidationError('If a state is specified, it must be one ' 'of "open" or "closed"') + return self.body class List_commits(Request): @@ -52,14 +56,12 @@ class List_files(Request): resource = File -class Merge_status(Request): +class Is_merged(Request): uri = 'repos/{user}/{repo}/pulls/{number}/merge' - resource = Raw class Merge(Request): uri = 'repos/{user}/{repo}/pulls/{number}/merge' - resource = Raw body_schema = { 'schema': ('commit_message',), 'required': (), diff --git a/pygithub3/requests/pull_requests/comments.py b/pygithub3/requests/pull_requests/comments.py index fa1e5b6..dd69894 100644 --- a/pygithub3/requests/pull_requests/comments.py +++ b/pygithub3/requests/pull_requests/comments.py @@ -1,4 +1,6 @@ -from pygithub3.requests.base import Request +# -*- encoding: utf-8 -*- + +from pygithub3.requests.base import Request, ValidationError from pygithub3.resources.pull_requests import Comment @@ -20,13 +22,14 @@ class Create(Request): 'required': ('body',), } - def validate_body(self, body): - if (not ('commit_id' in body and - 'path' in body and - 'position' in body) and - not 'in_reply_to' in body): + def clean_body(self): + if (not ('commit_id' in self.body and + 'path' in self.body and + 'position' in self.body) and + not 'in_reply_to' in self.body): raise ValidationError('supply either in_reply_to or commit_id, ' 'path, and position') + return self.body class Edit(Request): diff --git a/pygithub3/resources/pull_requests.py b/pygithub3/resources/pull_requests.py index 1fe2623..a28e0fe 100644 --- a/pygithub3/resources/pull_requests.py +++ b/pygithub3/resources/pull_requests.py @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + from .base import Resource diff --git a/pygithub3/services/pull_requests/__init__.py b/pygithub3/services/pull_requests/__init__.py index 66d9e58..545f862 100644 --- a/pygithub3/services/pull_requests/__init__.py +++ b/pygithub3/services/pull_requests/__init__.py @@ -1,4 +1,5 @@ -from pygithub3.exceptions import BadRequest, NotFound +# -*- encoding: utf-8 -*- + from pygithub3.services.base import Service, MimeTypeMixin from .comments import Comments @@ -15,7 +16,10 @@ class PullRequests(Service, MimeTypeMixin): :param str user: Username :param str repo: Repository + :returns: A :doc:`result` + .. note:: + Remember :ref:`config precedence` """ return self._get_result( self.make_request('pull_requests.list', user=user, repo=repo) @@ -28,37 +32,43 @@ class PullRequests(Service, MimeTypeMixin): :param str user: Username :param str repo: Repository + .. note:: + Remember :ref:`config precedence` """ return self._get( self.make_request('pull_requests.get', number=number, user=user, repo=repo) ) - def create(self, body, user=None, repo=None): + def create(self, data, user=None, repo=None): """Create a pull request - :param dict body: Data for the new pull request + :param dict data: Input. See `github pullrequests doc`_ :param str user: Username :param str repo: Repository + .. note:: + Remember :ref:`config precedence` """ return self._post( - self.make_request('pull_requests.create', body=body, user=user, + self.make_request('pull_requests.create', body=data, user=user, repo=repo) ) - def update(self, number, body, user=None, repo=None): + def update(self, number, data, user=None, repo=None): """Update a pull request :param str number: The number of the the pull request to update - :param dict body: The data to update the pull request with + :param dict data: Input. See `github pullrequests doc`_ :param str user: Username :param str repo: Repository + .. note:: + Remember :ref:`config precedence` """ return self._patch( self.make_request('pull_requests.update', number=number, - body=body, user=user, repo=repo) + body=data, user=user, repo=repo) ) def list_commits(self, number, user=None, repo=None): @@ -67,7 +77,10 @@ class PullRequests(Service, MimeTypeMixin): :param str number: The number of the pull request to list commits for :param str user: Username :param str repo: Repository + :returns: A :doc:`result` + .. note:: + Remember :ref:`config precedence` """ return self._get_result( self.make_request('pull_requests.list_commits', number=number, @@ -80,23 +93,28 @@ class PullRequests(Service, MimeTypeMixin): :param str number: The number of the pull request to list files for :param str user: Username :param str repo: Repository + :returns: A :doc:`result` + .. note:: + Remember :ref:`config precedence` """ return self._get_result( self.make_request('pull_requests.list_files', number=number, user=user, repo=repo) ) - def merge_status(self, number, user=None, repo=None): + def is_merged(self, number, user=None, repo=None): """Gets whether a pull request has been merged or not. :param str number: The pull request to check :param str user: Username :param str repo: Repository + .. note:: + Remember :ref:`config precedence` """ return self._bool( - self.make_request('pull_requests.merge_status', number=number, + self.make_request('pull_requests.is_merged', number=number, user=user, repo=repo) ) @@ -104,9 +122,13 @@ class PullRequests(Service, MimeTypeMixin): """Merge a pull request. :param str number: The pull request to merge + :param str message: Message of pull request :param str user: Username :param str repo: Repository + .. note:: + Remember :ref:`config precedence` + This currently raises an HTTP 405 error if the request is not mergable. diff --git a/pygithub3/services/pull_requests/comments.py b/pygithub3/services/pull_requests/comments.py index 3aa6d0e..2edbfdf 100644 --- a/pygithub3/services/pull_requests/comments.py +++ b/pygithub3/services/pull_requests/comments.py @@ -1,11 +1,10 @@ +# -*- encoding: utf-8 -*- from pygithub3.services.base import Service, MimeTypeMixin class Comments(Service, MimeTypeMixin): """Consume `Review Comments API - `_ - - """ + `_ """ def list(self, number, user=None, repo=None): """List all the comments for a pull request @@ -13,7 +12,10 @@ class Comments(Service, MimeTypeMixin): :param str number: The number of the pull request :param str user: Username :param str repo: Repository + :returns: A :doc:`result` + .. note:: + Remember :ref:`config precedence` """ return self._get_result( self.make_request('pull_requests.comments.list', number=number, @@ -27,37 +29,44 @@ class Comments(Service, MimeTypeMixin): :param str user: Username :param str repo: Repository + .. note:: + Remember :ref:`config precedence` """ return self._get( self.make_request('pull_requests.comments.get', number=number, user=user, repo=repo) ) - def create(self, number, body, user=None, repo=None): + def create(self, number, data, user=None, repo=None): """Create a comment :param str number: the pull request to comment on + :param dict data: Input. See `github pullrequests comments doc`_ :param str user: Username :param str repo: Repository + .. note:: + Remember :ref:`config precedence` """ return self._post( self.make_request('pull_requests.comments.create', number=number, - body=body, user=user, repo=repo) + body=data, user=user, repo=repo) ) - def edit(self, number, body, user=None, repo=None): + def update(self, number, message, user=None, repo=None): """Edit a comment :param str number: The id of the comment to edit + :param str message: Comment message :param str user: Username :param str repo: Repository + .. note:: + Remember :ref:`config precedence` """ - return self._patch( - self.make_request('pull_requests.comments.edit', number=number, - body=body, user=user, repo=repo) - ) + request = self.make_request('pull_requests.comments.edit', + number=number, body={'body': message}, user=user, repo=repo) + return self._patch(request) def delete(self, number, user=None, repo=None): """Delete a comment @@ -66,6 +75,8 @@ class Comments(Service, MimeTypeMixin): :param str user: Username :param str repo: Repository + .. note:: + Remember :ref:`config precedence` """ return self._delete( self.make_request('pull_requests.comments.delete', number=number, diff --git a/pygithub3/tests/services/test_pull_requests.py b/pygithub3/tests/services/test_pull_requests.py index 7fc15b7..93646df 100644 --- a/pygithub3/tests/services/test_pull_requests.py +++ b/pygithub3/tests/services/test_pull_requests.py @@ -7,8 +7,7 @@ from nose.tools import raises from pygithub3.tests.utils.core import TestCase from pygithub3.services.pull_requests import PullRequests, Comments -from pygithub3.resources.base import json -from pygithub3.requests.base import ValidationError +from pygithub3.requests.base import ValidationError, json from pygithub3.tests.utils.base import (mock_response, mock_response_result, mock_json) from pygithub3.tests.utils.services import _ @@ -107,19 +106,9 @@ class TestPullRequestsService(TestCase): ('get', _('repos/user/repo/pulls/123/files')) ) - def test_MERGE_STATUS_true(self, reqm): - reqm.return_value = mock_response(204) - resp = self.service.merge_status(123) - self.assertEqual(True, resp) - self.assertEqual( - reqm.call_args[0], - ('head', _('repos/user/repo/pulls/123/merge')) - ) - - def test_MERGE_STATUS_false(self, reqm): - reqm.return_value = mock_response(404) - resp = self.service.merge_status(123) - self.assertEqual(False, resp) + def test_IS_MERGED(self, reqm): + resp = self.service.is_merged(123) + self.assertTrue(resp) self.assertEqual( reqm.call_args[0], ('head', _('repos/user/repo/pulls/123/merge')) -- cgit v1.2.3-59-g8ed1b