Skip to content

Commit db23c0d

Browse files
committed
Merge branch 'ChristopherMacGown-pull_requests'
* ChristopherMacGown-pull_requests: Updated pull request versionadded attribute to 0.5.0. Moved versionadded directive to pull request class. Added pull request examples. Updated authors. Use pull request title for repr string. Minor PEP-8 fix. Document PullRequests methods. Added mergeable pull request attribute. body is optional parameter for opening pull request. Use project as parameter name to match rest of package. Fixed discussion/comments handling for pull requests. Updated pull request date handling. Added simple ISO-8601 parsing for pull request usage. Fixed the datetime format for the DateAttributes Adds support for Github API pull requests Conflicts: AUTHORS github2/client.py github2/core.py
2 parents 715d03b + a33fb2e commit db23c0d

6 files changed

Lines changed: 154 additions & 0 deletions

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ Surajram Kumarave <root@surajram.com>
2424
broderboy <timothy.broder@gmail.com>
2525
Patryk Zawadzki <patrys@pld-linux.org>
2626
Michael Basnight <mbasnight@gmail.com>
27+
Christopher MacGown <ignoti+github@gmail.com>

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ API documentation
99
organizations
1010
teams
1111
issues
12+
pull_requests
1213
network
1314
repos
1415
commit

doc/api/pull_requests.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Pull requests
2+
=============
3+
4+
.. py:currentmodule:: github2.pull_requests
5+
6+
.. autoclass:: PullRequest(type)
7+
8+
.. autoclass:: PullRequests(type)
9+
10+
Examples
11+
--------
12+
13+
Listing pull requests
14+
'''''''''''''''''''''
15+
16+
>>> results = github.pull_requests.list("ask/python-github2")
17+
18+
View a pull request
19+
'''''''''''''''''''
20+
21+
>>> request = github.pull_requests.show("ask/python-github2", 28)
22+
>>> pull.body
23+
'This implements the github API pull requests functionality. '
24+
25+
Open pull request
26+
'''''''''''''''''
27+
28+
To open a new pull request::
29+
30+
>>> pull = github.pull_requests.create("ask/python-github2", "master",
31+
... "JNRowe:my_new_branch",
32+
... title="Fancy features")
33+
>>> pull.number
34+
4
35+
36+
To attach code to an existing issue::
37+
38+
>>> pull = github.pull_requests.create("ask/python-github2", "master",
39+
... "JNRowe:my_new_branch",
40+
... issue=4)

github2/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from github2.commits import Commits
66
from github2.organizations import Organizations
77
from github2.teams import Teams
8+
from github2.pull_requests import PullRequests
89

910

1011
class Github(object):
@@ -53,6 +54,7 @@ def __init__(self, username=None, api_token=None, debug=False,
5354
self.commits = Commits(self.request)
5455
self.organizations = Organizations(self.request)
5556
self.teams = Teams(self.request)
57+
self.pull_requests = PullRequests(self.request)
5658

5759
def project_for_user_repo(self, user, repo):
5860
"""Return Github identifier for a user's repository

github2/core.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ def userdate_to_datetime(user_date):
7474
return strptime(user_date, '%Y-%m-%dT%H:%M:%SZ')
7575

7676

77+
def isodate_to_datetime(iso_date):
78+
"""Convert commit date string to Python datetime
79+
80+
:param str github_date: date string to parse
81+
"""
82+
date_without_tz = iso_date[:-1]
83+
return datetime.strptime(date_without_tz, COMMIT_DATE_FORMAT)
84+
85+
86+
def datetime_to_isodate(datetime_):
87+
"""Convert Python datetime to Github date string
88+
89+
:param str datetime_: datetime object to convert
90+
"""
91+
return "%s%z" % datetime_.isoformat()
92+
93+
7794
class GithubCommand(object):
7895

7996
def __init__(self, request):
@@ -161,6 +178,10 @@ class DateAttribute(Attribute):
161178
"user": {
162179
"to" : userdate_to_datetime,
163180
"from": datetime_to_ghdate,
181+
},
182+
"iso": {
183+
"to": isodate_to_datetime,
184+
"from": datetime_to_isodate,
164185
}
165186
}
166187

github2/pull_requests.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)