[feat]. Add username and custom bot mention tags
This commit is contained in:
@@ -13,6 +13,7 @@ class Settings(BaseSettings):
|
||||
gitea_base_url: str = Field(alias="GITEA_BASE_URL")
|
||||
gitea_token: SecretStr = Field(alias="GITEA_TOKEN")
|
||||
gitea_bot_username: str = Field(alias="GITEA_BOT_USERNAME")
|
||||
gitea_bot_mentions: str = Field(default="", alias="GITEA_BOT_MENTIONS")
|
||||
gitea_webhook_secret: SecretStr = Field(alias="GITEA_WEBHOOK_SECRET")
|
||||
|
||||
openai_api_key: SecretStr | None = Field(default=None, alias="OPENAI_API_KEY")
|
||||
@@ -60,6 +61,13 @@ class Settings(BaseSettings):
|
||||
values = [item.strip() for item in self.allowed_repos.split(",")]
|
||||
return {value for value in values if value}
|
||||
|
||||
@property
|
||||
def bot_command_aliases(self) -> set[str]:
|
||||
configured = [item.strip().lstrip("@").lower() for item in self.gitea_bot_mentions.split(",")]
|
||||
aliases = {"codex", self.gitea_bot_username.strip().lstrip("@").lower()}
|
||||
aliases.update(alias for alias in configured if alias)
|
||||
return aliases
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_settings() -> Settings:
|
||||
|
||||
@@ -422,7 +422,7 @@ async def gitea_webhook(
|
||||
return {"accepted": False, "reason": "bot comment ignored"}
|
||||
|
||||
comment_body = str(payload.get("comment", {}).get("body", "")).strip()
|
||||
parsed_command = parse_command(comment_body)
|
||||
parsed_command = parse_command(comment_body, aliases=settings.bot_command_aliases)
|
||||
if not parsed_command:
|
||||
logger.info(
|
||||
"Webhook ignored: no @codex review command repo=%s pr=%s comment_id=%s sender=%s",
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from collections.abc import Iterable
|
||||
|
||||
from gitea_codex_bot.types import ParsedCommand
|
||||
|
||||
COMMAND_RE = re.compile(r"^@codex\s+(review|explain|fix|ignore|rerun)\b(.*)$", re.IGNORECASE | re.DOTALL)
|
||||
COMMAND_RE = re.compile(r"^@([^\s]+)\s+(review|explain|fix|ignore|rerun)\b(.*)$", re.IGNORECASE | re.DOTALL)
|
||||
|
||||
|
||||
def parse_command(body: str) -> ParsedCommand | None:
|
||||
def parse_command(body: str, aliases: Iterable[str] | None = None) -> ParsedCommand | None:
|
||||
stripped = body.strip()
|
||||
match = COMMAND_RE.match(stripped)
|
||||
if not match:
|
||||
return None
|
||||
name = match.group(1).lower()
|
||||
rest = match.group(2).strip()
|
||||
command_alias = match.group(1).lstrip("@").lower()
|
||||
allowed_aliases = {alias.lstrip("@").lower() for alias in (aliases or {"codex"})}
|
||||
if command_alias not in allowed_aliases:
|
||||
return None
|
||||
|
||||
name = match.group(2).lower()
|
||||
rest = match.group(3).strip()
|
||||
tokens = [token for token in rest.split() if token]
|
||||
|
||||
parsed = ParsedCommand(name=name, raw=stripped, arguments=tokens)
|
||||
|
||||
Reference in New Issue
Block a user