oncall-engine/engine/apps/slack/slash_command.py
Innokentii Konstantinov 94219c25bb
Introduce slash command matcher (#4717)
With the Unified Slack app we now have two ways of calling commands.
1. Legacy one when command invoked directly: /escalate
2. Unified one: /grafana escalate
On top of that we have different slach commands for each environment:
/escalate-local, /escalate-dev, etc. It was leading to a weird command
to escalate via Unified App in dev u need to type: /grafana-dev
escalate-develop.

To support both, I introduced a matcher function for SlashCommandRoutes.
It allows to simplify handling of such cases without complex workarounds
in an EventAPIEndpoint.


# What this PR does

## Which issue(s) this PR closes

Related to [issue link here]

<!--
*Note*: If you want the issue to be auto-closed once the PR is merged,
change "Related to" to "Closes" in the line above.
If you have more than one GitHub issue that this PR closes, be sure to
preface
each issue link with a [closing
keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue).
This ensures that the issue(s) are auto-closed once the PR has been
merged.
-->

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [ ] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
2024-07-24 09:53:06 +00:00

41 lines
1.2 KiB
Python

from apps.slack.types.interaction_payloads import SlashCommandPayload
class SlashCommand:
"""
SlashCommand represents slack slash command.
Attributes:
command -- command itself
args -- list of args passed to command
Examples:
/grafana escalate
SlashCommand(command='grafana', args=['escalate'])
"""
def __init__(self, command, args):
# command itself
self.command = command
# list of args passed to command
self.args = args
@property
def subcommand(self):
"""
Return first arg as action subcommand: part of command which defines action
Example: /grafana escalate -> escalate
"""
return self.args[0] if len(self.args) > 0 else None
@staticmethod
def parse(payload: SlashCommandPayload):
"""
Parse slack slash command payload and return SlashCommand object
"""
command = payload["command"].lstrip("/")
args = payload["text"].split()
return SlashCommand(command, args)
@property
def is_grafana_command(self):
return self.command in ["grafana", "grafana-dev", "grafana-ops", "grafana-prod"]