aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pygithub3/requests/repos/commits.py64
-rw-r--r--pygithub3/resources/repos.py68
-rw-r--r--pygithub3/services/repos.py43
-rw-r--r--pygithub3/tests/services/test_repos.py67
4 files changed, 234 insertions, 8 deletions
diff --git a/pygithub3/requests/repos/commits.py b/pygithub3/requests/repos/commits.py
new file mode 100644
index 0000000..49df2a1
--- /dev/null
+++ b/pygithub3/requests/repos/commits.py
@@ -0,0 +1,64 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+from . import Request
+from pygithub3.resources.repos import Commit, GitCommit, Comment, Diff
+
+
+class List(Request):
+
+ uri = 'repos/{user}/{repo}/commits'
+ resource = GitCommit
+
+
+class Get(Request):
+
+ uri = 'repos/{user}/{repo}/commits/{sha}'
+ resource = Commit
+
+
+class List_comments(Request):
+
+ uri = 'repos/{user}/{repo}/comments'
+ resource = Comment
+
+ def clean_uri(self):
+ if self.sha:
+ return 'repos/{user}/{repo}/commits/{sha}/comments'
+
+
+class Create_comment(Request):
+
+ uri = 'repos/{user}/{repo}/commits/{sha}/comments'
+ resource = Comment
+ body_schema = {
+ 'schema': ('body', 'commit_id', 'line', 'path', 'position'),
+ 'required': ('body', 'commit_id', 'line', 'path', 'position'),
+ }
+
+
+class Get_comment(Request):
+
+ uri = 'repos/{user}/{repo}/comments/{comment_id}'
+ resource = Comment
+
+
+class Update_comment(Request):
+
+ uri = 'repos/{user}/{repo}/comments/{comment_id}'
+ resource = Comment
+ body_schema = {
+ 'schema': ('body', ),
+ 'required': ('body', ),
+ }
+
+
+class Compare(Request):
+
+ uri = 'repos/{user}/{repo}/compare/{base}...{head}'
+ resource = Diff
+
+
+class Delete_comment(Request):
+
+ uri = 'repos/{user}/{repo}/comments/{comment_id}'
diff --git a/pygithub3/resources/repos.py b/pygithub3/resources/repos.py
index 609ede4..de55b72 100644
--- a/pygithub3/resources/repos.py
+++ b/pygithub3/resources/repos.py
@@ -5,8 +5,6 @@ from .base import Resource
from .users import User
from .orgs import Org
-__all__ = ('Repo', )
-
class Repo(Resource):
@@ -24,17 +22,73 @@ class Team(Resource):
return '<Team (%s)>' % getattr(self, 'name', '')
+class Author(Resource):
+
+ _dates = ('date')
+
+ def __str__(self):
+ return '<Author (%s)>' % getattr(self, 'name', '')
+
+
+class Committer(Resource):
+
+ _dates = ('date')
+
+ def __str__(self):
+ return '<Committer (%s)>' % getattr(self, 'name', '')
+
+
+class GitCommit(Resource):
+
+ _maps = {'author': Author, 'committer': Committer, 'tree': 'self'}
+ _collection_maps = {'parents': 'self'}
+
+ def __str__(self):
+ return '<GitCommit (%s:%s)>' % (getattr(self, 'sha', ''),
+ getattr(self, 'message', ''))
+
+
+class Stats(Resource):
+ pass
+
+
+class File(Resource):
+
+ def __str__(self):
+ return '<File (%s)>' % getattr(self, 'filename', '')
+
+
class Commit(Resource):
+ _maps = {'commit': GitCommit, 'author': User, 'committer': User,
+ 'stats': Stats}
+ _collection_maps = {'parents': GitCommit, 'files': File}
+
+ def __str__(self):
+ return '<Commit (%s)>' % getattr(self, 'author', '')
+
+
+class Comment(Resource):
+
+ _dates = ('created_at', 'updated_at')
+ _maps = {'user': User}
+
+ def __str__(self):
+ return '<Comment (%s)>' % getattr(self, 'user', '')
+
+
+class Diff(Resource):
+
+ _maps = {'base_commit': Commit}
+ _collection_maps = {'commits': Commit, 'files': File}
+
def __str__(self):
- return '<Commit (%s:%s)>' % (
- getattr(self, 'sha', ''),
- getattr(self, 'message', ''))
+ return '<Diff (%s)>' % getattr(self, 'status', '')
class Tag(Resource):
- _maps = {'commit': Commit}
+ _maps = {'commit': GitCommit}
def __str__(self):
return '<Tag (%s)>' % getattr(self, 'name', '')
@@ -42,7 +96,7 @@ class Tag(Resource):
class Branch(Resource):
- _maps = {'commit': Commit}
+ _maps = {'commit': GitCommit}
def __str__(self):
return '<Branch (%s)>' % getattr(self, 'name', '')
diff --git a/pygithub3/services/repos.py b/pygithub3/services/repos.py
index 42a5788..bd9cdaa 100644
--- a/pygithub3/services/repos.py
+++ b/pygithub3/services/repos.py
@@ -4,6 +4,49 @@
from .base import Service
+class Commits(Service):
+
+ """ TODO: Pagination structure differs from usual
+ def list(self, user=None, repo=None, sha='', path=''):
+ request = self.make_request('repos.commits.list', user=user, repo=repo)
+ return self._get_result(request, sha=sha, path=path)
+ """
+
+ def get(self, sha, user=None, repo=None):
+ request = self.make_request('repos.commits.get',
+ sha=sha, user=user, repo=repo)
+ return self._get(request)
+
+ def list_comments(self, sha=None, user=None, repo=None):
+ request = self.make_request('repos.commits.list_comments',
+ sha=sha, user=user, repo=repo)
+ return self._get_result(request)
+
+ def create_comment(self, data, sha, user=None, repo=None):
+ request = self.make_request('repos.commits.create_comment',
+ sha=sha, user=user, repo=repo, body=data)
+ return self._post(request)
+
+ def get_comment(self, cid, user=None, repo=None):
+ request = self.make_request('repos.commits.get_comment',
+ comment_id=cid, user=user, repo=repo)
+ return self._get(request)
+
+ def update_comment(self, data, cid, user=None, repo=None):
+ request = self.make_request('repos.commits.update_comment',
+ comment_id=cid, user=user, repo=repo, body=data)
+ return self._patch(request)
+
+ def compare(self, base, head, user=None, repo=None):
+ request = self.make_request('repos.commits.compare',
+ base=base, head=head, user=user, repo=repo)
+ return self._get(request)
+
+ def delete_comment(self, cid, user=None, repo=None):
+ request = self.make_request('repos.commits.delete_comment',
+ comment_id=cid, user=user, repo=repo)
+ self._delete(request)
+
class Collaborator(Service):
def list(self, user=None, repo=None):
diff --git a/pygithub3/tests/services/test_repos.py b/pygithub3/tests/services/test_repos.py
index 93dfd5c..f8414d5 100644
--- a/pygithub3/tests/services/test_repos.py
+++ b/pygithub3/tests/services/test_repos.py
@@ -6,7 +6,7 @@ from unittest import TestCase
import requests
from mock import patch, Mock
-from pygithub3.services.repos import Repo, Collaborator
+from pygithub3.services.repos import Repo, Collaborator, Commits
from pygithub3.resources.base import json
from pygithub3.tests.utils.base import mock_response, mock_response_result
from pygithub3.tests.utils.services import _, mock_json
@@ -170,3 +170,68 @@ class TestCollaboratorsService(TestCase):
self.cs.delete('user')
self.assertEqual(request_method.call_args[0],
('delete', _('repos/octocat/oc_repo/collaborators/user')))
+
+
+@patch.object(requests.sessions.Session, 'request')
+class TestCommitsService(TestCase):
+
+ def setUp(self):
+ self.cs = Commits(user='oct', repo='re_oct')
+
+ """
+ def test_LIST(self, request_method):
+ request_method.return_value = mock_response_result()
+ self.cs.list().all()
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('repos/oct/re_oct/commits')))
+ """
+
+ def test_GET(self, request_method):
+ request_method.return_value = mock_response()
+ self.cs.get('e3bc')
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('repos/oct/re_oct/commits/e3bc')))
+
+ def test_LIST_comments(self, request_method):
+ request_method.return_value = mock_response_result()
+ self.cs.list_comments().all()
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('repos/oct/re_oct/comments')))
+
+ def test_LIST_comments_for_commit(self, request_method):
+ request_method.return_value = mock_response_result()
+ self.cs.list_comments(sha='e3bc').all()
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('repos/oct/re_oct/commits/e3bc/comments')))
+
+ def test_CREATE_comment(self, request_method):
+ request_method.return_value = mock_response('post')
+ data = dict(body='some', commit_id='e2bc',
+ line=1, path='some.txt', position=1)
+ self.cs.create_comment(data, 'e3bc')
+ self.assertEqual(request_method.call_args[0],
+ ('post', _('repos/oct/re_oct/commits/e3bc/comments')))
+
+ def test_GET_comment(self, request_method):
+ request_method.return_value = mock_response()
+ self.cs.get_comment(1)
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('repos/oct/re_oct/comments/1')))
+
+ def test_UPDATE_comment(self, request_method):
+ request_method.return_value = mock_response('patch')
+ self.cs.update_comment({'body': 'changed'}, 1)
+ self.assertEqual(request_method.call_args[0],
+ ('patch', _('repos/oct/re_oct/comments/1')))
+
+ def test_COMPARE(self, request_method):
+ request_method.return_value = mock_response()
+ self.cs.compare('develop', 'master')
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('repos/oct/re_oct/compare/develop...master')))
+
+ def test_DELETE_comment(self, request_method):
+ request_method.return_value = mock_response('delete')
+ self.cs.delete_comment(1)
+ self.assertEqual(request_method.call_args[0],
+ ('delete', _('repos/oct/re_oct/comments/1')))