Skip to content

Commit 0a76731

Browse files
omgjlkwestphahl
authored andcommitted
Add methods to interact with Checks API
This commit adds the methods necessary to create, list, read, and update CheckSuite objects and CheckRun objects. Suites and Runs can be fetched from a given commit object. They can be created via the repository object, or a singular item of each can be fetched by ID from the repository object. There is another TODO here to deep clean the CheckRun arguments due to nested objects. See sigmavirus24#887 for more details.
1 parent 6a3fb45 commit 0a76731

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

src/github3/repos/commit.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from __future__ import unicode_literals
44

55
from . import status
6-
from .. import git, models, users
6+
from .. import checks, git, models, users
77
from .comment import RepoComment
88

9+
from ..decorators import requires_auth
10+
911

1012
class _RepoCommit(models.GitHubCore):
1113
"""The :class:`RepoCommit <RepoCommit>` object.
@@ -36,6 +38,30 @@ def _update_attributes(self, commit):
3638
def _repr(self):
3739
return "<{0} [{1}]>".format(self.class_name, self.sha[:7])
3840

41+
@requires_auth
42+
def check_runs(self):
43+
"""Retrieve the check runs for this commit.
44+
45+
:returns:
46+
the check runs for this commit
47+
:rtype:
48+
:class:`~github3.checks.CheckRun`
49+
"""
50+
url = self._build_url('check-runs', base_url=self._api)
51+
return self._iter(-1, url, checks.CheckRun)
52+
53+
@requires_auth
54+
def check_suites(self):
55+
"""Retrieve the check suites for this commit.
56+
57+
:returns:
58+
the check suites for this commit
59+
:rtype:
60+
:class:`~github3.checks.CheckSuite`
61+
"""
62+
url = self._build_url('check-suites', base_url=self._api)
63+
return self._iter(-1, url, checks.CheckSuite)
64+
3965
def diff(self):
4066
"""Retrieve the diff for this commit.
4167

src/github3/repos/repo.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import uritemplate as urit
1414

15+
from .. import checks
1516
from .. import events
1617
from .. import exceptions
1718
from .. import git
@@ -263,6 +264,42 @@ def branches(self, number=-1, protected=False, etag=None):
263264
headers=branch.Branch.PREVIEW_HEADERS,
264265
)
265266

267+
@requires_auth
268+
def check_run(self, id):
269+
"""Return a single check run.
270+
271+
:param int id:
272+
(required), id of the check run
273+
:returns:
274+
the check run
275+
:rtype:
276+
:class:`~github3.checks.CheckRun`
277+
"""
278+
data = None
279+
if int(id) > 0:
280+
url = self._build_url('check-runs', str(id),
281+
base_url=self._api)
282+
data = self._json(self._get(url), 200)
283+
return self._instance_or_null(checks.CheckRun, data)
284+
285+
@requires_auth
286+
def check_suite(self, id):
287+
"""Return a single check suite.
288+
289+
:param int id:
290+
(required), id of the check suite
291+
:returns:
292+
the check suite
293+
:rtype:
294+
:class:`~github3.checks.CheckSuite`
295+
"""
296+
data = None
297+
if int(id) > 0:
298+
url = self._build_url('check-suites', str(id),
299+
base_url=self._api)
300+
data = self._json(self._get(url), 200)
301+
return self._instance_or_null(checks.CheckSuite, data)
302+
266303
def code_frequency(self, number=-1, etag=None):
267304
"""Iterate over the code frequency per week.
268305
@@ -517,6 +554,77 @@ def contributors(self, anon=False, number=-1, etag=None):
517554
params = {"anon": "true"}
518555
return self._iter(int(number), url, users.Contributor, params, etag)
519556

557+
@requires_auth
558+
def create_check_run(self, name, head_sha, details_url=None,
559+
external_id=None, started_at=None, status=None,
560+
conclusion=None, completed_at=None, output=None,
561+
actions=None):
562+
"""Create a check run object on a commit
563+
564+
:param str name:
565+
(required), The name of the check
566+
:param str head_sha:
567+
(required), The SHA of the commit
568+
:param str details_url:
569+
(optional), The URL of the integrator's site that has the full
570+
details of the check
571+
:param str external_id:
572+
(optional), A reference for the run on the integrator's system
573+
:param str started_at:
574+
(optional), ISO 8601 time format: YYYY-MM-DDTHH:MM:SSZ
575+
:param str status:
576+
(optional), ('queued', 'in_progress', 'completed')
577+
:param str conclusion:
578+
(optional), Required if you provide 'completed_at', or a
579+
'status' of 'completed'. The final conclusion of the check.
580+
('success', 'failure', 'neutral', 'cancelled', 'timed_out',
581+
'action_required')
582+
:param str completed_at:
583+
(optional), Required if you provide 'conclusion'. ISO 8601 time
584+
format: YYYY-MM-DDTHH:MM:SSZ
585+
:param dict output:
586+
(optional), key-value pairs representing the output. Format:
587+
{'title': 'string', 'summary', 'text, can be markdown', 'text':
588+
'text, can be markdown', 'annotations': [{}], 'images': [{}]}
589+
:param array actions:
590+
(optiona), array of action objects. Object format is:
591+
{'label': 'text', 'description', 'text', 'identifier', 'text'}
592+
:returns:
593+
the created check run
594+
:rtype:
595+
:class:`~github3.checks.CheckRun`
596+
"""
597+
json = None
598+
# TODO: Cleanse output dict, actions array
599+
if name and head_sha:
600+
data = {'name': name, 'details_url': details_url, 'external_id':
601+
external_id, 'started_at': started_at, 'status': status,
602+
'conclusion': conclusion, 'completed_at': completed_at,
603+
'output': output, 'actions': actions}
604+
self._remove_none(data)
605+
url = self._build_url('check-runs', base_url=self._api)
606+
json = self._json(self._post(url, data=data), 201)
607+
return self._instance_or_null(checks.CheckRun, json)
608+
609+
@requires_auth
610+
def create_check_suite(self, head_sha):
611+
"""Create a check suite object on a commit
612+
613+
:param str head_sha:
614+
The sha of the head commit.
615+
:returns:
616+
the created check suite
617+
:rtype:
618+
:class:`~github3.checks.CheckSuite`
619+
"""
620+
json = None
621+
if head_sha:
622+
data = {'head_sha': head_sha}
623+
self._remove_none(data)
624+
url = self._build_url('check-suites', base_url=self._api)
625+
json = self._json(self._post(url, data=data), 201)
626+
return self._instance_or_null(checks.CheckSuite, json)
627+
520628
@requires_auth
521629
def create_blob(self, content, encoding):
522630
"""Create a blob with ``content``.

0 commit comments

Comments
 (0)