diff options
Diffstat (limited to 'pygithub3')
31 files changed, 1165 insertions, 68 deletions
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/exceptions.py b/pygithub3/exceptions.py index a467256..9ee2597 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 @@ -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 ba403e6..d135865 100644 --- a/pygithub3/github.py +++ b/pygithub3/github.py @@ -17,10 +17,14 @@ class Github(object): from pygithub3.services.users import User from pygithub3.services.repos import Repo from pygithub3.services.gists import Gist + from pygithub3.services.git_data import GitData + from pygithub3.services.pull_requests import PullRequests from pygithub3.services.orgs import Org self._users = User(**config) self._repos = Repo(**config) self._gists = Gist(**config) + self._git_data = GitData(**config) + self._pull_requests = PullRequests(**config) self._orgs = Org(**config) @property @@ -51,6 +55,19 @@ class Github(object): return self._gists @property + def git_data(self): + """ + :ref:`Git Data service <Git Data service>` + """ + return self._git_data + + @property + def pull_requests(self): + """ + :ref:`Pull Requests service <Pull Requests service>` + """ + return self._pull_requests + def orgs(self): """ :ref:`Orgs service <Orgs service>` diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py index 03b0f8a..c4fe5cc 100644 --- a/pygithub3/requests/base.py +++ b/pygithub3/requests/base.py @@ -2,20 +2,17 @@ # -*- encoding: utf-8 -*- import re -try: - import simplejson as json -except ImportError: - import json -from pygithub3.exceptions import (DoesNotExists, UriInvalid, ValidationError, - InvalidBodySchema) +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' class Body(object): + """ Input's request handler """ def __init__(self, content, schema, required): self.content = content @@ -28,6 +25,7 @@ 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__) @@ -44,21 +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.body = Body(self.clean_body(), **self._clean_valid_body()) def clean_body(self): return self.body @@ -66,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): @@ -76,21 +87,6 @@ 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() @@ -110,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 DoesNotExists("'%s' module does not exists" - % module_chunk) - except AttributeError: - raise DoesNotExists( - "'%s' request doesn't exists into '%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)) diff --git a/pygithub3/requests/git_data/__init__.py b/pygithub3/requests/git_data/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pygithub3/requests/git_data/__init__.py diff --git a/pygithub3/requests/git_data/blobs.py b/pygithub3/requests/git_data/blobs.py new file mode 100644 index 0000000..a4bddd6 --- /dev/null +++ b/pygithub3/requests/git_data/blobs.py @@ -0,0 +1,18 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.requests.base import Request +from pygithub3.resources.git_data import Blob + + +class Get(Request): + uri = 'repos/{user}/{repo}/git/blobs/{sha}' + resource = Blob + + +class Create(Request): + uri = 'repos/{user}/{repo}/git/blobs' + resource = Blob + body_schema = { + 'schema': ('content', 'encoding'), + 'required': ('content', 'encoding'), # TODO: is enc really required? + } diff --git a/pygithub3/requests/git_data/commits.py b/pygithub3/requests/git_data/commits.py new file mode 100644 index 0000000..a26e07d --- /dev/null +++ b/pygithub3/requests/git_data/commits.py @@ -0,0 +1,18 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.requests.base import Request +from pygithub3.resources.repos import Commit + + +class Get(Request): + uri = 'repos/{user}/{repo}/git/commits/{sha}' + resource = Commit + + +class Create(Request): + uri = 'repos/{user}/{repo}/git/commits' + resource = Commit + body_schema = { + 'schema': ('message', 'tree', 'parents', 'author', 'committer'), + 'required': ('message', 'tree', 'parents'), + } diff --git a/pygithub3/requests/git_data/references.py b/pygithub3/requests/git_data/references.py new file mode 100644 index 0000000..2aac6a4 --- /dev/null +++ b/pygithub3/requests/git_data/references.py @@ -0,0 +1,33 @@ +from pygithub3.requests.base import Request +from pygithub3.resources.git_data import Reference + + +class Get(Request): + uri = 'repos/{user}/{repo}/git/refs/{ref}' + resource = Reference + + +class List(Request): + uri = 'repos/{user}/{repo}/git/refs/{namespace}' + resource = Reference + + +class Create(Request): + uri = 'repos/{user}/{repo}/git/refs' + resource = Reference + body_schema = { + 'schema': ('ref', 'sha'), + 'required': ('ref', 'sha'), + } + +class Update(Request): + uri = 'repos/{user}/{repo}/git/refs/{ref}' + resource = Reference + body_schema = { + 'schema': ('sha', 'force'), + 'required': ('sha',), + } + + +class Delete(Request): + uri = 'repos/{user}/{repo}/git/refs/{ref}' diff --git a/pygithub3/requests/git_data/tags.py b/pygithub3/requests/git_data/tags.py new file mode 100644 index 0000000..dbc8da4 --- /dev/null +++ b/pygithub3/requests/git_data/tags.py @@ -0,0 +1,15 @@ +from pygithub3.requests.base import Request +from pygithub3.resources.git_data import Tag + + +class Get(Request): + uri = 'repos/{user}/{repo}/git/tags/{sha}' + resource = Tag + +class Create(Request): + uri = 'repos/{user}/{repo}/git/tags' + resource = Tag + body_schema = { + 'schema': ('tag', 'message', 'object', 'type', 'tagger'), + 'required': ('type',), + } diff --git a/pygithub3/requests/git_data/trees.py b/pygithub3/requests/git_data/trees.py new file mode 100644 index 0000000..11d6409 --- /dev/null +++ b/pygithub3/requests/git_data/trees.py @@ -0,0 +1,16 @@ +from pygithub3.requests.base import Request +from pygithub3.resources.git_data import Tree + + +class Get(Request): + uri = 'repos/{user}/{repo}/git/trees/{sha}' + resource = Tree + + +class Create(Request): + uri = 'repos/{user}/{repo}/git/trees' + resource = Tree + body_schema = { + 'schema': ('tree', 'base_tree'), + 'required': ('tree',), + } diff --git a/pygithub3/requests/pull_requests/__init__.py b/pygithub3/requests/pull_requests/__init__.py new file mode 100644 index 0000000..f25572d --- /dev/null +++ b/pygithub3/requests/pull_requests/__init__.py @@ -0,0 +1,68 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.requests.base import Request, ValidationError +from pygithub3.resources.pull_requests import PullRequest, File +from pygithub3.resources.repos import Commit + + +class List(Request): + uri = 'repos/{user}/{repo}/pulls' + resource = PullRequest + + +class Get(Request): + uri = 'repos/{user}/{repo}/pulls/{number}' + resource = PullRequest + + +class Create(Request): + uri = 'repos/{user}/{repo}/pulls' + resource = PullRequest + body_schema = { + 'schema': ('title', 'body', 'base', 'head', 'issue'), + 'required': ('base', 'head'), + } + + 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}' + resource = PullRequest + body_schema = { + 'schema': ('title', 'body', 'state'), + 'required': (), + } + + 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): + uri = 'repos/{user}/{repo}/pulls/{number}/commits' + resource = Commit + + +class List_files(Request): + uri = 'repos/{user}/{repo}/pulls/{number}/files' + resource = File + + +class Is_merged(Request): + uri = 'repos/{user}/{repo}/pulls/{number}/merge' + + +class Merge(Request): + uri = 'repos/{user}/{repo}/pulls/{number}/merge' + body_schema = { + 'schema': ('commit_message',), + 'required': (), + } diff --git a/pygithub3/requests/pull_requests/comments.py b/pygithub3/requests/pull_requests/comments.py new file mode 100644 index 0000000..1904f6c --- /dev/null +++ b/pygithub3/requests/pull_requests/comments.py @@ -0,0 +1,46 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.requests.base import Request, ValidationError +from pygithub3.resources.pull_requests import Comment + + +class List(Request): + uri = 'repos/{user}/{repo}/pulls/{number}/comments' + resource = Comment + + +class Get(Request): + uri = 'repos/{user}/{repo}/pulls/comments/{number}' + resource = Comment + + +class Create(Request): + uri = 'repos/{user}/{repo}/pulls/{number}/comments' + resource = Comment + body_schema = { + 'schema': ('body', 'commit_id', 'path', 'position', 'in_reply_to'), + 'required': ('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 Update(Request): + uri = 'repos/{user}/{repo}/pulls/comments/{number}' + resource = Comment + body_schema = { + 'schema': ('body',), + 'required': ('body',), + } + + +class Delete(Request): + uri = 'repos/{user}/{repo}/pulls/comments/{number}' + resource = Comment 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): diff --git a/pygithub3/resources/gists.py b/pygithub3/resources/gists.py index 7e9550a..89425bf 100644 --- a/pygithub3/resources/gists.py +++ b/pygithub3/resources/gists.py @@ -15,6 +15,7 @@ class Fork(Resource): _dates = ('created_at', ) _maps = {'user': User} + def __str__(self): return '<GistFork>' diff --git a/pygithub3/resources/git_data.py b/pygithub3/resources/git_data.py new file mode 100644 index 0000000..4d12e01 --- /dev/null +++ b/pygithub3/resources/git_data.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from .base import Resource +from .repos import Author, Commit + + +class Blob(Resource): + def __str__(self): + return "<Blob (%s)>" % getattr(self, 'content', '') + + +class Reference(Resource): + def __str__(self): + return '<Reference (%s)>' % getattr(self, 'ref', '') + + +class Tag(Resource): + _maps = {'object': Commit, + 'tagger': Author} # committer? tagger? + + def __str__(self): + return '<Tag (%s)>' % getattr(self, 'tag', '') + + +class Tree(Resource): + def __str__(self): + return '<Tree (%s)>' % getattr(self, 'sha', '') diff --git a/pygithub3/resources/pull_requests.py b/pygithub3/resources/pull_requests.py new file mode 100644 index 0000000..a28e0fe --- /dev/null +++ b/pygithub3/resources/pull_requests.py @@ -0,0 +1,22 @@ +# -*- encoding: utf-8 -*- + +from .base import Resource + + +class PullRequest(Resource): + _dates = ('created_at', 'updated_at', 'closed_at', 'merged_at') + + def __str__(self): + return '<PullRequest (%s)>' % getattr(self, 'title', '') + + +class File(Resource): + def __str__(self): + return '<File (%s)>' % getattr(self, 'filename', '') + + +class Comment(Resource): + _dates = ('created_at', 'updated_at') + + def __str__(self): + return '<Comment (#%s)>' % getattr(self, 'id', '') diff --git a/pygithub3/resources/repos.py b/pygithub3/resources/repos.py index f50d694..85e24c7 100644 --- a/pygithub3/resources/repos.py +++ b/pygithub3/resources/repos.py @@ -10,7 +10,7 @@ from .orgs import Org class Repo(Resource): - _dates = ('created_at', 'pushed_at') + _dates = ('created_at', 'updated_at', 'pushed_at') _maps = {'owner': User, 'organization': Org, 'parent': 'self', 'source': 'self'} diff --git a/pygithub3/services/base.py b/pygithub3/services/base.py index c91bc72..649b2b3 100644 --- a/pygithub3/services/base.py +++ b/pygithub3/services/base.py @@ -80,6 +80,12 @@ class Service(object): """ self._client.set_token(token) + #TODO: Refact as decorator:: + """ + Reason: make_request and request_builder ... are confusing names + @precedence('user') + def list(self, sha, user=None): + """ def make_request(self, request, **kwargs): if 'user' in kwargs: kwargs['user'] = kwargs['user'] or self.get_user() @@ -146,6 +152,7 @@ class Service(object): return normal.Result(method) +# XXX: Refact to set_<type> method class MimeTypeMixin(object): """ Mimetype support to Services diff --git a/pygithub3/services/git_data/__init__.py b/pygithub3/services/git_data/__init__.py new file mode 100644 index 0000000..4a3ff9f --- /dev/null +++ b/pygithub3/services/git_data/__init__.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service +from .blobs import Blobs +from .commits import Commits +from .references import References +from .tags import Tags +from .trees import Trees + +class GitData(Service): + """Consume `Git Data API <http://developer.github.com/v3/git/>`_""" + + def __init__(self, **config): + self.blobs = Blobs(**config) + self.commits = Commits(**config) + self.references = References(**config) + self.tags = Tags(**config) + self.trees = Trees(**config) + super(GitData, self).__init__(**config) diff --git a/pygithub3/services/git_data/blobs.py b/pygithub3/services/git_data/blobs.py new file mode 100644 index 0000000..f6a2ff3 --- /dev/null +++ b/pygithub3/services/git_data/blobs.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service, MimeTypeMixin + + +class Blobs(Service, MimeTypeMixin): + """Consume `Blobs API <http://developer.github.com/v3/git/blobs/>`_""" + + def get(self, sha, user=None, repo=None): + """Get a particular blob + + :param str sha: The sha of the blob to get + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.blobs.get', sha=sha, + user=user, repo=repo) + return self._get(request, **self._get_mimetype_as_header()) + + def create(self, data, user=None, repo=None): + """Create a blob + + :param dict data: Data describing the blob to create + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.blobs.create', body=data, + user=user, repo=repo) + return self._post(request) diff --git a/pygithub3/services/git_data/commits.py b/pygithub3/services/git_data/commits.py new file mode 100644 index 0000000..ddeed98 --- /dev/null +++ b/pygithub3/services/git_data/commits.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service + + +class Commits(Service): + """Consume `Commits API <http://developer.github.com/v3/git/commits/>`_""" + + def get(self, sha, user=None, repo=None): + """get a commit from the current repo + + :param str sha: SHA of the Commit that you want + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.commits.get', sha=sha, + user=user, repo=repo) + return self._get(request) + + def create(self, data, user=None, repo=None): + """create a commit on a repo + + :param dict data: Input. See `github commits doc`_ + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.commits.create', user=user, + repo=repo, body=data) + return self._post(request) diff --git a/pygithub3/services/git_data/references.py b/pygithub3/services/git_data/references.py new file mode 100644 index 0000000..0b46062 --- /dev/null +++ b/pygithub3/services/git_data/references.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service + + +class References(Service): + """Consume `References API <http://developer.github.com/v3/git/refs/>`_""" + + def get(self, ref, user=None, repo=None): + """ Get a reference + + :param str ref: The name of the reference to get + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + + .. note:: + Remember that branch references look like "heads/<branch_name>" + """ + request = self.make_request('git_data.references.get', ref=ref, + user=user, repo=repo) + return self._get(request) + + def list(self, namespace='', user=None, repo=None): + """ List all the references + + :param str namespace: Limit the request to a particular type of + reference. For example, ``heads`` or ``tags``. + :param str user: Username + :param str repo: Repository + :returns: A :doc:`result` + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.references.list', user=user, + repo=repo, namespace=namespace) + return self._get_result(request) + + def create(self, data, user=None, repo=None): + """ Create a reference + + :param dict data: Input. See `github refs doc`_ + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.references.create', body=data, + user=user, repo=repo) + return self._post(request) + + def update(self, ref, data, user=None, repo=None): + """ Update an existing reference + + :param str ref: The SHA of the reference to update + :param dict data: Input. See `github refs doc`_ + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.references.update', ref=ref, + body=data, user=user, repo=repo) + return self._patch(request) + + def delete(self, ref, user=None, repo=None): + """Delete a reference + + :param str ref: The SHA of the reference to delete + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.references.delete', ref=ref, + user=user, repo=repo) + return self._delete(request) diff --git a/pygithub3/services/git_data/tags.py b/pygithub3/services/git_data/tags.py new file mode 100644 index 0000000..337f6f0 --- /dev/null +++ b/pygithub3/services/git_data/tags.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service + + +class Tags(Service): + """Consume `Tags API <http://developer.github.com/v3/git/tags/>`_""" + + def get(self, sha, user=None, repo=None): + """ Get a tag + + :param str sha: The sha of the tag to get. + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.tags.get', sha=sha, user=user, + repo=repo) + return self._get(request) + + def create(self, data, user=None, repo=None): + """ Create a tag + + :param dict data: Input. See `github tags doc`_ + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.tags.create', body=data, + user=user, repo=repo) + return self._post(request) diff --git a/pygithub3/services/git_data/trees.py b/pygithub3/services/git_data/trees.py new file mode 100644 index 0000000..6032e74 --- /dev/null +++ b/pygithub3/services/git_data/trees.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service + + +class Trees(Service): + """Consume `Trees API <http://developer.github.com/v3/git/trees/>`_""" + + def get(self, sha, recursive=False, user=None, repo=None): + """ Get a tree object + + :param str sha: The SHA of the tree you want. + :param bool recursive: Whether to resolve each sub-tree belonging to + this tree + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.trees.get', sha=sha, user=user, + repo=repo) + return self._get(request, recursive=recursive) + + def create(self, data, user=None, repo=None): + """ Create a tree object + + :param dict data: Input. See `github trees doc`_ + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('git_data.trees.create', body=data, + user=user, repo=repo) + return self._post(request) diff --git a/pygithub3/services/pull_requests/__init__.py b/pygithub3/services/pull_requests/__init__.py new file mode 100644 index 0000000..545f862 --- /dev/null +++ b/pygithub3/services/pull_requests/__init__.py @@ -0,0 +1,140 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service, MimeTypeMixin +from .comments import Comments + + +class PullRequests(Service, MimeTypeMixin): + """Consume `Pull Request API <http://developer.github.com/v3/pulls/>`_""" + + 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 + :returns: A :doc:`result` + + .. note:: + Remember :ref:`config precedence` + """ + 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 + + .. note:: + Remember :ref:`config precedence` + """ + return self._get( + self.make_request('pull_requests.get', number=number, user=user, + repo=repo) + ) + + def create(self, data, user=None, repo=None): + """Create a 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=data, user=user, + repo=repo) + ) + + 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 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=data, 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 + :returns: A :doc:`result` + + .. note:: + Remember :ref:`config precedence` + """ + 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 + :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 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.is_merged', number=number, + user=user, repo=repo) + ) + + def merge(self, number, message='', user=None, repo=None): + """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. + + """ + body = {'commit_message': message} + return self._put( + self.make_request('pull_requests.merge', number=number, + body=body, 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..ebc4e4b --- /dev/null +++ b/pygithub3/services/pull_requests/comments.py @@ -0,0 +1,84 @@ +# -*- encoding: utf-8 -*- +from pygithub3.services.base import Service, MimeTypeMixin + + +class Comments(Service, MimeTypeMixin): + """Consume `Review Comments API + <http://developer.github.com/v3/pulls/comments/>`_ """ + + 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 + :returns: A :doc:`result` + + .. note:: + Remember :ref:`config precedence` + """ + 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 + + .. 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, 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=data, user=user, repo=repo) + ) + + def update(self, number, message, user=None, repo=None): + """Update a comment + + :param str number: The id of the comment to update + :param str message: Comment message + :param str user: Username + :param str repo: Repository + + .. note:: + Remember :ref:`config precedence` + """ + request = self.make_request('pull_requests.comments.update', + number=number, body={'body': message}, user=user, repo=repo) + return self._patch(request) + + 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 + + .. note:: + Remember :ref:`config precedence` + """ + return self._delete( + self.make_request('pull_requests.comments.delete', number=number, + user=user, repo=repo) + ) diff --git a/pygithub3/tests/requests/test_core.py b/pygithub3/tests/requests/test_core.py index cd162b3..110f00e 100644 --- a/pygithub3/tests/requests/test_core.py +++ b/pygithub3/tests/requests/test_core.py @@ -5,8 +5,8 @@ from mock import Mock 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 from pygithub3.tests.utils.requests import ( RequestWithArgs, RequestCleanedUri, RequestBodyInvalidSchema, @@ -27,8 +27,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 diff --git a/pygithub3/tests/services/test_git_data.py b/pygithub3/tests/services/test_git_data.py new file mode 100644 index 0000000..da1abf7 --- /dev/null +++ b/pygithub3/tests/services/test_git_data.py @@ -0,0 +1,157 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +import requests +from mock import patch, Mock + +from pygithub3.tests.utils.core import TestCase +from pygithub3.services.git_data import (Blobs, Commits, References, Tags, + Trees) +from pygithub3.resources.base import json +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 TestBlobsService(TestCase): + def setUp(self): + self.service = Blobs(user='octocat', repo='repo') + + def test_GET(self, reqm): + reqm.return_value = mock_response() + self.service.get('abc123') + self.assertEqual(reqm.call_args[0], + ('get', _('repos/octocat/repo/git/blobs/abc123'))) + + def test_CREATE(self, reqm): + reqm.return_value = mock_response('post') + self.service.create({'content': 'hello, friends', 'encoding': + 'utf-8'}) + self.assertEqual(reqm.call_args[0], + ('post', _('repos/octocat/repo/git/blobs'))) + + +@patch.object(requests.sessions.Session, 'request') +class TestCommitsService(TestCase): + def setUp(self): + self.service = Commits(user='octocat', repo='repo') + + def test_GET(self, reqm): + reqm.return_value = mock_response() + self.service.get('abc123') + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/octocat/repo/git/commits/abc123')) + ) + + def test_CREATE(self, reqm): + reqm.return_value = mock_response('post') + self.service.create({ + 'message': 'hello', + 'tree': 'abc123', + 'parents': ['mom', 'dad'], + }) + self.assertEqual( + reqm.call_args[0], + ('post', _('repos/octocat/repo/git/commits')) + ) + + +@patch.object(requests.sessions.Session, 'request') +class TestReferencesService(TestCase): + def setUp(self): + self.service = References(user='user', repo='repo') + + def test_GET(self, reqm): + reqm.return_value = mock_response() + self.service.get('heads/fnord') + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/git/refs/heads/fnord')) + ) + + 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/git/refs')) + ) + + def test_create(self, reqm): + reqm.return_value = mock_response('post') + self.service.create({'sha': 'hello', 'ref': 'something'}) + self.assertEqual( + reqm.call_args[0], + ('post', _('repos/user/repo/git/refs')) + ) + + def test_update(self, reqm): + reqm.return_value = mock_response('patch') + self.service.update('master', {'sha': 'abc123'}) + self.assertEqual( + reqm.call_args[0], + ('patch', _('repos/user/repo/git/refs/master')) + ) + + def test_delete(self, reqm): + reqm.return_value = mock_response('delete') + self.service.delete('branch') + self.assertEqual( + reqm.call_args[0], + ('delete', _('repos/user/repo/git/refs/branch')) + ) + +@patch.object(requests.sessions.Session, 'request') +class TestTagsService(TestCase): + def setUp(self): + self.service = Tags(user='user', repo='repo') + + def test_GET(self, reqm): + reqm.return_value = mock_response() + self.service.get('abc123') + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/git/tags/abc123')) + ) + + def test_CREATE(self, reqm): + reqm.return_value = mock_response('post') + self.service.create({'tag': 'v1.2.3', 'message': 'a tag', + 'object': 'abc123', 'type': 'commit'}) + self.assertEqual( + reqm.call_args[0], + ('post', _('repos/user/repo/git/tags')) + ) + + +@patch.object(requests.sessions.Session, 'request') +class TestTreesService(TestCase): + def setUp(self): + self.service = Trees(user='user', repo='repo') + + def test_GET(self, reqm): + reqm.return_value = mock_response() + self.service.get('abc123') + self.assertEqual( + reqm.call_args[0], + ('get', _('repos/user/repo/git/trees/abc123')) + ) + + def test_CREATE(self, reqm): + reqm.return_value = mock_response('post') + self.service.create({ + 'tree': [ + {'path': 'foo.txt', 'mode': '100644', 'type': 'blob', + 'sha': 'abc123'}, + ], + }) + self.assertEqual( + reqm.call_args[0], + ('post', _('repos/user/repo/git/trees')) + ) diff --git a/pygithub3/tests/services/test_pull_requests.py b/pygithub3/tests/services/test_pull_requests.py new file mode 100644 index 0000000..4a9fd41 --- /dev/null +++ b/pygithub3/tests/services/test_pull_requests.py @@ -0,0 +1,190 @@ +#!/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.requests.base import ValidationError, json +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_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')) + ) + + 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_UPDATE(self, reqm): + reqm.return_value = mock_response(200) + data = { + 'body': 'something completely different', + } + self.service.update(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 49ea2b4..f90b5d3 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 |
