fix(pull): link the pending pull request when reopen is blocked (#40300)

When reopening or creating a pull request is blocked by an existing one
between the same branches, the error now links to it. Previously the
reopen error only showed the number and the create error didn't mention
it. Retargeting, which already linked it, now shares the same message.

The link is built in Go instead of in the translations, under new
translation keys, so other locales fall back to English instead of
garbling the link until they are retranslated.

Before:
<img width="800" height="597" alt="before"
src="https://github.com/user-attachments/assets/9f65d65e-0c62-4f1f-93bb-9021437b0d37"
/>

After:
<img width="800" height="600" alt="after"
src="https://github.com/user-attachments/assets/2aca79d2-473d-4c4d-8435-8d2c00ded00e"
/>

Fixes: https://github.com/go-gitea/gitea/issues/33800

Assisted-by: Claude Code:claude-opus-5-5

---------

Signed-off-by: Corey Estey <147874433+kernelius-glitch@users.noreply.github.com>
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
Corey Esteyandsilverwind authored and GitHub committed 2026-11-30 16:02:37 +00:00
1 parent 0aafa878e5
commit 95032ff039
4 files changed
+25 -10

No files matched your search

+2 -2
View File
@@ -1748,7 +1748,6 @@
"repo.pulls.desc": "Enable pull requests and code reviews.",
"repo.pulls.new": "New Pull Request",
"repo.pulls.new.description": "Discuss and review the changes in this comparison with others.",
"repo.pulls.new.already_existed": "A pull request between these branches already exists",
"repo.pulls.edit.already_changed": "Unable to save changes to the pull request. It appears the content has already been changed by another user. Please refresh the page and try editing again to avoid overwriting their changes.",
"repo.pulls.view": "View Pull Request",
"repo.pulls.compare_changes": "New Pull Request",
@@ -1778,7 +1777,7 @@
"repo.pulls.no_common_history": "These branches do not share a common merge base. Select a different base or compare branch.",
"repo.pulls.nothing_to_compare_have_tag": "There are no differences to show between the selected branches or tags.",
"repo.pulls.nothing_to_compare_and_allow_empty_pr": "These branches are equal. This PR will be empty.",
"repo.pulls.has_pull_request": "A pull request between these branches already exists: <a href=\"%[1]s\">%[2]s#%[3]d</a>",
"repo.pulls.has_pull_request_link": "A pull request between these branches already exists: %s",
"repo.pulls.create": "Create Pull Request",
"repo.pulls.title_desc": "wants to merge %[1]d commits from <code>%[2]s</code> into <code id=\"branch_target\">%[3]s</code>",
"repo.pulls.merged_title_desc": "merged %[1]d commits from <code>%[2]s</code> into <code>%[3]s</code> %[4]s",
@@ -1874,6 +1873,7 @@
"repo.pulls.push_rejected": "Push Failed: The push was rejected. Review the Git Hooks for this repository.",
"repo.pulls.push_rejected_summary": "Full Rejection Message",
"repo.pulls.push_rejected_no_message": "Push Failed: The push was rejected but there was no remote message. Review the Git Hooks for this repository.",
"repo.pulls.open_unmerged_pull_exists_link": "You cannot perform a reopen operation because there is a pending pull request (%s) with identical properties.",
"repo.pulls.open_unmerged_pull_exists": "You cannot perform a reopen operation because there is a pending pull request (#%d) with identical properties.",
"repo.pulls.status_checking": "Some checks haven't completed yet",
"repo.pulls.status_checks_success": "All checks were successful",
+2 -1
View File
@@ -15,6 +15,7 @@ import (
"gitea.dev/models/perm"
"gitea.dev/models/renderhelper"
user_model "gitea.dev/models/user"
"gitea.dev/modules/htmlutil"
"gitea.dev/modules/log"
"gitea.dev/modules/markup/markdown"
"gitea.dev/modules/setting"
@@ -92,7 +93,7 @@ func NewComment(ctx *context.Context) {
}
if branchOtherUnmergedPR != nil {
ctx.Flash.Error(ctx.Tr("repo.pulls.open_unmerged_pull_exists", branchOtherUnmergedPR.Index))
ctx.Flash.Error(ctx.Tr("repo.pulls.open_unmerged_pull_exists_link", htmlutil.HTMLFormat(`<a href="%s/pulls/%d">#%d</a>`, ctx.Repo.RepoLink, branchOtherUnmergedPR.Index, branchOtherUnmergedPR.Index)))
} else {
// sync ref of PR <refs/pulls/pr_index/head> in base repo for a reopened PR
if pull.Flow == issues_model.PullRequestFlowGithub {
+8 -5
View File
@@ -9,11 +9,9 @@ import (
"bytes"
"errors"
"fmt"
"html"
"html/template"
"net/http"
"slices"
"strconv"
"strings"
"time"
@@ -30,6 +28,7 @@ import (
"gitea.dev/modules/git"
"gitea.dev/modules/git/gitcmd"
"gitea.dev/modules/glob"
"gitea.dev/modules/htmlutil"
issue_template "gitea.dev/modules/issue/template"
"gitea.dev/modules/log"
"gitea.dev/modules/setting"
@@ -1290,7 +1289,7 @@ func CompareAndPullRequestPost(ctx *context.Context) {
return
}
if pr != nil {
ctx.JSONError(ctx.Tr("repo.pulls.new.already_existed"))
ctx.JSONError(existingPullRequestMessage(ctx, pr.Index))
return
}
@@ -1401,6 +1400,11 @@ func DownloadPullDiffOrPatch(ctx *context.Context, patch bool) {
}
}
func existingPullRequestMessage(ctx *context.Context, index int64) template.HTML {
link := htmlutil.HTMLFormat(`<a href="%s/pulls/%d">%s#%d</a>`, ctx.Repo.RepoLink, index, ctx.Repo.Repository.FullName(), index)
return ctx.Tr("repo.pulls.has_pull_request_link", link)
}
// UpdatePullRequestTarget change pull request's target branch
func UpdatePullRequestTarget(ctx *context.Context) {
issue := GetActionIssue(ctx)
@@ -1438,8 +1442,7 @@ func UpdatePullRequestTarget(ctx *context.Context) {
"user_error": errorMessage,
})
case errors.As(err, &prExistsErr):
RepoRelPath := ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name
errorMessage := ctx.Tr("repo.pulls.has_pull_request", html.EscapeString(ctx.Repo.RepoLink+"/pulls/"+strconv.FormatInt(prExistsErr.IssueID, 10)), html.EscapeString(RepoRelPath), prExistsErr.IssueID) // FIXME: Creates url inside locale string
errorMessage := existingPullRequestMessage(ctx, prExistsErr.IssueID)
ctx.Flash.Error(errorMessage)
ctx.JSON(http.StatusConflict, map[string]any{
+13 -2
View File
@@ -158,10 +158,13 @@ func TestPullCreate(t *testing.T) {
// test create the pull request again and it should fail now
link := "/user2/repo1/compare/master...user1/repo1:master"
req := NewRequestWithValues(t, "POST", link, map[string]string{
req := NewRequestWithValues(t, "POST", link+"?lang=de-DE", map[string]string{
"title": "This is a pull title",
})
session.MakeRequest(t, req, http.StatusBadRequest)
resp = session.MakeRequest(t, req, http.StatusBadRequest)
errorMessage := test.ParseJSONError(resp.Body.Bytes()).ErrorMessage
assert.Contains(t, errorMessage, `<a href="`+url+`">user2/repo1#`+path.Base(url)+`</a>`)
assert.NotContains(t, errorMessage, "%!")
// check .diff can be accessed and matches performed change
req = NewRequest(t, "GET", url+".diff")
@@ -177,6 +180,14 @@ func TestPullCreate(t *testing.T) {
assert.Regexp(t, "diff", resp.Body)
assert.Regexp(t, `Subject: \[PATCH\] Update README.md`, resp.Body)
assert.NotRegexp(t, "diff.*diff", resp.Body) // not two diffs, just one
testIssueClose(t, session, "user2", "repo1", path.Base(url))
newURL := test.RedirectURL(session.MakeRequest(t, NewRequestWithValues(t, "POST", link, map[string]string{"title": "This is a pull title"}), http.StatusOK))
req = NewRequestWithValues(t, "POST", url+"/comments?lang=de-DE", map[string]string{"status": "reopen"})
session.MakeRequest(t, req, http.StatusOK)
errorMessage = session.GetCookieFlashMessage().ErrorMsg
assert.Contains(t, errorMessage, `<a href="`+newURL+`">#`+path.Base(newURL)+`</a>`)
assert.NotContains(t, errorMessage, "%!")
})
}