Skip to content

Commit

Permalink
rename command, move input validation to earlier phase
Browse files Browse the repository at this point in the history
  • Loading branch information
markusressel committed Sep 28, 2024
1 parent 90563ce commit 6c29dd0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
21 changes: 12 additions & 9 deletions keel_telegram_bot/bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,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,
CommandHandler(COMMAND_UPDATE,
filters=(~ filters.REPLY) & (~ filters.FORWARDED),
callback=self._set_approval_count_callback),
callback=self._update_callback),
CommandHandler(COMMAND_APPROVE,
filters=(~ filters.REPLY) & (~ filters.FORWARDED),
callback=self._approve_callback),
Expand Down Expand Up @@ -321,9 +321,9 @@ async def _list_approvals_callback(
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",
@COMMAND_TIME_UPDATE.time()
@command(name=COMMAND_UPDATE,
description="Update the properties of a resource",
arguments=[
Argument(name=["identifier", "i"], description="Resource identifier",
example="daemonset/docker-proxy/docker-proxy"),
Expand All @@ -339,7 +339,7 @@ async def _list_approvals_callback(
],
error_handler=CustomErrorHandler(),
permissions=CONFIGURED_CHAT_ID & CONFIG_ADMINS)
async def _set_approval_count_callback(
async def _update_callback(
self, update: Update, context: ContextTypes.DEFAULT_TYPE,
identifier: str,
count: int or None,
Expand All @@ -354,6 +354,12 @@ async def _set_approval_count_callback(
message = update.effective_message
chat_id = update.effective_chat.id

# NOTE: validation needs to happen before entering the keyboard callback, because errors in the keyboard callback
# are currently not propagated properly.
if schedule is not None:
if trigger is None:
raise ValueError("Cannot set schedule without trigger")

async def execute(update: Update, context: ContextTypes.DEFAULT_TYPE, item: Resource, data: dict):
bot = context.bot
message = update.effective_message
Expand All @@ -372,9 +378,6 @@ async def execute(update: Update, context: ContextTypes.DEFAULT_TYPE, item: Reso
)

if schedule is not None:
if trigger is None:
raise ValueError("Cannot set schedule without trigger")

self._api_client.set_schedule(
identifier=item.identifier,
schedule=schedule,
Expand Down
2 changes: 1 addition & 1 deletion keel_telegram_bot/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
COMMAND_LIST_RESOURCES = ["resources"]
COMMAND_LIST_TRACKED = ["tracked"]
COMMAND_LIST_APPROVALS = ["approvals"]
COMMAND_SET_APPROVAL_COUNT = ["set_approval_count"]
COMMAND_UPDATE = ["update"]
COMMAND_APPROVE = ["approve", "a"]
COMMAND_REJECT = ["reject", "r"]
COMMAND_DELETE = ["delete", "d"]
Expand Down
2 changes: 1 addition & 1 deletion keel_telegram_bot/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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_UPDATE = COMMAND_TIME.labels(command=COMMAND_UPDATE[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 6c29dd0

Please sign in to comment.