|
| 1 | +from pygithub3.exceptions import BadRequest, NotFound |
| 2 | +from pygithub3.services.base import Service, MimeTypeMixin |
| 3 | +from .comments import Comments |
| 4 | + |
| 5 | + |
| 6 | +class PullRequests(Service, MimeTypeMixin): |
| 7 | + """Consume `Pull Request API <http://developer.github.com/v3/pulls/>`_""" |
| 8 | + |
| 9 | + def __init__(self, **config): |
| 10 | + self.comments = Comments(**config) |
| 11 | + super(PullRequests, self).__init__(**config) |
| 12 | + |
| 13 | + def list(self, user=None, repo=None): |
| 14 | + """List all of the pull requests for a repo |
| 15 | +
|
| 16 | + :param str user: Username |
| 17 | + :param str repo: Repository |
| 18 | +
|
| 19 | + """ |
| 20 | + return self._get_result( |
| 21 | + self.make_request('pull_requests.list', user=user, repo=repo) |
| 22 | + ) |
| 23 | + |
| 24 | + def get(self, number, user=None, repo=None): |
| 25 | + """Get a single pull request |
| 26 | +
|
| 27 | + :param str number: The number of the pull request to get |
| 28 | + :param str user: Username |
| 29 | + :param str repo: Repository |
| 30 | +
|
| 31 | + """ |
| 32 | + return self._get( |
| 33 | + self.make_request('pull_requests.get', number=number, user=user, |
| 34 | + repo=repo) |
| 35 | + ) |
| 36 | + |
| 37 | + def create(self, body, user=None, repo=None): |
| 38 | + """Create a pull request |
| 39 | +
|
| 40 | + :param dict body: Data for the new pull request |
| 41 | + :param str user: Username |
| 42 | + :param str repo: Repository |
| 43 | +
|
| 44 | + """ |
| 45 | + return self._post( |
| 46 | + self.make_request('pull_requests.create', body=body, user=user, |
| 47 | + repo=repo) |
| 48 | + ) |
| 49 | + |
| 50 | + def update(self, number, body, user=None, repo=None): |
| 51 | + """Update a pull request |
| 52 | +
|
| 53 | + :param str number: The number of the the pull request to update |
| 54 | + :param dict body: The data to update the pull request with |
| 55 | + :param str user: Username |
| 56 | + :param str repo: Repository |
| 57 | +
|
| 58 | + """ |
| 59 | + return self._patch( |
| 60 | + self.make_request('pull_requests.update', number=number, |
| 61 | + body=body, user=user, repo=repo) |
| 62 | + ) |
| 63 | + |
| 64 | + def list_commits(self, number, user=None, repo=None): |
| 65 | + """List the commits for a pull request |
| 66 | +
|
| 67 | + :param str number: The number of the pull request to list commits for |
| 68 | + :param str user: Username |
| 69 | + :param str repo: Repository |
| 70 | +
|
| 71 | + """ |
| 72 | + return self._get_result( |
| 73 | + self.make_request('pull_requests.list_commits', number=number, |
| 74 | + user=user, repo=repo) |
| 75 | + ) |
| 76 | + |
| 77 | + def list_files(self, number, user=None, repo=None): |
| 78 | + """List the files for a pull request |
| 79 | +
|
| 80 | + :param str number: The number of the pull request to list files for |
| 81 | + :param str user: Username |
| 82 | + :param str repo: Repository |
| 83 | +
|
| 84 | + """ |
| 85 | + return self._get_result( |
| 86 | + self.make_request('pull_requests.list_files', number=number, |
| 87 | + user=user, repo=repo) |
| 88 | + ) |
| 89 | + |
| 90 | + def merge_status(self, number, user=None, repo=None): |
| 91 | + """Gets whether a pull request has been merged or not. |
| 92 | +
|
| 93 | + :param str number: The pull request to check |
| 94 | + :param str user: Username |
| 95 | + :param str repo: Repository |
| 96 | +
|
| 97 | + """ |
| 98 | + # for this to work with a proper Resource, we would need to pass the |
| 99 | + # response's status code to the Resource constructor, and that's kind |
| 100 | + # of scary |
| 101 | + try: |
| 102 | + resp = self._client.get( |
| 103 | + self.make_request('pull_requests.merge_status', number=number, |
| 104 | + user=user, repo=repo) |
| 105 | + ) |
| 106 | + except NotFound: |
| 107 | + return False |
| 108 | + code = resp.status_code |
| 109 | + if code == 204: |
| 110 | + return True |
| 111 | + # TODO: more flexible way to return arbitrary objects based on |
| 112 | + # response. Probably something on Request |
| 113 | + raise BadRequest('got code %s: %s' % (code, resp.content)) |
| 114 | + # again, I'm sorry. |
| 115 | + |
| 116 | + def merge(self, number, message='', user=None, repo=None): |
| 117 | + """Merge a pull request. |
| 118 | +
|
| 119 | + :param str number: The pull request to merge |
| 120 | + :param str user: Username |
| 121 | + :param str repo: Repository |
| 122 | +
|
| 123 | + """ |
| 124 | + # so, the API docs don't actually say what the status code will be in |
| 125 | + # the case of a merge failure. I hope it's not a 404. |
| 126 | + return self._put( |
| 127 | + self.make_request('pull_requests.merge', number=number, |
| 128 | + message=message, user=user, repo=repo) |
| 129 | + ) |
0 commit comments