From 3fa77372d29dbf7c8b11758469e7f84420c08fc6 Mon Sep 17 00:00:00 2001 From: Thomas Carmet <8408330+tcarmet@users.noreply.github.com> Date: Thu, 25 Apr 2024 20:52:14 +0000 Subject: [PATCH] Fixes to support check runs --- .gitignore | 1 + bert_e/exceptions.py | 9 +++++---- bert_e/git_host/github/__init__.py | 30 +++++++++++++++++------------- bert_e/git_host/github/schema.py | 2 +- bert_e/tests/test_bert_e.py | 2 +- bert_e/workflow/pr_utils.py | 4 ++-- tox.ini | 2 +- 7 files changed, 28 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index a9d650db..70761f6b 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ settings.yml .coverage .pytest_cache/ build/ +.env diff --git a/bert_e/exceptions.py b/bert_e/exceptions.py index 43a3441f..9a66e802 100644 --- a/bert_e/exceptions.py +++ b/bert_e/exceptions.py @@ -22,13 +22,13 @@ class BertE_Exception(Exception): code = -1 + status: Literal[None, "in_progress", "queued", "success", "failure"] = None # base exceptions class TemplateException(BertE_Exception): code = -2 template = None - status: Literal[None, "in_progress", "success", "failure"] = None # whether to re-publish if the message is already in the history dont_repeat_if_in_history = -1 @@ -148,7 +148,7 @@ class Conflict(TemplateException): class ApprovalRequired(TemplateException): code = 115 template = 'need_approval.md' - status = "in_progress" + status = "queued" class BuildFailed(TemplateException): @@ -160,7 +160,7 @@ class BuildFailed(TemplateException): class AfterPullRequest(TemplateException): code = 120 template = 'after_pull_request.md' - status = "in_progress" + status = "queued" class IntegrationDataCreated(InformationException): @@ -265,7 +265,7 @@ class RequestIntegrationBranches(TemplateException): code = 135 template = "request_integration_branches.md" # TODO: review if it should be failure. - status = "in_progress" + status = "queued" class QueueBuildFailedMessage(TemplateException): @@ -568,6 +568,7 @@ class NothingToDo(SilentException): class BuildInProgress(SilentException): code = 303 + status = "in_progress" class BuildNotStarted(SilentException): diff --git a/bert_e/git_host/github/__init__.py b/bert_e/git_host/github/__init__.py index 21b3f280..12d5bbb1 100644 --- a/bert_e/git_host/github/__init__.py +++ b/bert_e/git_host/github/__init__.py @@ -53,7 +53,8 @@ def __init__(self, login: str, password: str, email: str, self.password = password self.app_id = app_id self.installation_id = installation_id - self.private_key = jwk_from_pem(private_key.encode('utf-8')) if private_key else None + self.private_key = jwk_from_pem(private_key.encode('utf-8')) \ + if private_key else None self.email = email self.org = org self.base_url = base_url.rstrip('/') @@ -832,11 +833,11 @@ def set_bot_status(self, status: str | None, title: str, summary: str): if status == "success": conclusion = "success" status = "completed" - elif status == "in_progress": - conclusion = None elif status == "failure": conclusion = "failure" status = "completed" + else: + conclusion = None self._add_checkrun( name='bert-e', status=status, conclusion=conclusion, @@ -846,18 +847,21 @@ def set_bot_status(self, status: str | None, title: str, summary: str): def _add_checkrun( self, name: str, status: str, conclusion: str | None, title: str, summary: str): + data = { + 'name': name, + 'head_sha': self.src_commit, + 'status': status, + 'output': { + 'title': title, + 'summary': summary, + }, + } + if conclusion is not None: + data['conclusion'] = conclusion + LOG.debug(data) return CheckRun.create( client=self.client, - data={ - 'name': name, - 'head_sha': self.src_commit, - 'status': status, - 'conclusion': conclusion, - 'output': { - 'title': title, - 'summary': summary, - }, - }, + data=data, owner=self.repo.owner, repo=self.repo.slug ) diff --git a/bert_e/git_host/github/schema.py b/bert_e/git_host/github/schema.py index 591a2db2..81e287ca 100644 --- a/bert_e/git_host/github/schema.py +++ b/bert_e/git_host/github/schema.py @@ -111,7 +111,7 @@ class AggregateCheckSuites(GitHubSchema): class Output(GitHubSchema): title = fields.Str() summary = fields.Str() - text = fields.Str() + text = fields.Str(allow_none=True) class CheckRun(GitHubSchema): diff --git a/bert_e/tests/test_bert_e.py b/bert_e/tests/test_bert_e.py index 68c80769..26721d4a 100644 --- a/bert_e/tests/test_bert_e.py +++ b/bert_e/tests/test_bert_e.py @@ -4797,7 +4797,7 @@ def test_set_bot_status(self): pr.src_commit, key="bert-e") == "failure" self.handle(pr.id, settings=settings, options=["bypass_jira_check"]) assert self.get_build_status( - pr.src_commit, key="bert-e") == "in_progress" + pr.src_commit, key="bert-e") == "queued" self.handle(pr.id, settings=settings, options=self.bypass_all) diff --git a/bert_e/workflow/pr_utils.py b/bert_e/workflow/pr_utils.py index e8b69fb7..0f66ce17 100644 --- a/bert_e/workflow/pr_utils.py +++ b/bert_e/workflow/pr_utils.py @@ -87,8 +87,8 @@ def _send_comment(settings, pull_request: AbstractPullRequest, msg: str, def _send_bot_status(settings, pull_request: AbstractPullRequest, comment: exceptions.TemplateException): """Post the bot status in a pull request.""" - if settings.send_bot_status is False: - LOG.debug("Not sending bot status (send_bot_status==False)") + if settings.send_bot_status is False or comment.status is None: + LOG.debug("No need to send bot status") return LOG.info(f"Setting bot status to {comment.status} as {comment.title}") pull_request.set_bot_status( diff --git a/tox.ini b/tox.ini index fe883985..41522a8e 100644 --- a/tox.ini +++ b/tox.ini @@ -80,7 +80,7 @@ setenv = WEBHOOK_LOGIN = {env:WEBHOOK_LOGIN:'webhook'} WEBHOOK_PWD = {env:WEBHOOK_PWD:'webhook'} -commands = bert-e-serve -v -f {env:BERT_E_SETTINGS:'settings.yml'} +commands = bert-e-serve -v -f {env:BERT_E_SETTINGS:'settings.yml'} -p 8000 [testenv:tests-githost] passenv = BERT_E_*