From a370ddab115c566158a7e6f34e072faf78ddadc3 Mon Sep 17 00:00:00 2001 From: Pey Lian Lim <2090236+pllim@users.noreply.github.com> Date: Tue, 4 Dec 2018 00:18:25 -0700 Subject: [PATCH 1/4] Add new method to access check run API --- baldrick/github/github_api.py | 57 +++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/baldrick/github/github_api.py b/baldrick/github/github_api.py index 73b9f13..2c2190a 100644 --- a/baldrick/github/github_api.py +++ b/baldrick/github/github_api.py @@ -474,6 +474,63 @@ def is_closed(self): class PullRequestHandler(IssueHandler): + # https://developer.github.com/v3/checks/runs/#create-a-check-run + def set_check(self, name, commit_hash, summary, details_url=None, + status='queued', conclusion='neutral'): + """ + Set check status. + + .. note:: This method does not provide API access to full + check run capability (e.g., Markdown text, annotation, + image). Add them as needed. + + Parameters + ---------- + name : str + Name of the check. + + commit_hash: str + The SHA of the commit. + + summary : str + Summary of the check run. + + details_url : str + The URL of the integrator's site that has the full details + of the check. + + status : { 'queued' | 'in_progress' | 'completed' } + The current status. + + conclusion : { 'success' | 'failure' | 'neutral' | 'cancelled' | 'timed_out' | 'action_required' } + The final conclusion of the check. + Required if you provide a status of ``'completed'``. + When the conclusion is ``'action_required'``, additional details + should be provided on the site specified by ``'details_url'``. + Note: Providing conclusion will automatically set the status + parameter to ``'completed'``. + + """ + url = f'{HOST}/repos/{self.repo}/check-runs' + headers = {'Accept': 'application/vnd.github.antiope-preview+json'} + + if commit_hash == "head": + commit_hash = self.head_sha + elif commit_hash == "base": + commit_hash = self.base_sha + + completed_at = datetime.utcnow().isoformat(timespec='seconds') + 'Z' + + output = {'title': name, 'summary': summary} + parameters = {'name': name, 'head_sha': commit_hash, 'status': status, + 'conclusion': conclusion, 'completed_at': completed_at, + 'output': output} + if details_url is not None: + parameters['details_url'] = details_url + + response = requests.post(url, headers=headers, json=parameters) + assert response.ok, response.content + def set_status(self, state, description, context, commit_hash="head", target_url=None): """ Set status message on a commit on GitHub. From b4e21ccacd84bd2444e34b884b4188f10e6ae591 Mon Sep 17 00:00:00 2001 From: Pey Lian Lim <2090236+pllim@users.noreply.github.com> Date: Fri, 7 Dec 2018 15:43:25 -0700 Subject: [PATCH 2/4] Address review comments from @astrofrog --- baldrick/github/github_api.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/baldrick/github/github_api.py b/baldrick/github/github_api.py index 2c2190a..96689b0 100644 --- a/baldrick/github/github_api.py +++ b/baldrick/github/github_api.py @@ -475,7 +475,7 @@ def is_closed(self): class PullRequestHandler(IssueHandler): # https://developer.github.com/v3/checks/runs/#create-a-check-run - def set_check(self, name, commit_hash, summary, details_url=None, + def set_check(self, name, summary, commit_hash='head', details_url=None, status='queued', conclusion='neutral'): """ Set check status. @@ -489,13 +489,13 @@ def set_check(self, name, commit_hash, summary, details_url=None, name : str Name of the check. - commit_hash: str - The SHA of the commit. - summary : str Summary of the check run. - details_url : str + commit_hash: { 'head' | 'base' } + The SHA of the commit. + + details_url : str or `None` The URL of the integrator's site that has the full details of the check. @@ -512,7 +512,8 @@ def set_check(self, name, commit_hash, summary, details_url=None, """ url = f'{HOST}/repos/{self.repo}/check-runs' - headers = {'Accept': 'application/vnd.github.antiope-preview+json'} + headers = self._headers + headers['Accept'] = 'application/vnd.github.antiope-preview+json' if commit_hash == "head": commit_hash = self.head_sha From ccd329f4c373f212d3a6bf09066b7faa2838d2c9 Mon Sep 17 00:00:00 2001 From: Pey Lian Lim <2090236+pllim@users.noreply.github.com> Date: Fri, 7 Dec 2018 15:45:20 -0700 Subject: [PATCH 3/4] DOC: Add change log --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 25be3c7..8c19b87 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,7 +1,7 @@ 0.3 (unreleased) ---------------- -* No changes yet. +* New API to post checks on GitHub. [#45] 0.2 (2018-11-22) ---------------- From 3868a220d73345b49050181ad6427a34e3205ee8 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Fri, 7 Dec 2018 16:09:15 -0700 Subject: [PATCH 4/4] Add optionals [docs only] --- baldrick/github/github_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baldrick/github/github_api.py b/baldrick/github/github_api.py index 96689b0..683b4f4 100644 --- a/baldrick/github/github_api.py +++ b/baldrick/github/github_api.py @@ -492,10 +492,10 @@ def set_check(self, name, summary, commit_hash='head', details_url=None, summary : str Summary of the check run. - commit_hash: { 'head' | 'base' } + commit_hash: { 'head' | 'base' }, optional The SHA of the commit. - details_url : str or `None` + details_url : str or `None`, optional The URL of the integrator's site that has the full details of the check.