Don't create self-references in merged PRs (#36490)

Fixes: https://github.com/go-gitea/gitea/issues/36488
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
silverwind
2026-01-30 21:12:24 +01:00
committed by GitHub
parent 2d1306291b
commit a16ca3c57c
2 changed files with 79 additions and 0 deletions

View File

@@ -89,6 +89,24 @@ func issueAddTime(ctx context.Context, issue *issues_model.Issue, doer *user_mod
return err
}
// isSelfReference checks if a commit is the merge commit of the PR it references.
// This prevents creating self-referencing timeline entries when a PR merge commit
// contains a reference to its own PR number in the commit message.
func isSelfReference(ctx context.Context, issue *issues_model.Issue, commitSHA string) bool {
if !issue.IsPull {
return false
}
if err := issue.LoadPullRequest(ctx); err != nil {
if !issues_model.IsErrPullRequestNotExist(err) {
log.Error("LoadPullRequest: %v", err)
}
return false
}
return issue.PullRequest.MergedCommitID == commitSHA
}
// getIssueFromRef returns the issue referenced by a ref. Returns a nil *Issue
// if the provided ref references a non-existent issue.
func getIssueFromRef(ctx context.Context, repo *repo_model.Repository, index int64) (*issues_model.Issue, error) {
@@ -158,6 +176,11 @@ func UpdateIssuesCommit(ctx context.Context, doer *user_model.User, repo *repo_m
continue
}
// Skip self-references: if this commit is the merge commit of the PR it references
if isSelfReference(ctx, refIssue, c.Sha1) {
continue
}
message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, html.EscapeString(repo.Link()), html.EscapeString(url.PathEscape(c.Sha1)), html.EscapeString(strings.SplitN(c.Message, "\n", 2)[0]))
if err = CreateRefComment(ctx, doer, refRepo, refIssue, message, c.Sha1); err != nil {
if errors.Is(err, user_model.ErrBlockedUser) {