[feat]. Add username and custom bot mention tags
This commit is contained in:
@@ -4,6 +4,8 @@ GITEA_BASE_URL=https://gitea.reversed.dev
|
|||||||
# Bot account token used to read PRs and write comments.
|
# Bot account token used to read PRs and write comments.
|
||||||
GITEA_TOKEN=replace
|
GITEA_TOKEN=replace
|
||||||
GITEA_BOT_USERNAME=codex-bot
|
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.
|
# Shared secret configured on the Gitea webhook.
|
||||||
GITEA_WEBHOOK_SECRET=replace
|
GITEA_WEBHOOK_SECRET=replace
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ Webhook-driven PR review bot for Gitea.
|
|||||||
|
|
||||||
- Handles `issue_comment` and `pull_request_comment` events.
|
- Handles `issue_comment` and `pull_request_comment` events.
|
||||||
- Verifies `X-Gitea-Signature` HMAC (`sha256`).
|
- Verifies `X-Gitea-Signature` HMAC (`sha256`).
|
||||||
- Triggers on `@codex review`, `@codex rerun`, `@codex explain`, `@codex fix`, `@codex ignore`.
|
- Triggers on `@codex ...`, `@<GITEA_BOT_USERNAME> ...`, plus optional custom aliases from `GITEA_BOT_MENTIONS`.
|
||||||
- Ignores bot-authored comments.
|
- Ignores bot-authored comments.
|
||||||
- Enforces strict repository allowlist (`ALLOWED_REPOS`).
|
- Enforces strict repository allowlist (`ALLOWED_REPOS`).
|
||||||
- Deduplicates webhook deliveries/comments in DB.
|
- 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_API_KEY` (required when `CODEX_AUTH_MODE=api_key`, optional when `CODEX_AUTH_MODE=chatgpt`)
|
||||||
- `OPENAI_PROJECT_ID`
|
- `OPENAI_PROJECT_ID`
|
||||||
- `OPENAI_ORG_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_MODE` (`api_key` default, or `chatgpt`)
|
||||||
- `CODEX_AUTH_JSON_PATH` (custom host path to `auth.json`; defaults to `~/.codex/auth.json` in `chatgpt` mode)
|
- `CODEX_AUTH_JSON_PATH` (custom host path to `auth.json`; defaults to `~/.codex/auth.json` in `chatgpt` mode)
|
||||||
- `DATABASE_URL` (overrides composed DB URL)
|
- `DATABASE_URL` (overrides composed DB URL)
|
||||||
|
|||||||
2
TODO.md
2
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`: 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.
|
- [ ] `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).
|
- [ ] `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)
|
### P2 (Nice to Have)
|
||||||
- [x] `FEATURE`: Add a note line at the end of comments to show model tokens used and such.
|
- [x] `FEATURE`: Add a note line at the end of comments to show model tokens used and such.
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ class Settings(BaseSettings):
|
|||||||
gitea_base_url: str = Field(alias="GITEA_BASE_URL")
|
gitea_base_url: str = Field(alias="GITEA_BASE_URL")
|
||||||
gitea_token: SecretStr = Field(alias="GITEA_TOKEN")
|
gitea_token: SecretStr = Field(alias="GITEA_TOKEN")
|
||||||
gitea_bot_username: str = Field(alias="GITEA_BOT_USERNAME")
|
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")
|
gitea_webhook_secret: SecretStr = Field(alias="GITEA_WEBHOOK_SECRET")
|
||||||
|
|
||||||
openai_api_key: SecretStr | None = Field(default=None, alias="OPENAI_API_KEY")
|
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(",")]
|
values = [item.strip() for item in self.allowed_repos.split(",")]
|
||||||
return {value for value in values if value}
|
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)
|
@lru_cache(maxsize=1)
|
||||||
def get_settings() -> Settings:
|
def get_settings() -> Settings:
|
||||||
|
|||||||
@@ -422,7 +422,7 @@ async def gitea_webhook(
|
|||||||
return {"accepted": False, "reason": "bot comment ignored"}
|
return {"accepted": False, "reason": "bot comment ignored"}
|
||||||
|
|
||||||
comment_body = str(payload.get("comment", {}).get("body", "")).strip()
|
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:
|
if not parsed_command:
|
||||||
logger.info(
|
logger.info(
|
||||||
"Webhook ignored: no @codex review command repo=%s pr=%s comment_id=%s sender=%s",
|
"Webhook ignored: no @codex review command repo=%s pr=%s comment_id=%s sender=%s",
|
||||||
|
|||||||
@@ -1,19 +1,25 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
from collections.abc import Iterable
|
||||||
|
|
||||||
from gitea_codex_bot.types import ParsedCommand
|
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()
|
stripped = body.strip()
|
||||||
match = COMMAND_RE.match(stripped)
|
match = COMMAND_RE.match(stripped)
|
||||||
if not match:
|
if not match:
|
||||||
return None
|
return None
|
||||||
name = match.group(1).lower()
|
command_alias = match.group(1).lstrip("@").lower()
|
||||||
rest = match.group(2).strip()
|
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]
|
tokens = [token for token in rest.split() if token]
|
||||||
|
|
||||||
parsed = ParsedCommand(name=name, raw=stripped, arguments=tokens)
|
parsed = ParsedCommand(name=name, raw=stripped, arguments=tokens)
|
||||||
|
|||||||
@@ -26,3 +26,16 @@ def test_parse_fix_branch() -> None:
|
|||||||
|
|
||||||
def test_invalid_command_returns_none() -> None:
|
def test_invalid_command_returns_none() -> None:
|
||||||
assert parse_command("hello") is 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"
|
||||||
|
|||||||
@@ -11,3 +11,16 @@ def test_codex_auth_defaults_to_api_key_mode() -> None:
|
|||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
assert settings.codex_auth_mode == "api_key"
|
assert settings.codex_auth_mode == "api_key"
|
||||||
assert settings.codex_auth_json_path is None
|
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"}
|
||||||
|
|||||||
@@ -93,6 +93,43 @@ def test_webhook_accepts_review_and_queues(monkeypatch) -> None:
|
|||||||
assert queued.trigger_comment_body == "@codex review security"
|
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:
|
def test_webhook_uses_latest_pr_head_sha_when_config_lookup_fails(monkeypatch) -> None:
|
||||||
posted_comments: list[str] = []
|
posted_comments: list[str] = []
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user