From 0de069fd3289e083bd896e7d07a863e52ac3e9d7 Mon Sep 17 00:00:00 2001 From: Space-Banane Date: Sat, 23 May 2026 12:48:00 +0200 Subject: [PATCH] [feat]. Add username and custom bot mention tags --- .env.example | 2 ++ README.md | 5 ++-- TODO.md | 2 +- src/gitea_codex_bot/config.py | 8 +++++ src/gitea_codex_bot/main.py | 2 +- src/gitea_codex_bot/services/commands.py | 14 ++++++--- tests/test_commands.py | 13 +++++++++ tests/test_config.py | 13 +++++++++ tests/test_webhook.py | 37 ++++++++++++++++++++++++ 9 files changed, 88 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index 9348323..fb27d06 100644 --- a/.env.example +++ b/.env.example @@ -4,6 +4,8 @@ GITEA_BASE_URL=https://gitea.reversed.dev # Bot account token used to read PRs and write comments. GITEA_TOKEN=replace GITEA_BOT_USERNAME=codex-bot +# Optional extra command mentions (comma-separated), e.g. "@review-buddy,helper-bot". +GITEA_BOT_MENTIONS= # Shared secret configured on the Gitea webhook. GITEA_WEBHOOK_SECRET=replace diff --git a/README.md b/README.md index 92f66ce..6c6332d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Webhook-driven PR review bot for Gitea. - Handles `issue_comment` and `pull_request_comment` events. - Verifies `X-Gitea-Signature` HMAC (`sha256`). -- Triggers on `@codex review`, `@codex rerun`, `@codex explain`, `@codex fix`, `@codex ignore`. +- Triggers on `@codex ...`, `@ ...`, plus optional custom aliases from `GITEA_BOT_MENTIONS`. - Ignores bot-authored comments. - Enforces strict repository allowlist (`ALLOWED_REPOS`). - Deduplicates webhook deliveries/comments in DB. @@ -54,6 +54,7 @@ Optional: - `OPENAI_API_KEY` (required when `CODEX_AUTH_MODE=api_key`, optional when `CODEX_AUTH_MODE=chatgpt`) - `OPENAI_PROJECT_ID` - `OPENAI_ORG_ID` +- `GITEA_BOT_MENTIONS` (comma-separated extra mention aliases, e.g. `@review-buddy,helper-bot`) - `CODEX_AUTH_MODE` (`api_key` default, or `chatgpt`) - `CODEX_AUTH_JSON_PATH` (custom host path to `auth.json`; defaults to `~/.codex/auth.json` in `chatgpt` mode) - `DATABASE_URL` (overrides composed DB URL) @@ -95,4 +96,4 @@ This project was made WITH codex and is meant to be used WITH codex as a review If you are as rich as Peter Steinberg and get a free OpenAI API Key, feel free to use it for this bot. ## Contributing -Contributions are welcome! Please open issues or submit pull requests for bug fixes, improvements, or new features. \ No newline at end of file +Contributions are welcome! Please open issues or submit pull requests for bug fixes, improvements, or new features. diff --git a/TODO.md b/TODO.md index 9e12e4f..3944a14 100644 --- a/TODO.md +++ b/TODO.md @@ -22,7 +22,7 @@ - [ ] `FEATURE`: Add retries/backoff for `codex exec` bootstrap (`npm install -g @openai/codex`) to reduce transient network/setup failures. - [ ] `FEATURE`: `WEBHOOK_MODE` is currently informational only; add runtime validation/check endpoint that confirms expected webhook scope (`repo` or `global`) is actually configured in Gitea by host admin. - [ ] `TEST`: Add end-to-end test path against live Gitea + MariaDB + docker runner (webhook -> queue -> runner -> PR comment update). -- [ ] `FEATURE`: Add username as possible command prefix, ex. "@bot-name review" in addition to "@codex review", for better UX discoverability. +- [x] `FEATURE`: Add username as possible command prefix, ex. "@bot-name review" in addition to "@codex review", for better UX discoverability. ### P2 (Nice to Have) - [x] `FEATURE`: Add a note line at the end of comments to show model tokens used and such. diff --git a/src/gitea_codex_bot/config.py b/src/gitea_codex_bot/config.py index b6d3481..3f4fd5a 100644 --- a/src/gitea_codex_bot/config.py +++ b/src/gitea_codex_bot/config.py @@ -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: diff --git a/src/gitea_codex_bot/main.py b/src/gitea_codex_bot/main.py index 7cdd027..befc36a 100644 --- a/src/gitea_codex_bot/main.py +++ b/src/gitea_codex_bot/main.py @@ -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", diff --git a/src/gitea_codex_bot/services/commands.py b/src/gitea_codex_bot/services/commands.py index 6c64cf3..02d2b9d 100644 --- a/src/gitea_codex_bot/services/commands.py +++ b/src/gitea_codex_bot/services/commands.py @@ -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) diff --git a/tests/test_commands.py b/tests/test_commands.py index a7a7ec0..dc1009b 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -26,3 +26,16 @@ def test_parse_fix_branch() -> None: def test_invalid_command_returns_none() -> None: assert parse_command("hello") is None + + +def test_parse_review_command_for_bot_username_alias() -> None: + cmd = parse_command("@codex-bot review", aliases={"codex", "codex-bot"}) + assert cmd is not None + assert cmd.name == "review" + + +def test_parse_review_command_for_custom_alias() -> None: + cmd = parse_command("@review-buddy review tests", aliases={"codex", "review-buddy"}) + assert cmd is not None + assert cmd.name == "review" + assert cmd.mode == "tests" diff --git a/tests/test_config.py b/tests/test_config.py index e1a5a6b..3f2edc2 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -11,3 +11,16 @@ def test_codex_auth_defaults_to_api_key_mode() -> None: settings = get_settings() assert settings.codex_auth_mode == "api_key" assert settings.codex_auth_json_path is None + + +def test_bot_command_aliases_include_codex_and_username() -> None: + settings = get_settings() + assert settings.bot_command_aliases == {"codex", "codex-bot"} + + +def test_bot_command_aliases_include_custom_mentions(monkeypatch) -> None: + monkeypatch.setenv("GITEA_BOT_MENTIONS", "@review-buddy,helper-bot") + get_settings.cache_clear() + + settings = get_settings() + assert settings.bot_command_aliases == {"codex", "codex-bot", "review-buddy", "helper-bot"} diff --git a/tests/test_webhook.py b/tests/test_webhook.py index 10afba5..07a4af0 100644 --- a/tests/test_webhook.py +++ b/tests/test_webhook.py @@ -93,6 +93,43 @@ def test_webhook_accepts_review_and_queues(monkeypatch) -> None: assert queued.trigger_comment_body == "@codex review security" +def test_webhook_accepts_review_for_bot_username_alias(monkeypatch) -> None: + posted_comments: list[str] = [] + + def _post_issue_comment(self, repo: str, pr_number: int, body: str) -> int: + posted_comments.append(body) + return 100 + + monkeypatch.setattr("gitea_codex_bot.services.gitea.GiteaClient.post_issue_comment", _post_issue_comment) + monkeypatch.setattr( + "gitea_codex_bot.services.gitea.GiteaClient.get_pull_request", + lambda *_args, **_kwargs: type("PR", (), {"head_sha": "abcdef123"})(), + ) + monkeypatch.setattr("gitea_codex_bot.services.gitea.GiteaClient.get_file_content", lambda *_args, **_kwargs: None) + + client = TestClient(app) + payload_obj = _payload("@codex-bot review security", username="alice", comment_id=311) + raw = json.dumps(payload_obj).encode() + + response = client.post( + "/webhook/gitea", + content=raw, + headers={ + "X-Gitea-Event": "issue_comment", + "X-Gitea-Delivery": "d-2-alias", + "X-Gitea-Signature": _sign(raw), + "Content-Type": "application/json", + }, + ) + assert response.status_code == 200 + assert response.json()["status"] == "queued" + assert posted_comments + session_factory = get_session_factory() + with session_factory() as session: + queued = session.execute(select(ReviewJob).where(ReviewJob.trigger_comment_id == 311)).scalar_one() + assert queued.trigger_comment_body == "@codex-bot review security" + + def test_webhook_uses_latest_pr_head_sha_when_config_lookup_fails(monkeypatch) -> None: posted_comments: list[str] = []