2019-11-18 16:13:07 +03:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2019-11-18 16:13:07 +03:00
2022-06-13 12:37:59 +03:00
package issues_test
2019-11-18 16:13:07 +03:00
import (
"fmt"
"testing"
2021-09-19 14:49:59 +03:00
"code.gitea.io/gitea/models/db"
2022-06-13 12:37:59 +03:00
issues_model "code.gitea.io/gitea/models/issues"
2021-12-10 04:27:50 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-12 17:36:47 +03:00
"code.gitea.io/gitea/models/unittest"
2021-11-24 12:49:20 +03:00
user_model "code.gitea.io/gitea/models/user"
2019-11-18 16:13:07 +03:00
"code.gitea.io/gitea/modules/references"
"github.com/stretchr/testify/assert"
)
func TestXRef_AddCrossReferences ( t * testing . T ) {
2021-11-12 17:36:47 +03:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2019-11-18 16:13:07 +03:00
// Issue #1 to test against
itarget := testCreateIssue ( t , 1 , 2 , "title1" , "content1" , false )
// PR to close issue #1
content := fmt . Sprintf ( "content2, closes #%d" , itarget . Index )
pr := testCreateIssue ( t , 1 , 2 , "title2" , content , true )
2022-08-16 05:22:25 +03:00
ref := unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : itarget . ID , RefIssueID : pr . ID , RefCommentID : 0 } )
2022-06-13 12:37:59 +03:00
assert . Equal ( t , issues_model . CommentTypePullRef , ref . Type )
2019-11-18 16:13:07 +03:00
assert . Equal ( t , pr . RepoID , ref . RefRepoID )
2021-06-07 08:27:09 +03:00
assert . True ( t , ref . RefIsPull )
2019-11-18 16:13:07 +03:00
assert . Equal ( t , references . XRefActionCloses , ref . RefAction )
// Comment on PR to reopen issue #1
content = fmt . Sprintf ( "content2, reopens #%d" , itarget . Index )
c := testCreateComment ( t , 1 , 2 , pr . ID , content )
2022-08-16 05:22:25 +03:00
ref = unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : itarget . ID , RefIssueID : pr . ID , RefCommentID : c . ID } )
2022-06-13 12:37:59 +03:00
assert . Equal ( t , issues_model . CommentTypeCommentRef , ref . Type )
2019-11-18 16:13:07 +03:00
assert . Equal ( t , pr . RepoID , ref . RefRepoID )
2021-06-07 08:27:09 +03:00
assert . True ( t , ref . RefIsPull )
2019-11-18 16:13:07 +03:00
assert . Equal ( t , references . XRefActionReopens , ref . RefAction )
// Issue mentioning issue #1
content = fmt . Sprintf ( "content3, mentions #%d" , itarget . Index )
i := testCreateIssue ( t , 1 , 2 , "title3" , content , false )
2022-08-16 05:22:25 +03:00
ref = unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : itarget . ID , RefIssueID : i . ID , RefCommentID : 0 } )
2022-06-13 12:37:59 +03:00
assert . Equal ( t , issues_model . CommentTypeIssueRef , ref . Type )
2019-11-18 16:13:07 +03:00
assert . Equal ( t , pr . RepoID , ref . RefRepoID )
2021-06-07 08:27:09 +03:00
assert . False ( t , ref . RefIsPull )
2019-11-18 16:13:07 +03:00
assert . Equal ( t , references . XRefActionNone , ref . RefAction )
// Issue #4 to test against
itarget = testCreateIssue ( t , 3 , 3 , "title4" , "content4" , false )
// Cross-reference to issue #4 by admin
content = fmt . Sprintf ( "content5, mentions user3/repo3#%d" , itarget . Index )
i = testCreateIssue ( t , 2 , 1 , "title5" , content , false )
2022-08-16 05:22:25 +03:00
ref = unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : itarget . ID , RefIssueID : i . ID , RefCommentID : 0 } )
2022-06-13 12:37:59 +03:00
assert . Equal ( t , issues_model . CommentTypeIssueRef , ref . Type )
2019-11-18 16:13:07 +03:00
assert . Equal ( t , i . RepoID , ref . RefRepoID )
2021-06-07 08:27:09 +03:00
assert . False ( t , ref . RefIsPull )
2019-11-18 16:13:07 +03:00
assert . Equal ( t , references . XRefActionNone , ref . RefAction )
// Cross-reference to issue #4 with no permission
content = fmt . Sprintf ( "content6, mentions user3/repo3#%d" , itarget . Index )
i = testCreateIssue ( t , 4 , 5 , "title6" , content , false )
2022-06-13 12:37:59 +03:00
unittest . AssertNotExistsBean ( t , & issues_model . Comment { IssueID : itarget . ID , RefIssueID : i . ID , RefCommentID : 0 } )
2019-11-18 16:13:07 +03:00
}
func TestXRef_NeuterCrossReferences ( t * testing . T ) {
2021-11-12 17:36:47 +03:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2019-11-18 16:13:07 +03:00
// Issue #1 to test against
itarget := testCreateIssue ( t , 1 , 2 , "title1" , "content1" , false )
// Issue mentioning issue #1
title := fmt . Sprintf ( "title2, mentions #%d" , itarget . Index )
i := testCreateIssue ( t , 1 , 2 , title , "content2" , false )
2022-08-16 05:22:25 +03:00
ref := unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : itarget . ID , RefIssueID : i . ID , RefCommentID : 0 } )
2022-06-13 12:37:59 +03:00
assert . Equal ( t , issues_model . CommentTypeIssueRef , ref . Type )
2019-11-18 16:13:07 +03:00
assert . Equal ( t , references . XRefActionNone , ref . RefAction )
2022-08-16 05:22:25 +03:00
d := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2019-11-18 16:13:07 +03:00
i . Title = "title2, no mentions"
2023-04-14 21:18:28 +03:00
assert . NoError ( t , issues_model . ChangeIssueTitle ( db . DefaultContext , i , d , title ) )
2019-11-18 16:13:07 +03:00
2022-08-16 05:22:25 +03:00
ref = unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : itarget . ID , RefIssueID : i . ID , RefCommentID : 0 } )
2022-06-13 12:37:59 +03:00
assert . Equal ( t , issues_model . CommentTypeIssueRef , ref . Type )
2019-11-18 16:13:07 +03:00
assert . Equal ( t , references . XRefActionNeutered , ref . RefAction )
}
func TestXRef_ResolveCrossReferences ( t * testing . T ) {
2021-11-12 17:36:47 +03:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2019-11-18 16:13:07 +03:00
2022-08-16 05:22:25 +03:00
d := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2019-11-18 16:13:07 +03:00
i1 := testCreateIssue ( t , 1 , 2 , "title1" , "content1" , false )
i2 := testCreateIssue ( t , 1 , 2 , "title2" , "content2" , false )
i3 := testCreateIssue ( t , 1 , 2 , "title3" , "content3" , false )
2022-06-13 12:37:59 +03:00
_ , err := issues_model . ChangeIssueStatus ( db . DefaultContext , i3 , d , true )
2019-12-16 00:57:34 +03:00
assert . NoError ( t , err )
2019-11-18 16:13:07 +03:00
pr := testCreatePR ( t , 1 , 2 , "titlepr" , fmt . Sprintf ( "closes #%d" , i1 . Index ) )
2022-08-16 05:22:25 +03:00
rp := unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : i1 . ID , RefIssueID : pr . Issue . ID , RefCommentID : 0 } )
2019-11-18 16:13:07 +03:00
c1 := testCreateComment ( t , 1 , 2 , pr . Issue . ID , fmt . Sprintf ( "closes #%d" , i2 . Index ) )
2022-08-16 05:22:25 +03:00
r1 := unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : i2 . ID , RefIssueID : pr . Issue . ID , RefCommentID : c1 . ID } )
2019-11-18 16:13:07 +03:00
// Must be ignored
c2 := testCreateComment ( t , 1 , 2 , pr . Issue . ID , fmt . Sprintf ( "mentions #%d" , i2 . Index ) )
2022-06-13 12:37:59 +03:00
unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : i2 . ID , RefIssueID : pr . Issue . ID , RefCommentID : c2 . ID } )
2019-11-18 16:13:07 +03:00
// Must be superseded by c4/r4
c3 := testCreateComment ( t , 1 , 2 , pr . Issue . ID , fmt . Sprintf ( "reopens #%d" , i3 . Index ) )
2022-06-13 12:37:59 +03:00
unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : i3 . ID , RefIssueID : pr . Issue . ID , RefCommentID : c3 . ID } )
2019-11-18 16:13:07 +03:00
c4 := testCreateComment ( t , 1 , 2 , pr . Issue . ID , fmt . Sprintf ( "closes #%d" , i3 . Index ) )
2022-08-16 05:22:25 +03:00
r4 := unittest . AssertExistsAndLoadBean ( t , & issues_model . Comment { IssueID : i3 . ID , RefIssueID : pr . Issue . ID , RefCommentID : c4 . ID } )
2019-11-18 16:13:07 +03:00
2022-05-03 22:46:28 +03:00
refs , err := pr . ResolveCrossReferences ( db . DefaultContext )
2019-11-18 16:13:07 +03:00
assert . NoError ( t , err )
assert . Len ( t , refs , 3 )
assert . Equal ( t , rp . ID , refs [ 0 ] . ID , "bad ref rp: %+v" , refs [ 0 ] )
assert . Equal ( t , r1 . ID , refs [ 1 ] . ID , "bad ref r1: %+v" , refs [ 1 ] )
assert . Equal ( t , r4 . ID , refs [ 2 ] . ID , "bad ref r4: %+v" , refs [ 2 ] )
}
2022-06-13 12:37:59 +03:00
func testCreateIssue ( t * testing . T , repo , doer int64 , title , content string , ispull bool ) * issues_model . Issue {
2022-08-16 05:22:25 +03:00
r := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : repo } )
d := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : doer } )
2021-06-14 05:22:55 +03:00
2022-11-12 23:18:50 +03:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2022-10-16 13:44:16 +03:00
assert . NoError ( t , err )
defer committer . Close ( )
idx , err := db . GetNextResourceIndex ( ctx , "issue_index" , r . ID )
2021-06-14 05:22:55 +03:00
assert . NoError ( t , err )
2022-06-13 12:37:59 +03:00
i := & issues_model . Issue {
2021-06-14 05:22:55 +03:00
RepoID : r . ID ,
PosterID : d . ID ,
Poster : d ,
Title : title ,
Content : content ,
IsPull : ispull ,
Index : idx ,
}
2019-11-18 16:13:07 +03:00
2022-06-13 12:37:59 +03:00
err = issues_model . NewIssueWithIndex ( ctx , d , issues_model . NewIssueOptions {
2021-06-14 05:22:55 +03:00
Repo : r ,
Issue : i ,
} )
2019-11-18 16:13:07 +03:00
assert . NoError ( t , err )
2022-06-13 12:37:59 +03:00
i , err = issues_model . GetIssueByID ( ctx , i . ID )
2019-11-18 16:13:07 +03:00
assert . NoError ( t , err )
2022-06-13 12:37:59 +03:00
assert . NoError ( t , i . AddCrossReferences ( ctx , d , false ) )
2021-11-19 16:39:57 +03:00
assert . NoError ( t , committer . Commit ( ) )
2019-11-18 16:13:07 +03:00
return i
}
2022-06-13 12:37:59 +03:00
func testCreatePR ( t * testing . T , repo , doer int64 , title , content string ) * issues_model . PullRequest {
2022-08-16 05:22:25 +03:00
r := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : repo } )
d := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : doer } )
2022-06-13 12:37:59 +03:00
i := & issues_model . Issue { RepoID : r . ID , PosterID : d . ID , Poster : d , Title : title , Content : content , IsPull : true }
pr := & issues_model . PullRequest { HeadRepoID : repo , BaseRepoID : repo , HeadBranch : "head" , BaseBranch : "base" , Status : issues_model . PullRequestStatusMergeable }
assert . NoError ( t , issues_model . NewPullRequest ( db . DefaultContext , r , i , nil , nil , pr ) )
2019-11-18 16:13:07 +03:00
pr . Issue = i
return pr
}
2022-06-13 12:37:59 +03:00
func testCreateComment ( t * testing . T , repo , doer , issue int64 , content string ) * issues_model . Comment {
2022-08-16 05:22:25 +03:00
d := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : doer } )
i := unittest . AssertExistsAndLoadBean ( t , & issues_model . Issue { ID : issue } )
2022-06-13 12:37:59 +03:00
c := & issues_model . Comment { Type : issues_model . CommentTypeComment , PosterID : doer , Poster : d , IssueID : issue , Issue : i , Content : content }
2019-11-18 16:13:07 +03:00
2022-11-12 23:18:50 +03:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-19 16:39:57 +03:00
assert . NoError ( t , err )
defer committer . Close ( )
err = db . Insert ( ctx , c )
2019-11-18 16:13:07 +03:00
assert . NoError ( t , err )
2022-06-13 12:37:59 +03:00
assert . NoError ( t , c . AddCrossReferences ( ctx , d , false ) )
2021-11-19 16:39:57 +03:00
assert . NoError ( t , committer . Commit ( ) )
2019-11-18 16:13:07 +03:00
return c
}