From 8fa60316d7d7bdd68c4402184bba9286b486db26 Mon Sep 17 00:00:00 2001 From: Thomas Carmet <8408330+tcarmet@users.noreply.github.com> Date: Fri, 1 Mar 2024 10:30:14 -0800 Subject: [PATCH] PTFE-1484 add capability to create checkruns (#166) --- bert_e/git_host/github/__init__.py | 46 +++++++++++++++++++++++ bert_e/git_host/github/schema.py | 16 ++++++++ bert_e/tests/unit/test_github_app_auth.py | 8 ++++ 3 files changed, 70 insertions(+) diff --git a/bert_e/git_host/github/__init__.py b/bert_e/git_host/github/__init__.py index b03a6c4f..e4948c4e 100644 --- a/bert_e/git_host/github/__init__.py +++ b/bert_e/git_host/github/__init__.py @@ -825,6 +825,24 @@ def add_comment(self, msg: str): url = self.data['comments_url'] return Comment.create(self.client, {'body': msg}, url=url) + def add_checkrun( + self, name: str, status: str, conclusion: str, + title: str, summary: str): + return CheckRun.create( + client=self.client, + data={ + 'name': name, + 'head_sha': self.src_commit, + 'status': status, + 'conclusion': conclusion, + 'output': { + 'title': title, + 'summary': summary, + }, + }, + owner=self.repo.owner, repo=self.repo.slug + ) + def get_comments(self): return Comment.list(self.client, url=self.data['comments_url']) @@ -974,6 +992,34 @@ def delete(self) -> None: self.client.delete(self.data['url']) +class CheckRun(base.AbstractGitHostObject): + GET_URL = '/repos/{owner}/{repo}/check-runs/{id}' + CREATE_URL = '/repos/{owner}/{repo}/check-runs' + + SCHEMA = schema.CheckRun + CREATE_SCHEMA = schema.CreateCheckRun + + @property + def name(self) -> str: + return self.data['name'] + + @property + def status(self) -> str: + return self.data['status'] + + @property + def conclusion(self) -> str: + return self.data['conclusion'] + + @property + def title(self) -> str: + return self.data['output']['title'] + + @property + def summary(self) -> str: + return self.data['output']['summary'] + + class Review(base.AbstractGitHostObject): LIST_URL = '/repos/{owner}/{repo}/pulls/{number}/reviews' CREATE_URL = LIST_URL diff --git a/bert_e/git_host/github/schema.py b/bert_e/git_host/github/schema.py index fda64620..591a2db2 100644 --- a/bert_e/git_host/github/schema.py +++ b/bert_e/git_host/github/schema.py @@ -108,12 +108,28 @@ class AggregateCheckSuites(GitHubSchema): check_suites = fields.Nested(CheckSuite, many=True) +class Output(GitHubSchema): + title = fields.Str() + summary = fields.Str() + text = fields.Str() + + class CheckRun(GitHubSchema): id = fields.Integer() head_sha = fields.Str() status = fields.Str() conclusion = fields.Str(allow_none=True) + output = fields.Nested(Output) html_url = fields.Url() + name = fields.Str() + + +class CreateCheckRun(GitHubSchema): + name = fields.Str() + head_sha = fields.Str() + status = fields.Str() + conclusion = fields.Str(allow_none=True) + output = fields.Nested(Output) class WorkflowRun(GitHubSchema): diff --git a/bert_e/tests/unit/test_github_app_auth.py b/bert_e/tests/unit/test_github_app_auth.py index 68d90f69..1ff0bc18 100644 --- a/bert_e/tests/unit/test_github_app_auth.py +++ b/bert_e/tests/unit/test_github_app_auth.py @@ -33,3 +33,11 @@ def test_github_auth_app(client_app): pr = repository.get_pull_request(1) assert pr.id == 1347 assert client_app.headers['Authorization'].startswith('Bearer ') is True + + +def test_github_check_run(client_app): + repository = client_app.get_repository('octo-org', 'Hello-World') + pr = repository.get_pull_request(1) + check_run = pr.add_checkrun( + 'bert-e', 'completed', 'success', 'title', 'summary') + assert check_run.name == check_run.data['name']