From 3ba6081400da8c700d768667e67db6e700ec5e63 Mon Sep 17 00:00:00 2001 From: Meni Yakove Date: Mon, 15 Jul 2024 21:29:07 +0300 Subject: [PATCH 1/3] Set approve label only if reviewer is part of approvers --- webhook_server_container/libs/github_api.py | 20 ++++++++++---------- webhook_server_container/utils/constants.py | 4 +++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/webhook_server_container/libs/github_api.py b/webhook_server_container/libs/github_api.py index 2618ea1b..85f5687d 100644 --- a/webhook_server_container/libs/github_api.py +++ b/webhook_server_container/libs/github_api.py @@ -1047,7 +1047,11 @@ def manage_reviewed_by_label(self, review_state: str, action: str, reviewed_user ) label_prefix = None label_to_remove = None - pull_request_labels = self.pull_request_labels_names() + + if reviewed_user in self.approvers: + approved_lgtm_label = f"APPROVED_BY_LABEL_PREFIX{reviewed_user}" + else: + approved_lgtm_label = f"LGTM_BY_LABEL_PREFIX{reviewed_user}" if review_state in ("approved", LGTM_STR): base_dict = self.hook_data.get("issue", self.hook_data.get("pull_request")) @@ -1056,17 +1060,14 @@ def manage_reviewed_by_label(self, review_state: str, action: str, reviewed_user LOGGER.info(f"{self.log_prefix} PR owner {pr_owner} set /lgtm, not adding label.") return - label_prefix = APPROVED_BY_LABEL_PREFIX _remove_label = f"{CHANGED_REQUESTED_BY_LABEL_PREFIX}{reviewed_user}" - if _remove_label in pull_request_labels: - label_to_remove = _remove_label + label_prefix = approved_lgtm_label + label_to_remove = _remove_label elif review_state == "changes_requested": label_prefix = CHANGED_REQUESTED_BY_LABEL_PREFIX - _remove_label = f"{APPROVED_BY_LABEL_PREFIX}{reviewed_user}" - - if _remove_label in pull_request_labels: - label_to_remove = _remove_label + _remove_label = approved_lgtm_label + label_to_remove = _remove_label elif review_state == "commented": label_prefix = COMMENTED_BY_LABEL_PREFIX @@ -1076,8 +1077,7 @@ def manage_reviewed_by_label(self, review_state: str, action: str, reviewed_user if action == ADD_STR: self._add_label(label=reviewer_label) - if label_to_remove: - self._remove_label(label=label_to_remove) + self._remove_label(label=label_to_remove) if action == DELETE_STR: self._remove_label(label=reviewer_label) diff --git a/webhook_server_container/utils/constants.py b/webhook_server_container/utils/constants.py index 67f1f3a3..d342f7ec 100644 --- a/webhook_server_container/utils/constants.py +++ b/webhook_server_container/utils/constants.py @@ -18,14 +18,15 @@ BUILD_CONTAINER_STR: str = "build-container" PYTHON_MODULE_INSTALL_STR: str = "python-module-install" WIP_STR: str = "wip" +LGTM_STR: str = "lgtm" CHERRY_PICK_LABEL_PREFIX: str = "cherry-pick-" CHERRY_PICKED_LABEL_PREFIX: str = "CherryPicked" APPROVED_BY_LABEL_PREFIX: str = "approved-" +LGTM_BY_LABEL_PREFIX: str = f"{LGTM_STR}-" CHANGED_REQUESTED_BY_LABEL_PREFIX: str = "changes-requested-" COMMENTED_BY_LABEL_PREFIX: str = "commented-" BRANCH_LABEL_PREFIX: str = "branch-" VERIFIED_LABEL_STR: str = "verified" -LGTM_STR: str = "lgtm" NEEDS_REBASE_LABEL_STR: str = "needs-rebase" HAS_CONFLICTS_LABEL_STR: str = "has-conflicts" HOLD_LABEL_STR: str = "hold" @@ -56,6 +57,7 @@ DYNAMIC_LABELS_DICT: Dict[str, str] = { APPROVED_BY_LABEL_PREFIX: "0E8A16", + LGTM_BY_LABEL_PREFIX: "DCED6F", COMMENTED_BY_LABEL_PREFIX: "D93F0B", CHANGED_REQUESTED_BY_LABEL_PREFIX: "F5621C", CHERRY_PICK_LABEL_PREFIX: "F09C74", From 15f3302d9c5ca641d7db4a7d9263694e6e5bfcfe Mon Sep 17 00:00:00 2001 From: Meni Yakove Date: Mon, 15 Jul 2024 21:50:41 +0300 Subject: [PATCH 2/3] Update the readme --- README.md | 80 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 806c2b1d..cc2f8e07 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,44 @@ # github-webhook-server -A [FastAPI-based](https://fastapi.tiangolo.com) webhook server for managing GitHub repositories. It handles tasks such as repository setup, branch protection, and webhook configuration. +A [FastAPI-based](https://fastapi.tiangolo.com) webhook server for managing GitHub pull requests workflow. and manage repositories. + +## Overview + +The tool will manage the following: + +###### Repositories + +- Configure repositories setting +- Configure branch protection +- Set itself as webhook for the repository +- Add missing lables to the repository + +###### Pull requests + +- Add reviewers from OWNER file +- Manage pull requests labels +- Check when the pull request is ready to be merged +- Build container from Dockerfile when pull request is merged +- Build container from Dockerfile when new release is pushed +- Push new release to PyPI when new release is pushed +- Open an issue for each pull request +- Add pull request size label + +###### Available user actions + +- Mark pull request as WIP by comment /wip to the pull request, To remove it from the pull request comment /wip cancel to the pull request. +- Block merging of pull request by comment /hold, To un-block merging of pull request comment /hold cancel. +- Mark pull request as verified by comment /verified to the pull request, to un-verify comment /verified cancel to the pull request. + - verified label removed on each new commit push. +- Cherry pick a merged pull request comment /cherry-pick in the pull request. + - Multiple target branches can be cherry-picked, separated by spaces. (/cherry-pick branch1 branch2) + - Cherry-pick will be started when pull request is merged +- Build and push container image command /build-and-push-container in the pull request (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= +- Add a label by comment use /