Skip to content

Commit

Permalink
added /set_approval_count <identifier> <count> command
Browse files Browse the repository at this point in the history
  • Loading branch information
markusressel committed Sep 27, 2024
1 parent 293b8af commit b00995e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
37 changes: 36 additions & 1 deletion keel_telegram_bot/bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions keel_telegram_bot/client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions keel_telegram_bot/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit b00995e

Please sign in to comment.