@@ -13,109 +13,98 @@ import (
1313
1414 issues_model "code.gitea.io/gitea/models/issues"
1515 "code.gitea.io/gitea/models/unittest"
16- "code.gitea.io/gitea/modules/git/gitcmd"
1716 issues_service "code.gitea.io/gitea/services/issue"
1817
1918 "github.com/stretchr/testify/assert"
19+ "github.com/stretchr/testify/require"
2020)
2121
22- func TestPull_RebaseComment (t * testing.T ) {
23- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
24- session := loginUser (t , "user1" )
25- testRepoFork (t , session , "user2" , "repo1" , "user1" , "repo1" , "" )
26- testEditFile (t , session , "user1" , "repo1" , "master" , "README.md" , "Hello, World (Edited)\n " )
27- testPullCreate (t , session , "user1" , "repo1" , false , "master" , "master" , "This is a pull title" )
28-
29- // create a conflict line on user2/repo1:master README.md
30- testEditFile (t , session , "user2" , "repo1" , "master" , "README.md" , "Hello, World (Edited Conflicted)\n " )
31-
32- // Now the pull request status should be conflicted
33- prIssue := unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {Title : "This is a pull title" })
34- assert .NoError (t , prIssue .LoadPullRequest (t .Context ()))
35- assert .Equal (t , issues_model .PullRequestStatusConflict , prIssue .PullRequest .Status )
36- assert .NoError (t , prIssue .PullRequest .LoadBaseRepo (t .Context ()))
37- assert .Equal (t , "user2/repo1" , prIssue .PullRequest .BaseRepo .FullName ())
38- assert .NoError (t , prIssue .PullRequest .LoadHeadRepo (t .Context ()))
39- assert .Equal (t , "user1/repo1" , prIssue .PullRequest .HeadRepo .FullName ())
40-
41- dstPath := t .TempDir ()
42- u .Path = "/user2/repo1.git"
43- doGitClone (dstPath , u )(t )
44- doGitCreateBranch (dstPath , "dev" )(t )
45- content , err := os .ReadFile (dstPath + "/README.md" )
46- assert .NoError (t , err )
47- assert .Equal (t , "Hello, World (Edited Conflicted)\n " , string (content ))
48-
49- doGitCheckoutWriteFileCommit (localGitAddCommitOptions {
50- LocalRepoPath : dstPath ,
51- CheckoutBranch : "dev" ,
52- TreeFilePath : "README.md" ,
53- TreeFileContent : "Hello, World (Edited Conflict Resolved)\n " ,
54- })(t )
55-
56- // do force push
57- u .Path = "/user1/repo1.git"
58- u .User = url .UserPassword ("user1" , userPassword )
59- doGitAddRemote (dstPath , "fork" , u )(t )
60- // non force push will fail
61- _ , _ , err = gitcmd .NewCommand ().AddArguments ("push" , "fork" , "dev:master" ).RunStdString (t .Context (), & gitcmd.RunOpts {Dir : dstPath })
62- assert .Error (t , err )
63- _ , _ , err = gitcmd .NewCommand ().AddArguments ("push" , "--force" , "fork" , "dev:master" ).RunStdString (t .Context (), & gitcmd.RunOpts {Dir : dstPath })
64- assert .NoError (t , err )
65-
66- time .Sleep (time .Second ) // wait for pull request conflict checking
67-
68- // reload the pr
69- prIssue = unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {Title : "This is a pull title" })
70- assert .NoError (t , prIssue .LoadPullRequest (t .Context ()))
71- assert .Equal (t , issues_model .PullRequestStatusMergeable , prIssue .PullRequest .Status )
72- comments , err := issues_model .FindComments (t .Context (), & issues_model.FindCommentsOptions {
73- IssueID : prIssue .ID ,
74- Type : issues_model .CommentTypeUndefined , // get all comments type
75- })
76- assert .NoError (t , err )
77- lastComment := comments [len (comments )- 1 ]
78- err = issues_service .LoadCommentPushCommits (t .Context (), lastComment )
79- assert .NoError (t , err )
80- assert .True (t , lastComment .IsForcePush )
22+ func testWaitForPullRequestStatus (t * testing.T , prIssue * issues_model.Issue , expectedStatus issues_model.PullRequestStatus ) (retIssue * issues_model.Issue ) {
23+ require .Eventually (t , func () bool {
24+ prIssueCond := * prIssue
25+ retIssue = unittest .AssertExistsAndLoadBean (t , & prIssueCond )
26+ require .NoError (t , retIssue .LoadPullRequest (t .Context ()))
27+ return retIssue .PullRequest .Status == expectedStatus
28+ }, 5 * time .Second , 20 * time .Millisecond )
29+ return retIssue
30+ }
31+
32+ func testPullCommentRebase (t * testing.T , u * url.URL , session * TestSession ) {
33+ testPRTitle := "Test PR for rebase comment"
34+ testEditFile (t , session , "user1" , "repo1" , "test-branch/rebase" , "README.md" , "Hello, World (Edited)\n " )
35+ testPullCreate (t , session , "user1" , "repo1" , false , "test-branch/rebase" , "test-branch/rebase" , testPRTitle )
36+
37+ // create a conflict line on user2/repo1:test-branch/rebase README.md
38+ testEditFile (t , session , "user2" , "repo1" , "test-branch/rebase" , "README.md" , "Hello, World (Edited Conflicted)\n " )
39+
40+ // Now the pull request status should be conflicted
41+ testWaitForPullRequestStatus (t , & issues_model.Issue {Title : testPRTitle }, issues_model .PullRequestStatusConflict )
42+
43+ dstPath := t .TempDir ()
44+ u .Path = "/user2/repo1.git"
45+ doGitClone (dstPath , u )(t )
46+ doGitCheckoutBranch (dstPath , "test-branch/rebase" )(t )
47+ doGitCreateBranch (dstPath , "local-branch/rebase" )(t )
48+ content , _ := os .ReadFile (dstPath + "/README.md" )
49+ require .Equal (t , "Hello, World (Edited Conflicted)\n " , string (content ))
50+
51+ doGitCheckoutWriteFileCommit (localGitAddCommitOptions {
52+ LocalRepoPath : dstPath ,
53+ CheckoutBranch : "local-branch/rebase" ,
54+ TreeFilePath : "README.md" ,
55+ TreeFileContent : "Hello, World (Edited Conflict Resolved)\n " ,
56+ })(t )
57+
58+ // do force push
59+ u .Path = "/user1/repo1.git"
60+ u .User = url .UserPassword ("user1" , userPassword )
61+ doGitAddRemote (dstPath , "base-repo" , u )(t )
62+ doGitPushTestRepositoryFail (dstPath , "base-repo" , "local-branch/rebase:test-branch/rebase" )(t )
63+ doGitPushTestRepository (dstPath , "--force" , "base-repo" , "local-branch/rebase:test-branch/rebase" )(t )
64+
65+ // reload the pr
66+ prIssue := testWaitForPullRequestStatus (t , & issues_model.Issue {Title : testPRTitle }, issues_model .PullRequestStatusMergeable )
67+ comments , err := issues_model .FindComments (t .Context (), & issues_model.FindCommentsOptions {
68+ IssueID : prIssue .ID ,
69+ Type : issues_model .CommentTypeUndefined , // get all comments type
70+ })
71+ require .NoError (t , err )
72+ lastComment := comments [len (comments )- 1 ]
73+ assert .NoError (t , issues_service .LoadCommentPushCommits (t .Context (), lastComment ))
74+ assert .True (t , lastComment .IsForcePush )
75+ }
76+
77+ func testPullCommentRetarget (t * testing.T , u * url.URL , session * TestSession ) {
78+ testPRTitle := "Test PR for retarget comment"
79+
80+ // create a non-conflict branch dev from master
81+ testCreateBranch (t , session , "user2" , "repo1" , "branch/test-branch/retarget" , "test-branch/retarget-no-conflict" , http .StatusSeeOther )
82+ // make a change on forked branch
83+ testEditFile (t , session , "user1" , "repo1" , "test-branch/retarget" , "README.md" , "Hello, World (Edited)\n " )
84+ testPullCreate (t , session , "user1" , "repo1" , false , "test-branch/retarget" , "test-branch/retarget" , testPRTitle )
85+ // create a conflict line on user2/repo1 README.md
86+ testEditFile (t , session , "user2" , "repo1" , "test-branch/retarget" , "README.md" , "Hello, World (Edited Conflicted)\n " )
87+
88+ // Now the pull request status should be conflicted
89+ prIssue := testWaitForPullRequestStatus (t , & issues_model.Issue {Title : testPRTitle }, issues_model .PullRequestStatusConflict )
90+
91+ // do retarget
92+ req := NewRequestWithValues (t , "POST" , fmt .Sprintf ("/user2/repo1/pull/%d/target_branch" , prIssue .PullRequest .Index ), map [string ]string {
93+ "_csrf" : GetUserCSRFToken (t , session ),
94+ "target_branch" : "test-branch/retarget-no-conflict" ,
8195 })
96+ session .MakeRequest (t , req , http .StatusOK )
97+ testWaitForPullRequestStatus (t , & issues_model.Issue {Title : testPRTitle }, issues_model .PullRequestStatusMergeable )
8298}
8399
84- func TestPull_RetargetComment (t * testing.T ) {
100+ func TestPullComment (t * testing.T ) {
85101 onGiteaRun (t , func (t * testing.T , u * url.URL ) {
86102 session := loginUser (t , "user1" )
103+ testCreateBranch (t , session , "user2" , "repo1" , "branch/master" , "test-branch/rebase" , http .StatusSeeOther )
104+ testCreateBranch (t , session , "user2" , "repo1" , "branch/master" , "test-branch/retarget" , http .StatusSeeOther )
87105 testRepoFork (t , session , "user2" , "repo1" , "user1" , "repo1" , "" )
88- testEditFile (t , session , "user1" , "repo1" , "master" , "README.md" , "Hello, World (Edited)\n " )
89- testPullCreate (t , session , "user1" , "repo1" , false , "master" , "master" , "This is a pull title" )
90-
91- session2 := loginUser (t , "user2" )
92- // create a non-conflict branch dev from master
93- testCreateBranch (t , session2 , "user2" , "repo1" , "branch/master" , "dev" , http .StatusSeeOther )
94-
95- // create a conflict line on user2/repo1:master README.md
96- testEditFile (t , session2 , "user2" , "repo1" , "master" , "README.md" , "Hello, World (Edited Conflicted)\n " )
97-
98- // Now the pull request status should be conflicted
99- prIssue := unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {Title : "This is a pull title" })
100- assert .NoError (t , prIssue .LoadPullRequest (t .Context ()))
101- assert .Equal (t , issues_model .PullRequestStatusConflict , prIssue .PullRequest .Status )
102- assert .NoError (t , prIssue .PullRequest .LoadBaseRepo (t .Context ()))
103- assert .Equal (t , "user2/repo1" , prIssue .PullRequest .BaseRepo .FullName ())
104- assert .NoError (t , prIssue .PullRequest .LoadHeadRepo (t .Context ()))
105- assert .Equal (t , "user1/repo1" , prIssue .PullRequest .HeadRepo .FullName ())
106-
107- // do retarget
108- req := NewRequestWithValues (t , "POST" , fmt .Sprintf ("/user2/repo1/pull/%d/target_branch" , prIssue .PullRequest .Index ), map [string ]string {
109- "_csrf" : GetUserCSRFToken (t , session2 ),
110- "target_branch" : "dev" ,
111- })
112- session2 .MakeRequest (t , req , http .StatusOK )
113-
114- time .Sleep (time .Second ) // wait for pull request conflict checking
115-
116- // reload the pr
117- prIssue = unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {Title : "This is a pull title" })
118- assert .NoError (t , prIssue .LoadPullRequest (t .Context ()))
119- assert .Equal (t , issues_model .PullRequestStatusMergeable , prIssue .PullRequest .Status )
106+
107+ t .Run ("RebaseComment" , func (t * testing.T ) { testPullCommentRebase (t , u , session ) })
108+ t .Run ("RetargetComment" , func (t * testing.T ) { testPullCommentRetarget (t , u , session ) })
120109 })
121110}
0 commit comments