From 7fae893669644900e117afd862c3b3df638c1aaf Mon Sep 17 00:00:00 2001 From: Meni Yakove Date: Tue, 15 Oct 2024 13:21:06 +0300 Subject: [PATCH 1/3] add support for `/retest all` --- webhook_server_container/libs/github_api.py | 97 ++++++++++++++------- 1 file changed, 66 insertions(+), 31 deletions(-) diff --git a/webhook_server_container/libs/github_api.py b/webhook_server_container/libs/github_api.py index ec14e04d..2a05b8ad 100644 --- a/webhook_server_container/libs/github_api.py +++ b/webhook_server_container/libs/github_api.py @@ -10,7 +10,7 @@ import time from concurrent.futures import Future, ThreadPoolExecutor, as_completed from pathlib import Path -from typing import Any, Dict, Generator, List, Optional, Set +from typing import Any, Callable, Dict, Generator, List, Optional, Set from stringcolor import cs from github.Branch import Branch @@ -149,6 +149,7 @@ def __init__(self, hook_data: Dict[Any, Any], headers: Headers, logger: logging. self.add_api_users_to_auto_verified_and_merged_users() self.supported_user_labels_str: str = "".join([f" * {label}\n" for label in USER_LABELS_DICT.keys()]) + self.current_pull_request_supported_retest = self._current_pull_request_supported_retest() self.welcome_msg: str = f""" Report bugs in [Issues](https://github.com/myakove/github-webhook-server/issues) @@ -1850,41 +1851,58 @@ def process_cherry_pick_command(self, issue_comment_id: int, command_args: str, def process_retest_command(self, issue_comment_id: int, command_args: str) -> None: _target_tests: List[str] = command_args.split() - for _test in _target_tests: - if _test == TOX_STR: - if not self.tox: - msg: str = f"No {TOX_STR} configured for this repository" - error_msg = f"{self.log_prefix} {msg}." - self.logger.info(error_msg) - self.pull_request.create_issue_comment(msg) - return + _not_supported_retests: List[str] = [] + _supported_retests: List[str] = [] + _retests_to_func_map: Dict[str, Callable] = { + TOX_STR: self._run_tox, + PRE_COMMIT_STR: self._run_pre_commit, + BUILD_CONTAINER_STR: self._run_build_container, + PYTHON_MODULE_INSTALL_STR: self._run_install_python_module, + } - self.create_comment_reaction(issue_comment_id=issue_comment_id, reaction=REACTIONS.ok) - self._run_tox() + if not _target_tests: + msg = "No test defined to retest" + error_msg = f"{self.log_prefix} {msg}." + self.logger.debug(error_msg) + self.pull_request.create_issue_comment(msg) + return - elif _test == PRE_COMMIT_STR: - self.create_comment_reaction(issue_comment_id=issue_comment_id, reaction=REACTIONS.ok) - self._run_pre_commit() + if "all" in command_args: + if len(command_args) > 1: + msg = "Invalid command. `all` cannot be used with other tests" + error_msg = f"{self.log_prefix} {msg}." + self.logger.debug(error_msg) + self.pull_request.create_issue_comment(msg) + return + + else: + _supported_retests = self.current_pull_request_supported_retest + + else: + for _test in _target_tests: + if _test in self.current_pull_request_supported_retest: + _supported_retests.append(_test) - elif _test == BUILD_CONTAINER_STR: - if self.build_and_push_container: - self.create_comment_reaction(issue_comment_id=issue_comment_id, reaction=REACTIONS.ok) - self._run_build_container() else: - msg = f"No {BUILD_CONTAINER_STR} configured for this repository" - error_msg = f"{self.log_prefix} {msg}" - self.logger.info(error_msg) - self.pull_request.create_issue_comment(msg) - - elif _test == PYTHON_MODULE_INSTALL_STR: - if not self.pypi: - error_msg = f"{self.log_prefix} No pypi configured" - self.logger.info(error_msg) - self.pull_request.create_issue_comment(error_msg) - return + _not_supported_retests.append(_test) - self.create_comment_reaction(issue_comment_id=issue_comment_id, reaction=REACTIONS.ok) - self._run_install_python_module() + if _not_supported_retests: + msg = f"No {' '.join(_not_supported_retests)} configured for this repository" + error_msg = f"{self.log_prefix} {msg}." + self.logger.debug(error_msg) + self.pull_request.create_issue_comment(msg) + + if _supported_retests: + self.create_comment_reaction(issue_comment_id=issue_comment_id, reaction=REACTIONS.ok) + + _retest_to_exec: List[Future] = [] + with ThreadPoolExecutor() as executor: + for _test in _supported_retests: + _retest_to_exec.append(executor.submit(_retests_to_func_map[_test])) + + for result in as_completed(_retest_to_exec): + if _exp := result.exception(): + self.logger.error(f"{self.log_prefix} {_exp}") def remove_labels_when_pull_request_sync(self) -> None: futures = [] @@ -2005,3 +2023,20 @@ def set_pull_request_automerge(self) -> None: except Exception as exp: self.logger.error(f"{self.log_prefix} Exception while setting auto merge: {exp}") + + def _current_pull_request_supported_retest(self) -> List[str]: + current_pull_request_supported_retest: List[str] = [] + + if self.tox: + current_pull_request_supported_retest.append(TOX_STR) + + if self.build_and_push_container: + current_pull_request_supported_retest.append(BUILD_CONTAINER_STR) + + if self.pypi: + current_pull_request_supported_retest.append(PYTHON_MODULE_INSTALL_STR) + + if self.pre_commit: + current_pull_request_supported_retest.append(PRE_COMMIT_STR) + + return current_pull_request_supported_retest From 618282c0830d6aa981ddc60be06aeec3ff3bca8e Mon Sep 17 00:00:00 2001 From: Meni Yakove Date: Tue, 15 Oct 2024 13:24:14 +0300 Subject: [PATCH 2/3] update wellcome msg and readme --- README.md | 1 + webhook_server_container/libs/github_api.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index 06323291..431222bb 100644 --- a/README.md +++ b/README.md @@ -263,6 +263,7 @@ reviewers: - `/retest tox`: run tox - `/retest build-container`: run build-container - `/retest python-module-install`: run python-module-install command +- `/retest all`: run all tests - `/build-and-push-container`: build and push container image (tag will be the pull request number) - You can add extra args to the Podman build command - Example: `/build-and-push-container --build-arg OPENSHIFT_PYTHON_WRAPPER_COMMIT=` diff --git a/webhook_server_container/libs/github_api.py b/webhook_server_container/libs/github_api.py index 2a05b8ad..f1346641 100644 --- a/webhook_server_container/libs/github_api.py +++ b/webhook_server_container/libs/github_api.py @@ -239,6 +239,12 @@ def prepare_retest_wellcome_msg(self) -> str: if self.pypi: retest_msg += f" * `/retest {PYTHON_MODULE_INSTALL_STR}`: Retest python-module-install\n" + if self.pre_commit: + retest_msg += f" * `/retest {PRE_COMMIT_STR}`: Retest pre-commit\n" + + if retest_msg: + retest_msg += " * `/retest all`: Retest all\n" + return " * This repository does not support retest actions" if not retest_msg else retest_msg def add_api_users_to_auto_verified_and_merged_users(self) -> None: From 2107bc5213ce272a28aa097c28af4e90eb149549 Mon Sep 17 00:00:00 2001 From: Meni Yakove Date: Tue, 15 Oct 2024 13:35:05 +0300 Subject: [PATCH 3/3] Set current supported retest func as propery --- webhook_server_container/libs/github_api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/webhook_server_container/libs/github_api.py b/webhook_server_container/libs/github_api.py index f1346641..e6a66dd3 100644 --- a/webhook_server_container/libs/github_api.py +++ b/webhook_server_container/libs/github_api.py @@ -149,7 +149,7 @@ def __init__(self, hook_data: Dict[Any, Any], headers: Headers, logger: logging. self.add_api_users_to_auto_verified_and_merged_users() self.supported_user_labels_str: str = "".join([f" * {label}\n" for label in USER_LABELS_DICT.keys()]) - self.current_pull_request_supported_retest = self._current_pull_request_supported_retest() + self.current_pull_request_supported_retest = self._current_pull_request_supported_retest self.welcome_msg: str = f""" Report bugs in [Issues](https://github.com/myakove/github-webhook-server/issues) @@ -2030,6 +2030,7 @@ def set_pull_request_automerge(self) -> None: except Exception as exp: self.logger.error(f"{self.log_prefix} Exception while setting auto merge: {exp}") + @property def _current_pull_request_supported_retest(self) -> List[str]: current_pull_request_supported_retest: List[str] = []