2020-01-17 09:03:40 +03:00
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package integrations
import (
2020-08-04 23:55:22 +03:00
"net/http"
2020-01-17 09:03:40 +03:00
"net/url"
"testing"
"time"
"code.gitea.io/gitea/models"
2021-11-16 11:53:21 +03:00
"code.gitea.io/gitea/models/unittest"
2020-01-17 09:03:40 +03:00
"code.gitea.io/gitea/modules/repofiles"
pull_service "code.gitea.io/gitea/services/pull"
repo_service "code.gitea.io/gitea/services/repository"
"github.com/stretchr/testify/assert"
)
2020-08-04 23:55:22 +03:00
func TestAPIPullUpdate ( t * testing . T ) {
2020-01-17 09:03:40 +03:00
onGiteaRun ( t , func ( t * testing . T , giteaURL * url . URL ) {
//Create PR to test
2021-11-16 11:53:21 +03:00
user := unittest . AssertExistsAndLoadBean ( t , & models . User { ID : 2 } ) . ( * models . User )
org26 := unittest . AssertExistsAndLoadBean ( t , & models . User { ID : 26 } ) . ( * models . User )
2020-01-17 09:03:40 +03:00
pr := createOutdatedPR ( t , user , org26 )
//Test GetDiverging
diffCount , err := pull_service . GetDiverging ( pr )
assert . NoError ( t , err )
assert . EqualValues ( t , 1 , diffCount . Behind )
assert . EqualValues ( t , 1 , diffCount . Ahead )
2020-08-04 23:55:22 +03:00
assert . NoError ( t , pr . LoadBaseRepo ( ) )
assert . NoError ( t , pr . LoadIssue ( ) )
2020-01-17 09:03:40 +03:00
2020-08-04 23:55:22 +03:00
session := loginUser ( t , "user2" )
token := getTokenForLoggedInUser ( t , session )
req := NewRequestf ( t , "POST" , "/api/v1/repos/%s/%s/pulls/%d/update?token=" + token , pr . BaseRepo . OwnerName , pr . BaseRepo . Name , pr . Issue . Index )
session . MakeRequest ( t , req , http . StatusOK )
2020-01-17 09:03:40 +03:00
//Test GetDiverging after update
diffCount , err = pull_service . GetDiverging ( pr )
assert . NoError ( t , err )
assert . EqualValues ( t , 0 , diffCount . Behind )
assert . EqualValues ( t , 2 , diffCount . Ahead )
} )
}
2021-08-31 17:03:45 +03:00
func TestAPIPullUpdateByRebase ( t * testing . T ) {
onGiteaRun ( t , func ( t * testing . T , giteaURL * url . URL ) {
//Create PR to test
2021-11-16 11:53:21 +03:00
user := unittest . AssertExistsAndLoadBean ( t , & models . User { ID : 2 } ) . ( * models . User )
org26 := unittest . AssertExistsAndLoadBean ( t , & models . User { ID : 26 } ) . ( * models . User )
2021-08-31 17:03:45 +03:00
pr := createOutdatedPR ( t , user , org26 )
//Test GetDiverging
diffCount , err := pull_service . GetDiverging ( pr )
assert . NoError ( t , err )
assert . EqualValues ( t , 1 , diffCount . Behind )
assert . EqualValues ( t , 1 , diffCount . Ahead )
assert . NoError ( t , pr . LoadBaseRepo ( ) )
assert . NoError ( t , pr . LoadIssue ( ) )
session := loginUser ( t , "user2" )
token := getTokenForLoggedInUser ( t , session )
req := NewRequestf ( t , "POST" , "/api/v1/repos/%s/%s/pulls/%d/update?style=rebase&token=" + token , pr . BaseRepo . OwnerName , pr . BaseRepo . Name , pr . Issue . Index )
session . MakeRequest ( t , req , http . StatusOK )
//Test GetDiverging after update
diffCount , err = pull_service . GetDiverging ( pr )
assert . NoError ( t , err )
assert . EqualValues ( t , 0 , diffCount . Behind )
assert . EqualValues ( t , 1 , diffCount . Ahead )
} )
}
2020-01-17 09:03:40 +03:00
func createOutdatedPR ( t * testing . T , actor , forkOrg * models . User ) * models . PullRequest {
baseRepo , err := repo_service . CreateRepository ( actor , actor , models . CreateRepoOptions {
Name : "repo-pr-update" ,
Description : "repo-tmp-pr-update description" ,
AutoInit : true ,
Gitignores : "C,C++" ,
License : "MIT" ,
Readme : "Default" ,
IsPrivate : false ,
} )
assert . NoError ( t , err )
assert . NotEmpty ( t , baseRepo )
2021-11-16 16:30:11 +03:00
headRepo , err := repo_service . ForkRepository ( actor , forkOrg , models . ForkRepoOptions {
2021-08-28 11:37:14 +03:00
BaseRepo : baseRepo ,
Name : "repo-pr-update" ,
Description : "desc" ,
} )
2020-01-17 09:03:40 +03:00
assert . NoError ( t , err )
assert . NotEmpty ( t , headRepo )
//create a commit on base Repo
_ , err = repofiles . CreateOrUpdateRepoFile ( baseRepo , actor , & repofiles . UpdateRepoFileOptions {
TreePath : "File_A" ,
Message : "Add File A" ,
Content : "File A" ,
IsNewFile : true ,
OldBranch : "master" ,
NewBranch : "master" ,
Author : & repofiles . IdentityOptions {
Name : actor . Name ,
Email : actor . Email ,
} ,
Committer : & repofiles . IdentityOptions {
Name : actor . Name ,
Email : actor . Email ,
} ,
Dates : & repofiles . CommitDateOptions {
Author : time . Now ( ) ,
Committer : time . Now ( ) ,
} ,
} )
assert . NoError ( t , err )
//create a commit on head Repo
_ , err = repofiles . CreateOrUpdateRepoFile ( headRepo , actor , & repofiles . UpdateRepoFileOptions {
TreePath : "File_B" ,
Message : "Add File on PR branch" ,
Content : "File B" ,
IsNewFile : true ,
OldBranch : "master" ,
NewBranch : "newBranch" ,
Author : & repofiles . IdentityOptions {
Name : actor . Name ,
Email : actor . Email ,
} ,
Committer : & repofiles . IdentityOptions {
Name : actor . Name ,
Email : actor . Email ,
} ,
Dates : & repofiles . CommitDateOptions {
Author : time . Now ( ) ,
Committer : time . Now ( ) ,
} ,
} )
assert . NoError ( t , err )
//create Pull
pullIssue := & models . Issue {
RepoID : baseRepo . ID ,
Title : "Test Pull -to-update-" ,
PosterID : actor . ID ,
Poster : actor ,
IsPull : true ,
}
pullRequest := & models . PullRequest {
HeadRepoID : headRepo . ID ,
BaseRepoID : baseRepo . ID ,
HeadBranch : "newBranch" ,
BaseBranch : "master" ,
HeadRepo : headRepo ,
BaseRepo : baseRepo ,
Type : models . PullRequestGitea ,
}
err = pull_service . NewPullRequest ( baseRepo , pullIssue , nil , nil , pullRequest , nil )
assert . NoError ( t , err )
2021-11-16 11:53:21 +03:00
issue := unittest . AssertExistsAndLoadBean ( t , & models . Issue { Title : "Test Pull -to-update-" } ) . ( * models . Issue )
2020-01-17 09:03:40 +03:00
pr , err := models . GetPullRequestByIssueID ( issue . ID )
assert . NoError ( t , err )
return pr
}