aboutsummaryrefslogtreecommitdiffstats
path: root/pygithub3/services/repos/commits.py
blob: 4ba1ff132beb4164937d76930955dba35dc7a9e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from . import Service, MimeTypeMixin


class Commits(Service, MimeTypeMixin):
    """ Consume `Commits API
    <http://developer.github.com/v3/repos/commits>`_

    .. note::
        This service support :ref:`mimetypes-section` configuration
    """

    def list(self, user=None, repo=None, sha=None, path=None):
        """ Get repository's commits

        :param str user: Username
        :param str repo: Repository
        :param str sha: Sha or branch to start listing commits from
        :param str path: Only commits containing this file path will be returned
        :returns: A :doc:`result`

        .. note::
            Remember :ref:`config precedence`

        .. warning::
            Usually a repository has thousand of commits, so be careful when
            consume the result. You should filter with ``sha`` or directly
            clone the repository

        ::

            commits_service.list(user='octocat', repo='oct_repo')
            commits_service.list(user='octocat', repo='oct_repo', sha='dev')
            commits_service.list(user='django', repo='django', sha='master',
                path='django/db/utils.py')
        """
        request = self.make_request('repos.commits.list', user=user, repo=repo)
        params = {}
        params.update(sha and {'sha': sha} or {})
        params.update(path and {'path': path} or {})
        return self._get_normal_result(request, **params)

    def get(self, sha, user=None, repo=None):
        """ Get a single commit

        :param str sha: Commit's sha
        :param str user: Username
        :param str repo: Repository

        .. note::
            Remember :ref:`config precedence`
        """
        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):
        """ Get commit's comments

        :param str sha: Commit's sha
        :param str user: Username
        :param str repo: Repository
        :returns: A :doc:`result`

        .. note::
            Remember :ref:`config precedence`

        If you call it without ``sha``, get all commit's comments of a
        repository
        ::

            commits_service.list_comments('6dcb09', user='octocat',
                repo='oct_repo')
            commits_service.list_comments(user='octocat', repo='oct_repo')
        """
        request = self.make_request('repos.commits.list_comments',
            sha=sha, user=user, repo=repo)
        return self._get_result(request, **self._get_mimetype_as_header())

    def create_comment(self, data, sha, user=None, repo=None):
        """ Create a commit comment

        :param dict data: Input. See `github commits doc`_
        :param str sha: Commit's sha
        :param str user: Username
        :param str repo: Repository

        .. note::
            Remember :ref:`config precedence`

        .. warning::
            You must be authenticated

        ::

            data = {
                "body": "Nice change",
                "commit_id": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
                "line": 1,
                "path": "file1.txt",
                "position": 4
            }
            commits_service.create_comment(data, '6dcb09', user='octocat',
                repo='oct_repo')
        """
        request = self.make_request('repos.commits.create_comment',
            sha=sha, user=user, repo=repo, body=data)
        return self._post(request, **self._get_mimetype_as_header())

    def get_comment(self, cid, user=None, repo=None):
        """ Get a single commit comment

        :param int cid: Commit comment id
        :param str user: Username
        :param str repo: Repository

        .. note::
            Remember :ref:`config precedence`
        """
        request = self.make_request('repos.commits.get_comment',
            comment_id=cid, user=user, repo=repo)
        return self._get(request, **self._get_mimetype_as_header())

    def update_comment(self, data, cid, user=None, repo=None):
        """ Update a single commit comment

        :param dict data: Input. See `github commits doc`_
        :param int cid: Commit comment id
        :param str user: Username
        :param str repo: Repository

        .. note::
            Remember :ref:`config precedence`

        .. warning::
            You must be authenticated

        ::

            commits_service.update_comment(dict(body='nice change'), 42,
                user='octocat', repo='oct_repo')
        """
        request = self.make_request('repos.commits.update_comment',
            comment_id=cid, user=user, repo=repo, body=data)
        return self._patch(request, **self._get_mimetype_as_header())

    def compare(self, base, head, user=None, repo=None):
        """ Compare two commits

        :param str base: Base commit sha
        :param str head: Head commit sha
        :param str user: Username
        :param str repo: Repository

        .. note::
            Remember :ref:`config precedence`

        ::

            commits_service.compare('6dcb09', 'master', user='octocat',
                repo='oct_repo')
        """
        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):
        """ Delete a single commit comment

        :param int cid: Commit comment id
        :param str user: Username
        :param str repo: Repository

        .. note::
            Remember :ref:`config precedence`
        """
        request = self.make_request('repos.commits.delete_comment',
            comment_id=cid, user=user, repo=repo)
        self._delete(request)