@@ -298,3 +298,63 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
298298 unittest .AssertNotExistsBean (t , issueBean , "is_closed=1" )
299299 unittest .CheckConsistencyFor (t , & activities_model.Action {})
300300}
301+
302+ func TestUpdateIssuesCommit_SelfReference (t * testing.T ) {
303+ assert .NoError (t , unittest .PrepareTestDatabase ())
304+ user := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
305+
306+ // Test that a PR merge commit that references its own PR does not create a self-reference comment
307+ // PR #2 (issue_id=2) has merged_commit_id: 1a8823cd1a9549fde083f992f6b9b87a7ab74fb3
308+ pushCommits := []* repository.PushCommit {
309+ {
310+ Sha1 : "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3" , // This is the merge commit for PR #2
311+ CommitterEmail : "user2@example.com" ,
312+ CommitterName : "User Two" ,
313+ AuthorEmail : "user2@example.com" ,
314+ AuthorName : "User Two" ,
315+ Message : "Merge pull request (#2) from branch1" , // References its own PR
316+ },
317+ }
318+
319+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
320+
321+ // This comment should NOT be created (self-reference)
322+ selfRefCommentBean := & issues_model.Comment {
323+ Type : issues_model .CommentTypeCommitRef ,
324+ CommitSHA : "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3" ,
325+ PosterID : user .ID ,
326+ IssueID : 2 , // PR #2 references itself
327+ }
328+
329+ unittest .AssertNotExistsBean (t , selfRefCommentBean )
330+ assert .NoError (t , UpdateIssuesCommit (t .Context (), user , repo , pushCommits , repo .DefaultBranch ))
331+ // The self-reference comment should still not exist
332+ unittest .AssertNotExistsBean (t , selfRefCommentBean )
333+ unittest .CheckConsistencyFor (t , & activities_model.Action {})
334+
335+ // Test that the same merge commit can still create references to other issues
336+ pushCommits2 := []* repository.PushCommit {
337+ {
338+ Sha1 : "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3" ,
339+ CommitterEmail : "user2@example.com" ,
340+ CommitterName : "User Two" ,
341+ AuthorEmail : "user2@example.com" ,
342+ AuthorName : "User Two" ,
343+ Message : "Merge pull request (#2) - also fixes #1" , // References a different issue
344+ },
345+ }
346+
347+ // This comment SHOULD be created (reference to a different issue)
348+ otherRefCommentBean := & issues_model.Comment {
349+ Type : issues_model .CommentTypeCommitRef ,
350+ CommitSHA : "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3" ,
351+ PosterID : user .ID ,
352+ IssueID : 1 , // References issue #1
353+ }
354+
355+ unittest .AssertNotExistsBean (t , otherRefCommentBean )
356+ assert .NoError (t , UpdateIssuesCommit (t .Context (), user , repo , pushCommits2 , repo .DefaultBranch ))
357+ // The reference to issue #1 should exist
358+ unittest .AssertExistsAndLoadBean (t , otherRefCommentBean )
359+ unittest .CheckConsistencyFor (t , & activities_model.Action {})
360+ }
0 commit comments