Repository navigation
fix(gitea): use user-facing base URL for links published in comments - #2617
Conversation
GiteaProvider derived both its API endpoint and every user-facing link from the single gitea.url setting, so on self-hosted Gitea/Forgejo deployments where the API is reached via an internal address, every link PR-Agent published pointed at the internal address. Mirror GithubProvider's separation of the two concerns: - add base_url_html, resolved from the new optional gitea.web_url setting, then derived from pr.html_url (built by Gitea/Forgejo from its external ROOT_URL), falling back to gitea.url so existing single-URL deployments are unaffected - get_line_link() now builds links from base_url_html; base_url stays in use for the API host and clone URLs - get_pr_url() returns pr.html_url when available instead of the raw (possibly API-form) webhook URL Fixes The-PR-Agent#2612
PR Summary by QodoFix GiteaProvider to publish user-facing links in comments
AI Description
Diagram
High-Level Assessment
Files changed (4)
|
Code Review by Qodo
1.
|
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Went through this against #2612. The shape is right and it does solve the reported problem. A few inline notes, mostly about web_url not reaching every published link. Nothing needing a redesign.
| return web_url | ||
| pr_html_url = getattr(self.pr, "html_url", "") if self.pr else "" | ||
| pr_url_suffix = f"/{self.owner}/{self.repo}/pulls/{self.pr_number}" | ||
| if pr_html_url and pr_html_url.endswith(pr_url_suffix): |
There was a problem hiding this comment.
This ranks the derived value above GITEA.URL. On an instance where the operator set GITEA__URL correctly but the server's own ROOT_URL is still the default, pr.html_url is built from ROOT_URL and does end with this suffix, so the match succeeds and get_line_link() moves from the working configured URL to an unbrowsable one.
Narrow in practice, since such a deployment already had broken links elsewhere and GITEA__WEB_URL fixes it. Might be worth a docs line saying instances with a stale ROOT_URL now need to set it, or preferring base_url when it was explicitly configured.
There was a problem hiding this comment.
Good catch — fixed in ee68be9. _resolve_base_url_html() now prefers an explicitly configured GITEA.URL over the pr.html_url derivation, so an instance whose server ROOT_URL is still a stale default keeps its working configured links. Resolution order is now: GITEA.WEB_URL → configured GITEA.URL → derived from pr.html_url → base_url. gitea.md also got a short paragraph spelling out the order and when to set GITEA__WEB_URL explicitly.
| # pr.html_url is the user-facing page; self.pr_url may carry the API-form | ||
| # URL (e.g. when the run was triggered by a webhook payload). | ||
| if self.pr and getattr(self.pr, "html_url", ""): | ||
| return self.pr.html_url |
There was a problem hiding this comment.
base_url_html is not consulted here, so GITEA.WEB_URL does not reach links built from get_pr_url(). Someone who sets it precisely because ROOT_URL is wrong still gets a ROOT_URL derived link in the /describe "PR Description updated to latest commit" comment (pr_description.py:205) and in the changelog link (pr_update_changelog.py:122, on by default).
There was a problem hiding this comment.
Fixed in ee68be9: get_pr_url() now builds the page URL from base_url_html ({base_url_html}/{owner}/{repo}/pulls/{n}), so a configured GITEA.WEB_URL reaches the /describe update comment, the changelog link, and the incremental-review commit link in pr_reviewer.py.
I checked every call site before switching: pr_description.py:205, pr_update_changelog.py:122 and pr_reviewer.py:280 are all user-facing output, and repo_context.py uses the value only as a cache key. No API or clone path goes through get_pr_url() — the API host and _prepare_clone_url_with_token/get_git_repo_url still use base_url directly.
|
|
||
| def get_pr_url(self) -> str: | ||
| # pr.html_url is the user-facing page; self.pr_url may carry the API-form | ||
| # URL (e.g. when the run was triggered by a webhook payload). |
There was a problem hiding this comment.
Gitea sets url and html_url to the same value on the PR payload, so self.pr_url should not carry the API form here. The API-shaped default in the new fixture would not survive _parse_pr_url either, since it strips /api/v1 then checks path_parts[2] == 'pulls', which sees the repo name and raises. The change looks harmless, but the stated rationale may not be the real one.
There was a problem hiding this comment.
You're right on both counts. I verified against the gitea.com API that url and html_url on the PR payload are set identically to the user-facing page, and an API-shaped URL indeed wouldn't survive _parse_pr_url (after stripping /api/v1 the pulls segment check sees the repo name and raises). In ee68be9 the fixture default now uses the realistic user-facing form, and the get_pr_url() comment states the actual rationale.
| # Optional user-facing base URL used to build links published in comments, for | ||
| # self-hosted instances where the API is reached via an internal address but | ||
| # users browse a different external URL. Defaults to `url`. | ||
| # web_url = "" |
There was a problem hiding this comment.
One caveat on offering this as a per repo key: apply_repo_settings constructs and caches the provider before merging the repo's .pr_agent.toml, and /describe, /review and /improve reuse that cached instance, so web_url set in a repo's .pr_agent.toml will not apply for those commands while tools that build a fresh provider do pick it up.
Inherited rather than introduced, since base_url has the same staleness. The env var form in gitea.md is unaffected.
There was a problem hiding this comment.
Agreed — inherited behavior, since base_url has the same staleness, so no code change here. I documented it instead: the comment on web_url in configuration.toml now notes that a per-repo .pr_agent.toml override does not apply to /describe, /review and /improve because the provider is constructed and cached before repo settings are merged, and recommends setting it globally or via GITEA__WEB_URL.
| def _provider(pr_html_url="", pr_url="http://forgejo:3000/api/v1/repos/owner/repo/pulls/4"): | ||
| from pr_agent.git_providers.gitea_provider import GiteaProvider | ||
|
|
||
| provider = GiteaProvider.__new__(GiteaProvider) |
There was a problem hiding this comment.
Since every test builds the provider with __new__, line 111 (self.base_url_html = self._resolve_base_url_html()) has no coverage and removing it would leave the suite green. Having test_get_line_link_uses_user_facing_base_url set provider.base_url_html = provider._resolve_base_url_html() instead of hardcoding it would cover the wiring by proxy.
There was a problem hiding this comment.
Done in ee68be9: test_get_line_link_uses_user_facing_base_url now sets provider.base_url_html = provider._resolve_base_url_html() under patched settings instead of hardcoding it, and the two new get_pr_url tests do the same — so the line-111 wiring is covered by proxy and the suite goes red if the assignment is dropped.
…url via base_url_html, fix fixture, cover resolver - _resolve_base_url_html now prefers an explicitly configured GITEA.URL over the pr.html_url derivation, so instances whose server ROOT_URL is a stale default keep their working configured links - get_pr_url() builds the user-facing page URL from base_url_html, so GITEA.WEB_URL reaches the /describe update comment, the changelog link and the incremental-review commit link; verified no API/clone call sites use get_pr_url (those paths use base_url directly) - drop the API-form pr_url rationale: Gitea sets url and html_url identically on the PR payload, and _parse_pr_url rejects the API form anyway; fixture now uses the realistic user-facing form - tests resolve base_url_html through _resolve_base_url_html() instead of hardcoding it, covering the __init__ wiring by proxy - document the GITEA__WEB_URL resolution order, the ROOT_URL caveat and the per-repo .pr_agent.toml limitation for cached providers
|
Thanks for the careful review, @IsmaelMartinez — all five notes are addressed in ee68be9:
|
|
Code review by qodo was updated up to the latest commit ee68be9 |
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Thanks for turning all of that around so quickly. Re-checked the new resolution order and spotted one thing worth another pass, noted inline.
| web_url = (get_settings().get("GITEA.WEB_URL", "") or "").rstrip("/") | ||
| if web_url: | ||
| return web_url | ||
| if get_settings().get("GITEA.URL", None): |
There was a problem hiding this comment.
This does not distinguish an operator-set value from the shipped default. configuration.toml:310 defines url = "https://gitea.com", so get_settings().get("GITEA.URL", None) is truthy on every deployment, this branch always returns, and the pr.html_url derivation below is unreachable in production.
The new tests pass because _settings() omits GITEA.URL unless a test supplies it, which no real run does.
The effect is that this issue's own scenario regresses: an operator whose GITEA__URL is the internal API address gets that address back in links unless they also set GITEA__WEB_URL, which is what the derivation was there to avoid. Preferring the configured value only when it differs from the shipped default would keep both behaviours.
Can you fix this one? I hope it helps.
There was a problem hiding this comment.
Good catch — fixed in 2fb2227. You're right on both counts: configuration.toml always ships url = "https://gitea.com", so the truthiness check made the derivation unreachable, and the _settings() helper omitting GITEA.URL is exactly why the suite stayed green against a condition no real run has.
Done as you suggested: the configured value now only wins when it differs from the shipped default. The resolver rstrips and compares GITEA.URL against a new DEFAULT_GITEA_URL module constant, which __init__ also uses as its fallback so the two can't drift. Precedence: GITEA.WEB_URL → non-default GITEA.URL → derived from pr.html_url → base_url. The operator with an internal GITEA__URL and no GITEA__WEB_URL gets the html_url derivation again; an explicitly configured URL still beats a stale server ROOT_URL.
Two regression tests added, this time supplying GITEA.URL the way a real run does: the exact shipped default, and a trailing-slash variant — neither shadows the derivation anymore. The configuration.toml comment and gitea.md now state the new precedence. tests/unittest/test_gitea_provider.py: 36 passed.
…ows pr.html_url derivation
|
Code review by qodo was updated up to the latest commit 2fb2227 |
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Thanks, this is a clean fix for self-hosted Gitea where the API base and the browsable base differ. Merging.
Fixes #2612
Problem
GiteaProviderderives both its API endpoint and every user-facing link from the singlegitea.urlsetting. On self-hosted Gitea/Forgejo deployments where the API is reached via an internal address (Docker service name, cluster DNS) but users browse a different external address, every link PR-Agent publishes points at the internal address and is unclickable:get_line_link()built code links from the API base URLget_pr_url()returned the raw (possibly API-form) webhook URLFix
Mirrors the separation
GithubProvideralready has (base_urlvsbase_url_html), following the approach suggested in the issue:base_url_htmlattribute, resolved by_resolve_base_url_html():gitea.web_urlsetting (also documented inconfiguration.tomland the Gitea installation guide, env varGITEA__WEB_URL)pr.html_url, which Gitea/Forgejo builds from its own externalROOT_URL(zero-config for correctly configured instances)gitea.url, so existing single-URL deployments are unaffectedget_line_link()now builds links frombase_url_html;base_urlstays in use for the API host,get_git_repo_url()and clone URL preparation (those should stay internal)get_pr_url()returnspr.html_urlwhen available, falling back to the raw PR URL otherwiseTests
Added 7 unit tests to
tests/unittest/test_gitea_provider.pycovering:web_urlprecedence, derivation frompr.html_url(the reverse-proxy scenario from the issue), fallback tobase_urlfor the issue flow and for missing/foreignhtml_url, all threeget_line_link()variants, and bothget_pr_url()paths.Ruff (repo config) reports only pre-existing findings in untouched regions of the file; all lines this PR touches are clean.