@@ -25,6 +25,8 @@ import (
2525 api "code.gitea.io/gitea/modules/structs"
2626 "code.gitea.io/gitea/modules/test"
2727 "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"
2830
2931 "github.com/stretchr/testify/assert"
3032 "github.com/unknwon/i18n"
@@ -65,7 +67,7 @@ func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum str
6567
6668func TestPullMerge (t * testing.T ) {
6769 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
6971 assert .NoError (t , err )
7072 hookTasksLenBefore := len (hookTasks )
7173
@@ -87,7 +89,7 @@ func TestPullMerge(t *testing.T) {
8789
8890func TestPullRebase (t * testing.T ) {
8991 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
9193 assert .NoError (t , err )
9294 hookTasksLenBefore := len (hookTasks )
9395
@@ -109,7 +111,7 @@ func TestPullRebase(t *testing.T) {
109111
110112func TestPullRebaseMerge (t * testing.T ) {
111113 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
113115 assert .NoError (t , err )
114116 hookTasksLenBefore := len (hookTasks )
115117
@@ -131,7 +133,7 @@ func TestPullRebaseMerge(t *testing.T) {
131133
132134func TestPullSquash (t * testing.T ) {
133135 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
135137 assert .NoError (t , err )
136138 hookTasksLenBefore := len (hookTasks )
137139
@@ -335,3 +337,74 @@ func TestCantMergeUnrelated(t *testing.T) {
335337 gitRepo .Close ()
336338 })
337339}
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