Skip to content

Commit d65a3d9

Browse files
omgjlkwestphahl
authored andcommitted
Add testing for checks objects
Many bugs were fixed after trying to run sample data through the Checks classes. GitHub is sending a really minimal set of nested data which is requiring new place holder types for PullRequests and Apps. CustomHeaders were needed to be passed in all session calls and accounted for in the tests as well. Also update versionadded strings
1 parent 5df6e16 commit d65a3d9

File tree

6 files changed

+494
-22
lines changed

6 files changed

+494
-22
lines changed

src/github3/checks.py

Lines changed: 145 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,131 @@
44

55
from json import dumps
66

7-
from . import apps
87
from . import decorators
98
from . import models
109

1110

11+
class CheckPullRequest(models.GitHubCore):
12+
"""Representation of a Pull Request returned in Checks APIs.
13+
14+
.. versionadded:: 1.3.0
15+
16+
.. note::
17+
18+
Refreshing this object returns a :class:`~github3.pulls.PullRequest`.
19+
20+
This object has the following attributes:
21+
22+
.. attribute:: id
23+
24+
The unique id of this pull request across all of GitHub.
25+
26+
.. attribute:: number
27+
28+
The number of this pull request on its repository.
29+
30+
.. attribute:: head
31+
32+
A dict of minimal head information retrieved from the Check data
33+
representing the source of the pull request
34+
35+
.. attribute:: base
36+
37+
A dict of minimal base information retrieved from the Check data
38+
representing the pull request destination.
39+
"""
40+
41+
def _update_attributes(self, pull):
42+
self.id = pull['id']
43+
self.number = pull['number']
44+
self.base = pull['base']
45+
self.head = pull['head']
46+
self._api = self.url = pull['url']
47+
48+
def _repr(self):
49+
return '<CheckPullRequest [#{0}]>'.format(self.number)
50+
51+
def to_pull(self):
52+
"""Retrieve a full PullRequest object for this CheckPullRequest.
53+
54+
:returns:
55+
The full information about this pull request.
56+
:rtype:
57+
:class:`~github3.pulls.PullRequest`
58+
"""
59+
from . import pulls
60+
json = self._json(self._get(self.url), 200)
61+
return self._instance_or_null(pulls.PullRequest, json)
62+
63+
refresh = to_pull
64+
65+
66+
class CheckApp(models.GitHubCore):
67+
"""Representation of an App returned in Checks APIs.
68+
69+
.. versionadded:: 1.3.0
70+
71+
.. note::
72+
73+
Refreshing this object returns a :class:`~github3.apps.App`.
74+
75+
This object has the following attributes:
76+
77+
.. attribute:: description
78+
79+
The description of the App provided by the owner.
80+
81+
.. attribute:: external_url
82+
83+
The URL provided for the App by the owner.
84+
85+
.. attribute:: html_url
86+
87+
The HTML URL provided for the App by the owner.
88+
89+
.. attribute:: id
90+
91+
The unique identifier for the App. This is useful in cases where you
92+
may want to authenticate either as an App or as a specific
93+
installation of an App.
94+
95+
.. attribute:: name
96+
97+
The display name of the App that the user sees.
98+
99+
.. attribute:: owner
100+
101+
A dict of minimal user information retrieved from the Check data
102+
representing the app owner
103+
"""
104+
105+
def _update_attributes(self, app):
106+
self.description = app['description']
107+
self.external_url = app['external_url']
108+
self.html_url = app['html_url']
109+
self.id = app['id']
110+
self.name = app['name']
111+
self.owner = app['owner']
112+
113+
def _repr(self):
114+
return '<App ["{}" by {}]>'.format(self.name,
115+
str(self.owner['login']))
116+
117+
def to_app(self):
118+
"""Retrieve a full App object for this CheckApp.
119+
120+
:returns:
121+
The full information about this App.
122+
:rtype:
123+
:class:`~github3.apps.App`
124+
"""
125+
from . import apps
126+
json = self._json(self._get(self.url), 200)
127+
return self._instance_or_null(apps.App, json)
128+
129+
refresh = to_app
130+
131+
12132
class CheckSuite(models.GitHubCore):
13133
"""The :class:`CheckSuite <CheckSuite>` object.
14134
@@ -49,12 +169,17 @@ class CheckSuite(models.GitHubCore):
49169
.. attribute:: pull_requests
50170
51171
A list of representations of the pull requests the suite belongs to as
52-
:class:`~github3.pulls.ShortPullRequest`. This may be empty.
172+
:class:`~github3.checks.CheckPullRequest`. This may be empty.
53173
54174
.. attribute:: id
55175
56176
The unique GitHub assigned numerical id of this check suite.
57177
178+
.. attribute:: app
179+
180+
A :class:`~github3.checks.CheckApp` representing the App
181+
this suite belongs to.
182+
58183
.. CheckSuite Documentation:
59184
http://developer.github.com/v3/checks/suites/
60185
"""
@@ -66,21 +191,21 @@ class CheckSuite(models.GitHubCore):
66191

67192
def _update_attributes(self, suite):
68193
# Import here, because a toplevel import causes an import loop
69-
from . import pulls, repos
194+
from . import repos
70195
self._api = suite['url']
71-
# self.base = Base(pull['base'], self)
72196
self.status = suite['status']
73197
self.conclusion = suite['conclusion']
74198
self.head_branch = suite['head_branch']
75199
self.head_sha = suite['head_sha']
76200
self.before = suite['before']
77201
self.after = suite['after']
78-
pull_requests = suite.get('pull_requests', [])
202+
prs = suite.get('pull_requests', [])
79203
self.pull_requests = [
80-
pulls.ShortPullRequest(p, self) for p in pull_requests
204+
CheckPullRequest(p, self) for p in prs
81205
]
82206
self.repository = repos.ShortRepository(suite['repository'], self)
83207
self.id = suite['id']
208+
self.app = CheckApp(suite['app'], self)
84209

85210
def _repr(self):
86211
return '<{s.class_name} [{s.id}:{s.status}]>'.format(s=self)
@@ -108,13 +233,14 @@ def check_runs(self):
108233
:class:`~github3.checks.CheckRun`
109234
"""
110235
url = self._build_url('check-runs', base_url=self._api)
111-
return self._iter(-1, url, CheckRun)
236+
return self._iter(-1, url, CheckRun,
237+
headers=CheckSuite.CUSTOM_HEADERS)
112238

113239

114240
class CheckRun(models.GitHubCore):
115241
"""The :class:`CheckRun <CheckRun>` object.
116242
117-
.. versionadded:: 1.2.0
243+
.. versionadded:: 1.3.0
118244
119245
Please see GitHub's `CheckRun Documentation`_ for more information.
120246
@@ -148,8 +274,8 @@ class CheckRun(models.GitHubCore):
148274
149275
.. attribute:: pull_requests
150276
151-
A list of representations of the pull requests the check run belongs to
152-
as :class:`~github3.pulls.ShortPullRequest` (this may be empty).
277+
A list of representations of the pull requests the run belongs to as
278+
:class:`~github3.checks.CheckPullRequest`. This may be empty.
153279
154280
.. attribute:: id
155281
@@ -174,7 +300,7 @@ class CheckRun(models.GitHubCore):
174300
175301
.. attribute:: app
176302
177-
A :class:`~github3.apps.App` representing the App
303+
A :class:`~github3.checks.CheckApp` representing the App
178304
this run belongs to.
179305
180306
.. CheckRun Documentation:
@@ -187,23 +313,21 @@ class CheckRun(models.GitHubCore):
187313
}
188314

189315
def _update_attributes(self, run):
190-
# Import here, because a toplevel import causes an import loop
191-
from . import pulls
192316
self._api = run['url']
193317
self.html_url = run['html_url']
194318
self.status = run['status']
195319
self.conclusion = run['conclusion']
196-
self.started_at = self._strptime(run['created_at'])
320+
self.started_at = self._strptime(run['started_at'])
197321
self.completed_at = self._strptime(run['completed_at'])
198322
self.head_sha = run['head_sha']
199323
self.name = run['name']
200-
pull_requests = run.get('pull_requests', [])
324+
prs = run.get('pull_requests', [])
201325
self.pull_requests = [
202-
pulls.ShortPullRequest(p, self) for p in pull_requests
326+
CheckPullRequest(p, self) for p in prs
203327
]
204328
self.id = run['id']
205329
self.external_id = run['external_id']
206-
self.app = apps.App(run['app'], self)
330+
self.app = CheckApp(run['app'], self)
207331
self.check_suite = run['check_suite']['id']
208332
# self.output = CheckRunOutput(run['output'], self)
209333
self.output = run['output'] # TODO: turn into an object
@@ -259,7 +383,10 @@ def update(self, name=None, details_url=None, external_id=None,
259383
json = None
260384

261385
if data:
262-
json = self._json(self._patch(self._api, data=dumps(data)), 200)
386+
json = self._json(self._patch(
387+
self._api, data=dumps(data),
388+
headers=CheckSuite.CUSTOM_HEADERS), 200
389+
)
263390
if json:
264391
self._update_attributes(json)
265392
return True

src/github3/repos/commit.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ def check_runs(self):
4848
:class:`~github3.checks.CheckRun`
4949
"""
5050
url = self._build_url('check-runs', base_url=self._api)
51-
return self._iter(-1, url, checks.CheckRun)
51+
return self._iter(-1, url, checks.CheckRun,
52+
headers=checks.CheckRun.CUSTOM_HEADERS)
5253

5354
@decorators.requires_app_installation_auth
5455
def check_suites(self):
@@ -62,7 +63,8 @@ def check_suites(self):
6263
:class:`~github3.checks.CheckSuite`
6364
"""
6465
url = self._build_url('check-suites', base_url=self._api)
65-
return self._iter(-1, url, checks.CheckSuite)
66+
return self._iter(-1, url, checks.CheckSuite,
67+
headers=checks.CheckSuite.CUSTOM_HEADERS)
6668

6769
def diff(self):
6870
"""Retrieve the diff for this commit.

tests/unit/json/check_run_example

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"id": 4,
3+
"head_sha": "ce587453ced02b1526dfb4cb910479d431683101",
4+
"node_id": "MDg6Q2hlY2tSdW40",
5+
"external_id": "",
6+
"url": "https://api.github.com/repos/github/hello-world/check-runs/4",
7+
"html_url": "http://github.com/github/hello-world/runs/4",
8+
"details_url": "https://example.com",
9+
"status": "completed",
10+
"conclusion": "neutral",
11+
"started_at": "2018-05-04T01:14:52Z",
12+
"completed_at": "2018-05-04T01:14:52Z",
13+
"output": {
14+
"title": "Mighty Readme report",
15+
"summary": "There are 0 failures, 2 warnings, and 1 notice.",
16+
"text": "You may have some misspelled words on lines 2 and 4. You also may want to add a section in your README about how to install your app.",
17+
"annotations_count": 2,
18+
"annotations_url": "https://api.github.com/repos/github/hello-world/check-runs/4/annotations"
19+
},
20+
"name": "mighty_readme",
21+
"check_suite": {
22+
"id": 5
23+
},
24+
"app": {
25+
"id": 1,
26+
"node_id": "MDExOkludGVncmF0aW9uMQ==",
27+
"owner": {
28+
"login": "github",
29+
"id": 1,
30+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE=",
31+
"url": "https://api.github.com/orgs/github",
32+
"repos_url": "https://api.github.com/orgs/github/repos",
33+
"events_url": "https://api.github.com/orgs/github/events",
34+
"hooks_url": "https://api.github.com/orgs/github/hooks",
35+
"issues_url": "https://api.github.com/orgs/github/issues",
36+
"members_url": "https://api.github.com/orgs/github/members{/member}",
37+
"public_members_url": "https://api.github.com/orgs/github/public_members{/member}",
38+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
39+
"description": "A great organization"
40+
},
41+
"name": "Super CI",
42+
"description": "",
43+
"external_url": "https://example.com",
44+
"html_url": "https://github.com/apps/super-ci",
45+
"created_at": "2017-07-08T16:18:44-04:00",
46+
"updated_at": "2017-07-08T16:18:44-04:00"
47+
},
48+
"pull_requests": [
49+
{
50+
"url": "https://api.github.com/repos/github/hello-world/pulls/1",
51+
"id": 1934,
52+
"number": 3956,
53+
"head": {
54+
"ref": "say-hello",
55+
"sha": "3dca65fa3e8d4b3da3f3d056c59aee1c50f41390",
56+
"repo": {
57+
"id": 526,
58+
"url": "https://api.github.com/repos/github/hello-world",
59+
"name": "hello-world"
60+
}
61+
},
62+
"base": {
63+
"ref": "master",
64+
"sha": "e7fdf7640066d71ad16a86fbcbb9c6a10a18af4f",
65+
"repo": {
66+
"id": 526,
67+
"url": "https://api.github.com/repos/github/hello-world",
68+
"name": "hello-world"
69+
}
70+
}
71+
}
72+
]
73+
}

0 commit comments

Comments
 (0)