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

@@ -298,3 +298,59 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
unittest.AssertNotExistsBean(t, issueBean, "is_closed=1")
unittest.CheckConsistencyFor(t, &activities_model.Action{})
}
func TestUpdateIssuesCommit_SelfReference(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
// Test that a PR merge commit that references its own PR does not create a self-reference comment
// PR #2 (issue_id=2) has merged_commit_id: 1a8823cd1a9549fde083f992f6b9b87a7ab74fb3
pushCommits := []*repository.PushCommit{
{
Sha1: "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3",
CommitterEmail: "user2@example.com",
CommitterName: "User Two",
AuthorEmail: "user2@example.com",
AuthorName: "User Two",
Message: "Merge pull request 'issue2' (#2) from branch1 into master",
},
}
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
selfRefCommentBean := &issues_model.Comment{
Type: issues_model.CommentTypeCommitRef,
CommitSHA: "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3",
PosterID: user.ID,
IssueID: 2,
}
unittest.AssertNotExistsBean(t, selfRefCommentBean)
assert.NoError(t, UpdateIssuesCommit(t.Context(), user, repo, pushCommits, repo.DefaultBranch))
unittest.AssertNotExistsBean(t, selfRefCommentBean)
unittest.CheckConsistencyFor(t, &activities_model.Action{})
// Test that normal commit references are still created
pushCommits2 := []*repository.PushCommit{
{
Sha1: "abcdef9876543210",
CommitterEmail: "user2@example.com",
CommitterName: "User Two",
AuthorEmail: "user2@example.com",
AuthorName: "User Two",
Message: "Fix bug, refs #1",
},
}
otherRefCommentBean := &issues_model.Comment{
Type: issues_model.CommentTypeCommitRef,
CommitSHA: "abcdef9876543210",
PosterID: user.ID,
IssueID: 1,
}
unittest.AssertNotExistsBean(t, otherRefCommentBean)
assert.NoError(t, UpdateIssuesCommit(t.Context(), user, repo, pushCommits2, repo.DefaultBranch))
unittest.AssertExistsAndLoadBean(t, otherRefCommentBean)
unittest.CheckConsistencyFor(t, &activities_model.Action{})
}