@@ -25,6 +25,8 @@ import (
25
25
api "code.gitea.io/gitea/modules/structs"
26
26
"code.gitea.io/gitea/modules/test"
27
27
"code.gitea.io/gitea/services/pull"
28
+ repo_service "code.gitea.io/gitea/services/repository"
29
+ files_service "code.gitea.io/gitea/services/repository/files"
28
30
29
31
"github.com/stretchr/testify/assert"
30
32
"github.com/unknwon/i18n"
@@ -65,7 +67,7 @@ func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum str
65
67
66
68
func TestPullMerge (t * testing.T ) {
67
69
onGiteaRun (t , func (t * testing.T , giteaURL * url.URL ) {
68
- hookTasks , err := webhook .HookTasks (1 , 1 ) //Retrieve previous hook number
70
+ hookTasks , err := webhook .HookTasks (1 , 1 ) // Retrieve previous hook number
69
71
assert .NoError (t , err )
70
72
hookTasksLenBefore := len (hookTasks )
71
73
@@ -87,7 +89,7 @@ func TestPullMerge(t *testing.T) {
87
89
88
90
func TestPullRebase (t * testing.T ) {
89
91
onGiteaRun (t , func (t * testing.T , giteaURL * url.URL ) {
90
- hookTasks , err := webhook .HookTasks (1 , 1 ) //Retrieve previous hook number
92
+ hookTasks , err := webhook .HookTasks (1 , 1 ) // Retrieve previous hook number
91
93
assert .NoError (t , err )
92
94
hookTasksLenBefore := len (hookTasks )
93
95
@@ -109,7 +111,7 @@ func TestPullRebase(t *testing.T) {
109
111
110
112
func TestPullRebaseMerge (t * testing.T ) {
111
113
onGiteaRun (t , func (t * testing.T , giteaURL * url.URL ) {
112
- hookTasks , err := webhook .HookTasks (1 , 1 ) //Retrieve previous hook number
114
+ hookTasks , err := webhook .HookTasks (1 , 1 ) // Retrieve previous hook number
113
115
assert .NoError (t , err )
114
116
hookTasksLenBefore := len (hookTasks )
115
117
@@ -131,7 +133,7 @@ func TestPullRebaseMerge(t *testing.T) {
131
133
132
134
func TestPullSquash (t * testing.T ) {
133
135
onGiteaRun (t , func (t * testing.T , giteaURL * url.URL ) {
134
- hookTasks , err := webhook .HookTasks (1 , 1 ) //Retrieve previous hook number
136
+ hookTasks , err := webhook .HookTasks (1 , 1 ) // Retrieve previous hook number
135
137
assert .NoError (t , err )
136
138
hookTasksLenBefore := len (hookTasks )
137
139
@@ -335,3 +337,74 @@ func TestCantMergeUnrelated(t *testing.T) {
335
337
gitRepo .Close ()
336
338
})
337
339
}
340
+
341
+ func TestConflictChecking (t * testing.T ) {
342
+ onGiteaRun (t , func (t * testing.T , giteaURL * url.URL ) {
343
+ user := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 }).(* user_model.User )
344
+
345
+ // Create new clean repo to test conflict checking.
346
+ baseRepo , err := repo_service .CreateRepository (user , user , models.CreateRepoOptions {
347
+ Name : "conflict-checking" ,
348
+ Description : "Tempo repo" ,
349
+ AutoInit : true ,
350
+ Readme : "Default" ,
351
+ DefaultBranch : "main" ,
352
+ })
353
+ assert .NoError (t , err )
354
+ assert .NotEmpty (t , baseRepo )
355
+
356
+ // create a commit on new branch.
357
+ _ , err = files_service .CreateOrUpdateRepoFile (baseRepo , user , & files_service.UpdateRepoFileOptions {
358
+ TreePath : "important_file" ,
359
+ Message : "Add a important file" ,
360
+ Content : "Just a non-important file" ,
361
+ IsNewFile : true ,
362
+ OldBranch : "main" ,
363
+ NewBranch : "important-secrets" ,
364
+ })
365
+ assert .NoError (t , err )
366
+
367
+ // create a commit on main branch.
368
+ _ , err = files_service .CreateOrUpdateRepoFile (baseRepo , user , & files_service.UpdateRepoFileOptions {
369
+ TreePath : "important_file" ,
370
+ Message : "Add a important file" ,
371
+ Content : "Not the same content :P" ,
372
+ IsNewFile : true ,
373
+ OldBranch : "main" ,
374
+ NewBranch : "main" ,
375
+ })
376
+ assert .NoError (t , err )
377
+
378
+ // create Pull to merge the important-secrets branch into main branch.
379
+ pullIssue := & models.Issue {
380
+ RepoID : baseRepo .ID ,
381
+ Title : "PR with conflict!" ,
382
+ PosterID : user .ID ,
383
+ Poster : user ,
384
+ IsPull : true ,
385
+ }
386
+
387
+ pullRequest := & models.PullRequest {
388
+ HeadRepoID : baseRepo .ID ,
389
+ BaseRepoID : baseRepo .ID ,
390
+ HeadBranch : "important-secrets" ,
391
+ BaseBranch : "main" ,
392
+ HeadRepo : baseRepo ,
393
+ BaseRepo : baseRepo ,
394
+ Type : models .PullRequestGitea ,
395
+ }
396
+ err = pull .NewPullRequest (baseRepo , pullIssue , nil , nil , pullRequest , nil )
397
+ assert .NoError (t , err )
398
+
399
+ issue := unittest .AssertExistsAndLoadBean (t , & models.Issue {Title : "PR with conflict!" }).(* models.Issue )
400
+ conflictingPR , err := models .GetPullRequestByIssueID (issue .ID )
401
+ assert .NoError (t , err )
402
+
403
+ // Ensure conflictedFiles is populated.
404
+ assert .Equal (t , 1 , len (conflictingPR .ConflictedFiles ))
405
+ // Check if status is correct.
406
+ assert .Equal (t , models .PullRequestStatusConflict , conflictingPR .Status )
407
+ // Ensure that mergeable returns false
408
+ assert .False (t , conflictingPR .Mergeable ())
409
+ })
410
+ }
0 commit comments