Skip to content

Commit f0937a0

Browse files
committed
Support for Statuses API.
Closes sigmavirus24#11. /cc @ajwlucas Tests coming very soon.
1 parent bfec508 commit f0937a0

3 files changed

Lines changed: 96 additions & 15 deletions

File tree

‎docs/repos.rst‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,27 @@ Repository Objects
3232
---------
3333

3434
.. autoclass:: Branch
35-
:inherited-members:
35+
:members:
3636

3737
---------
3838

3939
.. autoclass:: Contents
40-
:inherited-members:
40+
:members:
4141

4242
---------
4343

4444
.. autoclass:: Download
45-
:inherited-members:
45+
:members:
4646

4747
---------
4848

4949
.. autoclass:: Hook
50-
:inherited-members:
50+
:members:
5151

5252
---------
5353

5454
.. autoclass:: RepoTag
55-
:inherited-members:
55+
:members:
5656

5757
---------
5858

@@ -65,9 +65,14 @@ about `comments <http://developer.github.com/v3/repos/comments/>`_.
6565
---------
6666

6767
.. autoclass:: RepoCommit
68-
:inherited-members:
68+
:members:
6969

7070
---------
7171

7272
.. autoclass:: Comparison
73-
:inherited-members:
73+
:members:
74+
75+
---------
76+
77+
.. autoclass:: Status
78+
:members:

‎github3/models.py‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def to_json(self):
2727
"""Return the json representing this object."""
2828
return self._json_data
2929

30+
def _strptime(self, time_str):
31+
"""Converts an ISO 8601 formatted string into a datetime object."""
32+
if time_str:
33+
return datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%SZ')
34+
else:
35+
return None
36+
3037
@classmethod
3138
def from_json(cls, json):
3239
"""Return an instance of ``cls`` formed from ``json``."""
@@ -46,7 +53,6 @@ def __init__(self, json, ses=None):
4653
ses = session()
4754
self._session = ses
4855
self._github_url = 'https://api.github.com'
49-
self._time_format = '%Y-%m-%dT%H:%M:%SZ'
5056
self._remaining = 5000
5157
self._rel_reg = compile(r'<(https://[0-9a-zA-Z\./\?=&]+)>; '
5258
'rel="(\w+)"')
@@ -100,13 +106,6 @@ def _put(self, url, **kwargs):
100106
req = self._session.put(url, **kwargs)
101107
return req
102108

103-
def _strptime(self, time_str):
104-
"""Converts an ISO 8601 formatted string into a datetime object."""
105-
if time_str:
106-
return datetime.strptime(time_str, self._time_format)
107-
else:
108-
return None
109-
110109
def _build_url(self, *args, **kwargs):
111110
"""Builds a new API url from scratch."""
112111
parts = [kwargs.get('base_url') or self._github_url]

‎github3/repos.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,24 @@ def create_ref(self, ref, sha):
521521
json = self._json(self._post(url, data), 201)
522522
return Reference(json, self) if json else None
523523

524+
@GitHubCore.requires_auth
525+
def create_status(self, sha, state, target_url='', description=''):
526+
"""Create a status object on a commit.
527+
528+
:param str sha: (required), SHA of the commit to create the status on
529+
:param str state: (required), state of the test; only the following
530+
are accepted: 'pending', 'success', 'error', 'failure'
531+
:param str target_url: (optional), URL to associate with this status.
532+
:param str description: (optional), short description of the status
533+
"""
534+
json = {}
535+
if sha and state:
536+
data = dumps({'state': state, 'target_url': target_url,
537+
'description': description})
538+
url = self._build_url('statuses', sha, base_url=self._api)
539+
json = self._json(self._post(url, data=data), 201)
540+
return Status(json) if json else None
541+
524542
@GitHubCore.requires_auth
525543
def create_tag(self, tag, message, sha, obj_type, tagger,
526544
lightweight=False):
@@ -1086,6 +1104,18 @@ def list_refs(self, subspace=''):
10861104
json = self._json(self._get(url), 200)
10871105
return [Reference(r, self) for r in json]
10881106

1107+
def list_statuses(self, sha):
1108+
"""List the statuses for a specific SHA.
1109+
1110+
:param str sha: SHA of the commit to list the statuses of
1111+
:returns: list of :class:`Status <Status>`
1112+
"""
1113+
json = []
1114+
if sha:
1115+
url = self._build_url('statuses', sha, base_url=self._api)
1116+
json = self._json(self._get(url), 200)
1117+
return [Status(s) for s in json]
1118+
10891119
def list_tags(self):
10901120
"""List tags on this repository.
10911121
@@ -1900,3 +1930,50 @@ def status(self):
19001930
def total_commits(self):
19011931
"""Number of commits difference in the comparison."""
19021932
return self._ttl_commits
1933+
1934+
1935+
class Status(GitHubObject):
1936+
"""The :class:`Status <Status>` object. This represents information from
1937+
the Repo Status API."""
1938+
def __init__(self, status):
1939+
super(Status, self).__init__(status)
1940+
self._created = self._strptime(status.get('created_at'))
1941+
self._creator = User(status.get('creator'))
1942+
self._desc = status.get('description')
1943+
self._id = status.get('id')
1944+
self._stat = status.get('state')
1945+
self._tgt = status.get('target_url')
1946+
self._updated = None
1947+
if status.get('updated_at'):
1948+
self._updated = self._strptime(status.get('updated_at'))
1949+
1950+
def __repr__(self):
1951+
return '<Status [{s.id}:{s.state}]>'.format(s=self)
1952+
1953+
@property
1954+
def created_at(self):
1955+
return self._created
1956+
1957+
@property
1958+
def creator(self):
1959+
return self._creator
1960+
1961+
@property
1962+
def description(self):
1963+
return self._desc
1964+
1965+
@property
1966+
def id(self):
1967+
return self._id
1968+
1969+
@property
1970+
def state(self):
1971+
return self._stat
1972+
1973+
@property
1974+
def target_url(self):
1975+
return self._tgt
1976+
1977+
@property
1978+
def updated_at(self):
1979+
return self._updated

0 commit comments

Comments
 (0)