From b00995eb8a096963901225e2f11233fcfdf6870e Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Sat, 28 Sep 2024 01:16:19 +0200 Subject: [PATCH] added /set_approval_count command --- keel_telegram_bot/bot/__init__.py | 37 +++++++++++++++++++++++++- keel_telegram_bot/client/api_client.py | 10 +++++++ keel_telegram_bot/stats.py | 1 + 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/keel_telegram_bot/bot/__init__.py b/keel_telegram_bot/bot/__init__.py index 8605582..f0def2a 100644 --- a/keel_telegram_bot/bot/__init__.py +++ b/keel_telegram_bot/bot/__init__.py @@ -18,7 +18,7 @@ from keel_telegram_bot.client.approval import Approval from keel_telegram_bot.client.resource import Resource from keel_telegram_bot.client.tracked_image import TrackedImage -from keel_telegram_bot.client.types import SemverPolicyType +from keel_telegram_bot.client.types import SemverPolicyType, Provider from keel_telegram_bot.config import Config from keel_telegram_bot.stats import * from keel_telegram_bot.util import send_message, approval_to_str, resource_to_str, tracked_image_to_str @@ -59,6 +59,9 @@ def __init__(self, config: Config, api_client: KeelApiClient): CommandHandler(COMMAND_LIST_APPROVALS, filters=(~ filters.REPLY) & (~ filters.FORWARDED), callback=self._list_approvals_callback), + CommandHandler(COMMAND_SET_APPROVAL_COUNT, + filters=(~ filters.REPLY) & (~ filters.FORWARDED), + callback=self._set_approval_count_callback), CommandHandler(COMMAND_APPROVE, filters=(~ filters.REPLY) & (~ filters.FORWARDED), callback=self._approve_callback), @@ -287,6 +290,38 @@ async def _list_approvals_callback(self, update: Update, context: ContextTypes.D text = "\n\n".join(lines).strip() await send_message(bot, chat_id, text, reply_to=message.message_id, parse_mode="HTML") + @COMMAND_TIME_SET_APPROVAL_COUNT.time() + @command(name=COMMAND_SET_APPROVAL_COUNT, + description="Set the approval count for a resource", + arguments=[ + Argument(name=["identifier", "i"], description="Resource identifier", + example="daemonset/docker-proxy/docker-proxy"), + Argument(name=["count", "c"], description="Approval count", example="2", type=int), + ], + permissions=CONFIGURED_CHAT_ID & CONFIG_ADMINS) + async def _set_approval_count_callback(self, update: Update, context: ContextTypes.DEFAULT_TYPE, + identifier: str, count: int) -> None: + """ + Set the required approval count for a resource + """ + bot = context.bot + message = update.effective_message + chat_id = update.effective_chat.id + + self._api_client.get_resources() + + self._api_client.set_required_approvals_count( + identifier=identifier, + provider=Provider.Kubernetes, + votes_required=count + ) + + resource = self._api_client.get_resource(identifier=identifier) + resource_lines = resource_to_str(resource) + text = resource_lines + + await send_message(bot, chat_id, text, reply_to=message.message_id) + @COMMAND_TIME_APPROVE.time() @command(name=COMMAND_APPROVE, description="Approve a pending item", diff --git a/keel_telegram_bot/client/api_client.py b/keel_telegram_bot/client/api_client.py index bb80940..7ad379a 100644 --- a/keel_telegram_bot/client/api_client.py +++ b/keel_telegram_bot/client/api_client.py @@ -39,6 +39,16 @@ def get_resources(self) -> List[Resource]: result = [Resource.from_dict(resource) for resource in response] return result + def get_resource(self, identifier: str) -> Resource or None: + """ + Returns a resource by identifier + """ + resources = self.get_resources() + for resource in resources: + if resource.identifier == identifier: + return resource + return None + def get_tracked(self) -> List[TrackedImage]: """ Returns a list of all tracked images diff --git a/keel_telegram_bot/stats.py b/keel_telegram_bot/stats.py index a239ccd..1adf591 100644 --- a/keel_telegram_bot/stats.py +++ b/keel_telegram_bot/stats.py @@ -8,6 +8,7 @@ COMMAND_TIME_LIST_RESOURCES = COMMAND_TIME.labels(command=COMMAND_LIST_RESOURCES[0]) COMMAND_TIME_LIST_TRACKED = COMMAND_TIME.labels(command=COMMAND_LIST_TRACKED[0]) COMMAND_TIME_LIST_APPROVALS = COMMAND_TIME.labels(command=COMMAND_LIST_APPROVALS[0]) +COMMAND_TIME_SET_APPROVAL_COUNT = COMMAND_TIME.labels(command=COMMAND_SET_APPROVAL_COUNT[0]) COMMAND_TIME_APPROVE = COMMAND_TIME.labels(command=COMMAND_APPROVE[0]) COMMAND_TIME_REJECT = COMMAND_TIME.labels(command=COMMAND_REJECT[0]) COMMAND_TIME_DELETE = COMMAND_TIME.labels(command=COMMAND_DELETE[0])