|
| 1 | +from github2.core import BaseData, GithubCommand, Attribute, DateAttribute |
| 2 | + |
| 3 | + |
| 4 | +class PullRequest(BaseData): |
| 5 | + """Pull request encapsulation |
| 6 | +
|
| 7 | + .. versionadded:: 0.5.0 |
| 8 | + """ |
| 9 | + state = Attribute("The pull request state") |
| 10 | + base = Attribute("The base repo") |
| 11 | + head = Attribute("The head of the pull request") |
| 12 | + issue_user = Attribute("The user who created the pull request.") |
| 13 | + user = Attribute("The owner of the repo.") |
| 14 | + title = Attribute("The text of the pull request title.") |
| 15 | + body = Attribute("The text of the body.") |
| 16 | + position = Attribute("Floating point position of the pull request.") |
| 17 | + number = Attribute("Number of this request.") |
| 18 | + votes = Attribute("Number of votes for this request.") |
| 19 | + comments = Attribute("Number of comments made on this request.") |
| 20 | + diff_url = Attribute("The URL to the unified diff.") |
| 21 | + patch_url = Attribute("The URL to the downloadable patch.") |
| 22 | + labels = Attribute("A list of labels attached to the pull request.") |
| 23 | + html_url = Attribute("The URL to the pull request.") |
| 24 | + issue_created_at = DateAttribute("The date the issue for this pull request was opened.", |
| 25 | + format='iso') |
| 26 | + issue_updated_at = DateAttribute("The date the issue for this pull request was last updated.", |
| 27 | + format='iso') |
| 28 | + created_at = DateAttribute("The date when this pull request was created.", |
| 29 | + format='iso') |
| 30 | + updated_at = DateAttribute("The date when this pull request was last updated.", |
| 31 | + format='iso') |
| 32 | + closed_at = DateAttribute("The date when this pull request was closed", |
| 33 | + format='iso') |
| 34 | + discussion = Attribute("Discussion thread for the pull request.") |
| 35 | + mergeable = Attribute("Whether the pull request can be merge cleanly") |
| 36 | + |
| 37 | + def __repr__(self): |
| 38 | + return "<PullRequest: %s>" % self.title.encode('utf-8') |
| 39 | + |
| 40 | + |
| 41 | +class PullRequests(GithubCommand): |
| 42 | + """Operations on pull requests |
| 43 | +
|
| 44 | + .. versionadded:: 0.5.0 |
| 45 | + """ |
| 46 | + domain = "pulls" |
| 47 | + |
| 48 | + def create(self, project, base, head, title=None, body=None, issue=None): |
| 49 | + """Create a new pull request |
| 50 | +
|
| 51 | + Pull requests can be created from scratch, or attached to an existing |
| 52 | + issue. If an ``issue`` parameter is supplied the pull request is |
| 53 | + attached to that issue, else a new pull request is created. |
| 54 | +
|
| 55 | + :param str project: Github project |
| 56 | + :param str base: branch changes should be pulled into |
| 57 | + :param str head: branch of the changes to be pulled |
| 58 | + :param str title: title for pull request |
| 59 | + :param str body: optional body for pull request |
| 60 | + :param str issue: existing issue to attach pull request to |
| 61 | + """ |
| 62 | + post_data = {"base": base, "head": head} |
| 63 | + if issue: |
| 64 | + post_data["issue"] = issue |
| 65 | + elif title: |
| 66 | + post_data["title"] = title |
| 67 | + if body: |
| 68 | + post_data["body"] = body |
| 69 | + pull_request_data = [("pull[%s]" % k, v) for k, v in post_data.items()] |
| 70 | + return self.get_value(project, post_data=dict(pull_request_data), |
| 71 | + filter="pull", datatype=PullRequest) |
| 72 | + |
| 73 | + def show(self, project, number): |
| 74 | + """Show a single pull request |
| 75 | +
|
| 76 | + :param str project: Github project |
| 77 | + :param int number: pull request number in the Github database |
| 78 | + """ |
| 79 | + return self.get_value(project, str(number), filter="pull", |
| 80 | + datatype=PullRequest) |
| 81 | + |
| 82 | + def list(self, project, state="open"): |
| 83 | + """List all pull requests for a project |
| 84 | +
|
| 85 | + :param str project: Github project |
| 86 | + :param str state: can be either ``open`` or ``closed`` |
| 87 | + """ |
| 88 | + return self.get_values(project, state, filter="pulls", |
| 89 | + datatype=PullRequest) |
0 commit comments