2017-11-02 23:21:03 +05:30
// Copyright 2017 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.
2022-09-03 00:48:23 +05:30
package integration
2017-11-02 23:21:03 +05:30
import (
2021-04-10 13:57:29 +05:30
"encoding/hex"
2018-01-16 16:37:47 +05:30
"fmt"
2020-04-27 16:50:09 +05:30
"math/rand"
2019-02-12 20:39:43 +05:30
"net/http"
2017-12-08 17:51:37 +05:30
"net/url"
2021-09-22 11:08:34 +05:30
"os"
2019-02-12 20:39:43 +05:30
"path"
2017-11-02 23:21:03 +05:30
"path/filepath"
2019-05-31 15:42:15 +05:30
"strconv"
2017-11-02 23:21:03 +05:30
"testing"
"time"
2022-05-20 19:38:52 +05:30
"code.gitea.io/gitea/models/db"
2022-06-13 15:07:59 +05:30
issues_model "code.gitea.io/gitea/models/issues"
2021-11-28 17:28:28 +05:30
"code.gitea.io/gitea/models/perm"
2021-12-10 06:57:50 +05:30
repo_model "code.gitea.io/gitea/models/repo"
2021-11-16 14:23:21 +05:30
"code.gitea.io/gitea/models/unittest"
2021-11-24 15:19:20 +05:30
user_model "code.gitea.io/gitea/models/user"
2019-03-27 15:03:00 +05:30
"code.gitea.io/gitea/modules/git"
2021-04-09 03:55:57 +05:30
"code.gitea.io/gitea/modules/lfs"
2019-10-12 05:43:27 +05:30
"code.gitea.io/gitea/modules/setting"
2019-05-31 15:42:15 +05:30
api "code.gitea.io/gitea/modules/structs"
2022-09-03 00:48:23 +05:30
"code.gitea.io/gitea/tests"
2017-11-02 23:21:03 +05:30
"github.com/stretchr/testify/assert"
)
2018-01-16 16:37:47 +05:30
const (
2022-01-20 23:16:10 +05:30
littleSize = 1024 // 1ko
bigSize = 128 * 1024 * 1024 // 128Mo
2018-01-16 16:37:47 +05:30
)
2019-02-04 05:26:53 +05:30
func TestGit ( t * testing . T ) {
onGiteaRun ( t , testGit )
}
2017-11-02 23:21:03 +05:30
2019-02-04 05:26:53 +05:30
func testGit ( t * testing . T , u * url . URL ) {
username := "user2"
baseAPITestContext := NewAPITestContext ( t , username , "repo1" )
2017-11-02 23:21:03 +05:30
2019-02-04 05:26:53 +05:30
u . Path = baseAPITestContext . GitPath ( )
2017-11-02 23:21:03 +05:30
2019-06-22 23:05:34 +05:30
forkedUserCtx := NewAPITestContext ( t , "user4" , "repo1" )
2019-02-04 05:26:53 +05:30
t . Run ( "HTTP" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-06-22 23:05:34 +05:30
ensureAnonymousClone ( t , u )
2019-02-04 05:26:53 +05:30
httpContext := baseAPITestContext
httpContext . Reponame = "repo-tmp-17"
2019-06-22 23:05:34 +05:30
forkedUserCtx . Reponame = httpContext . Reponame
2017-11-02 23:21:03 +05:30
2022-09-04 20:44:53 +05:30
dstPath := t . TempDir ( )
2019-02-12 20:39:43 +05:30
2019-06-22 23:05:34 +05:30
t . Run ( "CreateRepoInDifferentUser" , doAPICreateRepository ( forkedUserCtx , false ) )
2021-11-28 17:28:28 +05:30
t . Run ( "AddUserAsCollaborator" , doAPIAddCollaborator ( forkedUserCtx , httpContext . Username , perm . AccessModeRead ) )
2019-06-22 23:05:34 +05:30
t . Run ( "ForkFromDifferentUser" , doAPIForkRepository ( httpContext , forkedUserCtx . Username ) )
2019-02-12 20:39:43 +05:30
2019-05-31 15:42:15 +05:30
u . Path = httpContext . GitPath ( )
u . User = url . UserPassword ( username , userPassword )
2019-02-12 20:39:43 +05:30
2019-05-31 15:42:15 +05:30
t . Run ( "Clone" , doGitClone ( dstPath , u ) )
2019-05-28 16:02:41 +05:30
2022-09-04 20:44:53 +05:30
dstPath2 := t . TempDir ( )
2022-01-24 02:49:32 +05:30
t . Run ( "Partial Clone" , doPartialGitClone ( dstPath2 , u ) )
2019-05-31 15:42:15 +05:30
little , big := standardCommitAndPushTest ( t , dstPath )
littleLFS , bigLFS := lfsCommitAndPushTest ( t , dstPath )
rawTest ( t , & httpContext , little , big , littleLFS , bigLFS )
mediaTest ( t , & httpContext , little , big , littleLFS , bigLFS )
2019-02-12 20:39:43 +05:30
2021-07-28 15:12:56 +05:30
t . Run ( "CreateAgitFlowPull" , doCreateAgitFlowPull ( dstPath , & httpContext , "master" , "test/head" ) )
2019-05-31 15:42:15 +05:30
t . Run ( "BranchProtectMerge" , doBranchProtectPRMerge ( & httpContext , dstPath ) )
2022-05-07 22:35:52 +05:30
t . Run ( "AutoMerge" , doAutoPRMerge ( & httpContext , dstPath ) )
2021-03-04 09:11:23 +05:30
t . Run ( "CreatePRAndSetManuallyMerged" , doCreatePRAndSetManuallyMerged ( httpContext , httpContext , dstPath , "master" , "test-manually-merge" ) )
2019-06-22 23:05:34 +05:30
t . Run ( "MergeFork" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-06-22 23:05:34 +05:30
t . Run ( "CreatePRAndMerge" , doMergeFork ( httpContext , forkedUserCtx , "master" , httpContext . Username + ":master" ) )
rawTest ( t , & forkedUserCtx , little , big , littleLFS , bigLFS )
mediaTest ( t , & forkedUserCtx , little , big , littleLFS , bigLFS )
} )
2019-12-15 08:19:52 +05:30
t . Run ( "PushCreate" , doPushCreate ( httpContext , u ) )
2019-02-04 05:26:53 +05:30
} )
t . Run ( "SSH" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-02-04 05:26:53 +05:30
sshContext := baseAPITestContext
sshContext . Reponame = "repo-tmp-18"
keyname := "my-testing-key"
2019-06-22 23:05:34 +05:30
forkedUserCtx . Reponame = sshContext . Reponame
t . Run ( "CreateRepoInDifferentUser" , doAPICreateRepository ( forkedUserCtx , false ) )
2021-11-28 17:28:28 +05:30
t . Run ( "AddUserAsCollaborator" , doAPIAddCollaborator ( forkedUserCtx , sshContext . Username , perm . AccessModeRead ) )
2019-06-22 23:05:34 +05:30
t . Run ( "ForkFromDifferentUser" , doAPIForkRepository ( sshContext , forkedUserCtx . Username ) )
2022-01-20 23:16:10 +05:30
// Setup key the user ssh key
2019-02-04 05:26:53 +05:30
withKeyFile ( t , keyname , func ( keyFile string ) {
t . Run ( "CreateUserKey" , doAPICreateUserKey ( sshContext , "test-key" , keyFile ) )
2018-01-16 16:37:47 +05:30
2022-01-20 23:16:10 +05:30
// Setup remote link
// TODO: get url from api
2019-02-04 05:26:53 +05:30
sshURL := createSSHUrl ( sshContext . GitPath ( ) , u )
2018-01-16 16:37:47 +05:30
2022-01-20 23:16:10 +05:30
// Setup clone folder
2022-09-04 20:44:53 +05:30
dstPath := t . TempDir ( )
2019-02-04 05:26:53 +05:30
2019-05-31 15:42:15 +05:30
t . Run ( "Clone" , doGitClone ( dstPath , sshURL ) )
little , big := standardCommitAndPushTest ( t , dstPath )
littleLFS , bigLFS := lfsCommitAndPushTest ( t , dstPath )
rawTest ( t , & sshContext , little , big , littleLFS , bigLFS )
mediaTest ( t , & sshContext , little , big , littleLFS , bigLFS )
2021-07-28 15:12:56 +05:30
t . Run ( "CreateAgitFlowPull" , doCreateAgitFlowPull ( dstPath , & sshContext , "master" , "test/head2" ) )
2019-05-31 15:42:15 +05:30
t . Run ( "BranchProtectMerge" , doBranchProtectPRMerge ( & sshContext , dstPath ) )
2019-06-22 23:05:34 +05:30
t . Run ( "MergeFork" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-06-22 23:05:34 +05:30
t . Run ( "CreatePRAndMerge" , doMergeFork ( sshContext , forkedUserCtx , "master" , sshContext . Username + ":master" ) )
rawTest ( t , & forkedUserCtx , little , big , littleLFS , bigLFS )
mediaTest ( t , & forkedUserCtx , little , big , littleLFS , bigLFS )
} )
2019-12-15 08:19:52 +05:30
t . Run ( "PushCreate" , doPushCreate ( sshContext , sshURL ) )
2017-12-08 17:51:37 +05:30
} )
2017-11-02 23:21:03 +05:30
} )
}
2018-01-16 16:37:47 +05:30
2019-02-04 05:26:53 +05:30
func ensureAnonymousClone ( t * testing . T , u * url . URL ) {
2022-09-04 20:44:53 +05:30
dstLocalPath := t . TempDir ( )
2019-02-04 05:26:53 +05:30
t . Run ( "CloneAnonymous" , doGitClone ( dstLocalPath , u ) )
}
2019-05-31 15:42:15 +05:30
func standardCommitAndPushTest ( t * testing . T , dstPath string ) ( little , big string ) {
t . Run ( "Standard" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-05-31 15:42:15 +05:30
little , big = commitAndPushTest ( t , dstPath , "data-file-" )
} )
2022-06-20 15:32:49 +05:30
return little , big
2019-05-31 15:42:15 +05:30
}
func lfsCommitAndPushTest ( t * testing . T , dstPath string ) ( littleLFS , bigLFS string ) {
t . Run ( "LFS" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-05-31 15:42:15 +05:30
prefix := "lfs-data-file-"
2022-04-01 08:25:30 +05:30
err := git . NewCommand ( git . DefaultContext , "lfs" ) . AddArguments ( "install" ) . Run ( & git . RunOpts { Dir : dstPath } )
2019-05-31 15:42:15 +05:30
assert . NoError ( t , err )
2022-10-23 20:14:45 +05:30
_ , _ , err = git . NewCommand ( git . DefaultContext , "lfs" ) . AddArguments ( "track" ) . AddDynamicArguments ( prefix + "*" ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2019-05-31 15:42:15 +05:30
assert . NoError ( t , err )
err = git . AddChanges ( dstPath , false , ".gitattributes" )
assert . NoError ( t , err )
2019-11-27 06:05:52 +05:30
2022-01-25 23:45:58 +05:30
err = git . CommitChangesWithArgs ( dstPath , git . AllowLFSFiltersArgs ( ) , git . CommitChangesOptions {
2019-10-12 05:43:27 +05:30
Committer : & git . Signature {
Email : "user2@example.com" ,
Name : "User Two" ,
When : time . Now ( ) ,
} ,
Author : & git . Signature {
Email : "user2@example.com" ,
Name : "User Two" ,
When : time . Now ( ) ,
} ,
Message : fmt . Sprintf ( "Testing commit @ %v" , time . Now ( ) ) ,
} )
2019-11-16 23:51:39 +05:30
assert . NoError ( t , err )
2019-05-31 15:42:15 +05:30
littleLFS , bigLFS = commitAndPushTest ( t , dstPath , prefix )
t . Run ( "Locks" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-05-31 15:42:15 +05:30
lockTest ( t , dstPath )
} )
} )
2022-06-20 15:32:49 +05:30
return littleLFS , bigLFS
2019-05-31 15:42:15 +05:30
}
func commitAndPushTest ( t * testing . T , dstPath , prefix string ) ( little , big string ) {
t . Run ( "PushCommit" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-05-31 15:42:15 +05:30
t . Run ( "Little" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-05-31 15:42:15 +05:30
little = doCommitAndPush ( t , littleSize , dstPath , prefix )
} )
t . Run ( "Big" , func ( t * testing . T ) {
if testing . Short ( ) {
t . Skip ( "Skipping test in short mode." )
return
}
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-05-31 15:42:15 +05:30
big = doCommitAndPush ( t , bigSize , dstPath , prefix )
} )
} )
2022-06-20 15:32:49 +05:30
return little , big
2019-05-31 15:42:15 +05:30
}
func rawTest ( t * testing . T , ctx * APITestContext , little , big , littleLFS , bigLFS string ) {
t . Run ( "Raw" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-05-31 15:42:15 +05:30
username := ctx . Username
reponame := ctx . Reponame
session := loginUser ( t , username )
// Request raw paths
req := NewRequest ( t , "GET" , path . Join ( "/" , username , reponame , "/raw/branch/master/" , little ) )
2021-04-06 22:14:02 +05:30
resp := session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , littleSize , resp . Length )
2019-05-31 15:42:15 +05:30
2019-10-12 05:43:27 +05:30
if setting . LFS . StartServer {
req = NewRequest ( t , "GET" , path . Join ( "/" , username , reponame , "/raw/branch/master/" , littleLFS ) )
2021-04-06 22:14:02 +05:30
resp := session . MakeRequest ( t , req , http . StatusOK )
2019-10-12 05:43:27 +05:30
assert . NotEqual ( t , littleSize , resp . Body . Len ( ) )
2021-03-14 21:23:59 +05:30
assert . LessOrEqual ( t , resp . Body . Len ( ) , 1024 )
if resp . Body . Len ( ) != littleSize && resp . Body . Len ( ) <= 1024 {
2021-04-09 03:55:57 +05:30
assert . Contains ( t , resp . Body . String ( ) , lfs . MetaFileIdentifier )
2021-03-14 21:23:59 +05:30
}
2019-10-12 05:43:27 +05:30
}
2019-05-31 15:42:15 +05:30
if ! testing . Short ( ) {
req = NewRequest ( t , "GET" , path . Join ( "/" , username , reponame , "/raw/branch/master/" , big ) )
2021-04-06 22:14:02 +05:30
resp := session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , bigSize , resp . Length )
2019-05-31 15:42:15 +05:30
2019-10-12 05:43:27 +05:30
if setting . LFS . StartServer {
req = NewRequest ( t , "GET" , path . Join ( "/" , username , reponame , "/raw/branch/master/" , bigLFS ) )
2021-04-06 22:14:02 +05:30
resp := session . MakeRequest ( t , req , http . StatusOK )
2019-10-12 05:43:27 +05:30
assert . NotEqual ( t , bigSize , resp . Body . Len ( ) )
2021-03-14 21:23:59 +05:30
if resp . Body . Len ( ) != bigSize && resp . Body . Len ( ) <= 1024 {
2021-04-09 03:55:57 +05:30
assert . Contains ( t , resp . Body . String ( ) , lfs . MetaFileIdentifier )
2021-03-14 21:23:59 +05:30
}
2019-10-12 05:43:27 +05:30
}
2019-05-31 15:42:15 +05:30
}
} )
}
func mediaTest ( t * testing . T , ctx * APITestContext , little , big , littleLFS , bigLFS string ) {
t . Run ( "Media" , func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-05-31 15:42:15 +05:30
username := ctx . Username
reponame := ctx . Reponame
session := loginUser ( t , username )
// Request media paths
req := NewRequest ( t , "GET" , path . Join ( "/" , username , reponame , "/media/branch/master/" , little ) )
resp := session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , littleSize , resp . Length )
2022-08-09 08:52:24 +05:30
req = NewRequest ( t , "GET" , path . Join ( "/" , username , reponame , "/media/branch/master/" , littleLFS ) )
resp = session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , littleSize , resp . Length )
2019-05-31 15:42:15 +05:30
if ! testing . Short ( ) {
req = NewRequest ( t , "GET" , path . Join ( "/" , username , reponame , "/media/branch/master/" , big ) )
resp = session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , bigSize , resp . Length )
2019-10-12 05:43:27 +05:30
if setting . LFS . StartServer {
req = NewRequest ( t , "GET" , path . Join ( "/" , username , reponame , "/media/branch/master/" , bigLFS ) )
resp = session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , bigSize , resp . Length )
}
2019-05-31 15:42:15 +05:30
}
} )
}
func lockTest ( t * testing . T , repoPath string ) {
2019-05-28 16:02:41 +05:30
lockFileTest ( t , "README.md" , repoPath )
}
func lockFileTest ( t * testing . T , filename , repoPath string ) {
2022-04-01 08:25:30 +05:30
_ , _ , err := git . NewCommand ( git . DefaultContext , "lfs" ) . AddArguments ( "locks" ) . RunStdString ( & git . RunOpts { Dir : repoPath } )
2018-01-16 16:37:47 +05:30
assert . NoError ( t , err )
2022-10-23 20:14:45 +05:30
_ , _ , err = git . NewCommand ( git . DefaultContext , "lfs" ) . AddArguments ( "lock" ) . AddDynamicArguments ( filename ) . RunStdString ( & git . RunOpts { Dir : repoPath } )
2018-01-16 16:37:47 +05:30
assert . NoError ( t , err )
2022-04-01 08:25:30 +05:30
_ , _ , err = git . NewCommand ( git . DefaultContext , "lfs" ) . AddArguments ( "locks" ) . RunStdString ( & git . RunOpts { Dir : repoPath } )
2018-01-16 16:37:47 +05:30
assert . NoError ( t , err )
2022-10-23 20:14:45 +05:30
_ , _ , err = git . NewCommand ( git . DefaultContext , "lfs" ) . AddArguments ( "unlock" ) . AddDynamicArguments ( filename ) . RunStdString ( & git . RunOpts { Dir : repoPath } )
2018-01-16 16:37:47 +05:30
assert . NoError ( t , err )
}
2019-05-31 15:42:15 +05:30
func doCommitAndPush ( t * testing . T , size int , repoPath , prefix string ) string {
name , err := generateCommitWithNewData ( size , repoPath , "user2@example.com" , "User Two" , prefix )
2018-01-16 16:37:47 +05:30
assert . NoError ( t , err )
2022-04-01 08:25:30 +05:30
_ , _ , err = git . NewCommand ( git . DefaultContext , "push" , "origin" , "master" ) . RunStdString ( & git . RunOpts { Dir : repoPath } ) // Push
2018-01-16 16:37:47 +05:30
assert . NoError ( t , err )
2019-02-12 20:39:43 +05:30
return name
2018-01-16 16:37:47 +05:30
}
2019-05-31 15:42:15 +05:30
func generateCommitWithNewData ( size int , repoPath , email , fullName , prefix string ) ( string , error ) {
2022-01-20 23:16:10 +05:30
// Generate random file
2020-04-27 16:50:09 +05:30
bufSize := 4 * 1024
if bufSize > size {
bufSize = size
2018-01-16 16:37:47 +05:30
}
2020-04-27 16:50:09 +05:30
buffer := make ( [ ] byte , bufSize )
2021-09-22 11:08:34 +05:30
tmpFile , err := os . CreateTemp ( repoPath , prefix )
2018-01-16 16:37:47 +05:30
if err != nil {
2019-02-12 20:39:43 +05:30
return "" , err
2018-01-16 16:37:47 +05:30
}
defer tmpFile . Close ( )
2020-04-27 16:50:09 +05:30
written := 0
for written < size {
n := size - written
if n > bufSize {
n = bufSize
}
_ , err := rand . Read ( buffer [ : n ] )
if err != nil {
return "" , err
}
n , err = tmpFile . Write ( buffer [ : n ] )
if err != nil {
return "" , err
}
written += n
}
2018-01-16 16:37:47 +05:30
if err != nil {
2019-02-12 20:39:43 +05:30
return "" , err
2018-01-16 16:37:47 +05:30
}
2022-01-20 23:16:10 +05:30
// Commit
2019-10-12 05:43:27 +05:30
// Now here we should explicitly allow lfs filters to run
2022-01-25 23:45:58 +05:30
globalArgs := git . AllowLFSFiltersArgs ( )
2019-11-27 06:05:52 +05:30
err = git . AddChangesWithArgs ( repoPath , globalArgs , false , filepath . Base ( tmpFile . Name ( ) ) )
2018-01-16 16:37:47 +05:30
if err != nil {
2019-02-12 20:39:43 +05:30
return "" , err
2018-01-16 16:37:47 +05:30
}
2019-11-27 06:05:52 +05:30
err = git . CommitChangesWithArgs ( repoPath , globalArgs , git . CommitChangesOptions {
2018-01-16 16:37:47 +05:30
Committer : & git . Signature {
Email : email ,
Name : fullName ,
When : time . Now ( ) ,
} ,
Author : & git . Signature {
Email : email ,
Name : fullName ,
When : time . Now ( ) ,
} ,
Message : fmt . Sprintf ( "Testing commit @ %v" , time . Now ( ) ) ,
} )
2019-02-12 20:39:43 +05:30
return filepath . Base ( tmpFile . Name ( ) ) , err
2018-01-16 16:37:47 +05:30
}
2019-05-31 15:42:15 +05:30
func doBranchProtectPRMerge ( baseCtx * APITestContext , dstPath string ) func ( t * testing . T ) {
return func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-05-31 15:42:15 +05:30
t . Run ( "CreateBranchProtected" , doGitCreateBranch ( dstPath , "protected" ) )
t . Run ( "PushProtectedBranch" , doGitPushTestRepository ( dstPath , "origin" , "protected" ) )
ctx := NewAPITestContext ( t , baseCtx . Username , baseCtx . Reponame )
2021-09-11 19:51:17 +05:30
t . Run ( "ProtectProtectedBranchNoWhitelist" , doProtectBranch ( ctx , "protected" , "" , "" ) )
2019-05-31 15:42:15 +05:30
t . Run ( "GenerateCommit" , func ( t * testing . T ) {
_ , err := generateCommitWithNewData ( littleSize , dstPath , "user2@example.com" , "User Two" , "branch-data-file-" )
assert . NoError ( t , err )
} )
t . Run ( "FailToPushToProtectedBranch" , doGitPushTestRepositoryFail ( dstPath , "origin" , "protected" ) )
t . Run ( "PushToUnprotectedBranch" , doGitPushTestRepository ( dstPath , "origin" , "protected:unprotected" ) )
var pr api . PullRequest
var err error
t . Run ( "CreatePullRequest" , func ( t * testing . T ) {
pr , err = doAPICreatePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , "protected" , "unprotected" ) ( t )
assert . NoError ( t , err )
} )
2020-02-21 23:48:13 +05:30
t . Run ( "GenerateCommit" , func ( t * testing . T ) {
_ , err := generateCommitWithNewData ( littleSize , dstPath , "user2@example.com" , "User Two" , "branch-data-file-" )
assert . NoError ( t , err )
} )
t . Run ( "PushToUnprotectedBranch" , doGitPushTestRepository ( dstPath , "origin" , "protected:unprotected-2" ) )
var pr2 api . PullRequest
t . Run ( "CreatePullRequest" , func ( t * testing . T ) {
pr2 , err = doAPICreatePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , "unprotected" , "unprotected-2" ) ( t )
assert . NoError ( t , err )
} )
t . Run ( "MergePR2" , doAPIMergePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , pr2 . Index ) )
2019-05-31 15:42:15 +05:30
t . Run ( "MergePR" , doAPIMergePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , pr . Index ) )
t . Run ( "PullProtected" , doGitPull ( dstPath , "origin" , "protected" ) )
2021-09-11 19:51:17 +05:30
t . Run ( "ProtectProtectedBranchUnprotectedFilePaths" , doProtectBranch ( ctx , "protected" , "" , "unprotected-file-*" ) )
t . Run ( "GenerateCommit" , func ( t * testing . T ) {
_ , err := generateCommitWithNewData ( littleSize , dstPath , "user2@example.com" , "User Two" , "unprotected-file-" )
assert . NoError ( t , err )
} )
t . Run ( "PushUnprotectedFilesToProtectedBranch" , doGitPushTestRepository ( dstPath , "origin" , "protected" ) )
t . Run ( "ProtectProtectedBranchWhitelist" , doProtectBranch ( ctx , "protected" , baseCtx . Username , "" ) )
2019-05-31 15:42:15 +05:30
t . Run ( "CheckoutMaster" , doGitCheckoutBranch ( dstPath , "master" ) )
t . Run ( "CreateBranchForced" , doGitCreateBranch ( dstPath , "toforce" ) )
t . Run ( "GenerateCommit" , func ( t * testing . T ) {
_ , err := generateCommitWithNewData ( littleSize , dstPath , "user2@example.com" , "User Two" , "branch-data-file-" )
assert . NoError ( t , err )
} )
t . Run ( "FailToForcePushToProtectedBranch" , doGitPushTestRepositoryFail ( dstPath , "-f" , "origin" , "toforce:protected" ) )
t . Run ( "MergeProtectedToToforce" , doGitMerge ( dstPath , "protected" ) )
t . Run ( "PushToProtectedBranch" , doGitPushTestRepository ( dstPath , "origin" , "toforce:protected" ) )
t . Run ( "CheckoutMasterAgain" , doGitCheckoutBranch ( dstPath , "master" ) )
}
}
2021-12-20 10:11:31 +05:30
func doProtectBranch ( ctx APITestContext , branch , userToWhitelist , unprotectedFilePatterns string ) func ( t * testing . T ) {
2019-05-31 15:42:15 +05:30
// We are going to just use the owner to set the protection.
return func ( t * testing . T ) {
csrf := GetCSRF ( t , ctx . Session , fmt . Sprintf ( "/%s/%s/settings/branches" , url . PathEscape ( ctx . Username ) , url . PathEscape ( ctx . Reponame ) ) )
if userToWhitelist == "" {
// Change branch to protected
req := NewRequestWithValues ( t , "POST" , fmt . Sprintf ( "/%s/%s/settings/branches/%s" , url . PathEscape ( ctx . Username ) , url . PathEscape ( ctx . Reponame ) , url . PathEscape ( branch ) ) , map [ string ] string {
2021-09-11 19:51:17 +05:30
"_csrf" : csrf ,
"protected" : "on" ,
"unprotected_file_patterns" : unprotectedFilePatterns ,
2019-05-31 15:42:15 +05:30
} )
2022-03-23 10:24:07 +05:30
ctx . Session . MakeRequest ( t , req , http . StatusSeeOther )
2019-05-31 15:42:15 +05:30
} else {
2022-05-20 19:38:52 +05:30
user , err := user_model . GetUserByName ( db . DefaultContext , userToWhitelist )
2019-05-31 15:42:15 +05:30
assert . NoError ( t , err )
// Change branch to protected
req := NewRequestWithValues ( t , "POST" , fmt . Sprintf ( "/%s/%s/settings/branches/%s" , url . PathEscape ( ctx . Username ) , url . PathEscape ( ctx . Reponame ) , url . PathEscape ( branch ) ) , map [ string ] string {
2021-09-11 19:51:17 +05:30
"_csrf" : csrf ,
"protected" : "on" ,
"enable_push" : "whitelist" ,
"enable_whitelist" : "on" ,
"whitelist_users" : strconv . FormatInt ( user . ID , 10 ) ,
"unprotected_file_patterns" : unprotectedFilePatterns ,
2019-05-31 15:42:15 +05:30
} )
2022-03-23 10:24:07 +05:30
ctx . Session . MakeRequest ( t , req , http . StatusSeeOther )
2019-05-31 15:42:15 +05:30
}
// Check if master branch has been locked successfully
flashCookie := ctx . Session . GetCookie ( "macaron_flash" )
assert . NotNil ( t , flashCookie )
assert . EqualValues ( t , "success%3DBranch%2Bprotection%2Bfor%2Bbranch%2B%2527" + url . QueryEscape ( branch ) + "%2527%2Bhas%2Bbeen%2Bupdated." , flashCookie . Value )
}
}
2019-06-22 23:05:34 +05:30
func doMergeFork ( ctx , baseCtx APITestContext , baseBranch , headBranch string ) func ( t * testing . T ) {
return func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2019-06-22 23:05:34 +05:30
var pr api . PullRequest
var err error
2020-04-28 14:02:23 +05:30
// Create a test pullrequest
2019-06-22 23:05:34 +05:30
t . Run ( "CreatePullRequest" , func ( t * testing . T ) {
pr , err = doAPICreatePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , baseBranch , headBranch ) ( t )
assert . NoError ( t , err )
} )
2020-04-28 14:02:23 +05:30
// Ensure the PR page works
t . Run ( "EnsureCanSeePull" , doEnsureCanSeePull ( baseCtx , pr ) )
// Then get the diff string
2021-04-06 22:14:02 +05:30
var diffHash string
2021-04-10 13:57:29 +05:30
var diffLength int
2020-04-03 18:51:41 +05:30
t . Run ( "GetDiff" , func ( t * testing . T ) {
req := NewRequest ( t , "GET" , fmt . Sprintf ( "/%s/%s/pulls/%d.diff" , url . PathEscape ( baseCtx . Username ) , url . PathEscape ( baseCtx . Reponame ) , pr . Index ) )
2021-04-06 22:14:02 +05:30
resp := ctx . Session . MakeRequestNilResponseHashSumRecorder ( t , req , http . StatusOK )
diffHash = string ( resp . Hash . Sum ( nil ) )
2021-04-10 13:57:29 +05:30
diffLength = resp . Length
2020-04-03 18:51:41 +05:30
} )
2020-04-28 14:02:23 +05:30
// Now: Merge the PR & make sure that doesn't break the PR page or change its diff
2019-06-22 23:05:34 +05:30
t . Run ( "MergePR" , doAPIMergePullRequest ( baseCtx , baseCtx . Username , baseCtx . Reponame , pr . Index ) )
2020-04-28 14:02:23 +05:30
t . Run ( "EnsureCanSeePull" , doEnsureCanSeePull ( baseCtx , pr ) )
2021-04-10 13:57:29 +05:30
t . Run ( "CheckPR" , func ( t * testing . T ) {
oldMergeBase := pr . MergeBase
pr2 , err := doAPIGetPullRequest ( baseCtx , baseCtx . Username , baseCtx . Reponame , pr . Index ) ( t )
assert . NoError ( t , err )
assert . Equal ( t , oldMergeBase , pr2 . MergeBase )
} )
t . Run ( "EnsurDiffNoChange" , doEnsureDiffNoChange ( baseCtx , pr , diffHash , diffLength ) )
2020-04-28 14:02:23 +05:30
// Then: Delete the head branch & make sure that doesn't break the PR page or change its diff
2020-04-03 18:51:41 +05:30
t . Run ( "DeleteHeadBranch" , doBranchDelete ( baseCtx , baseCtx . Username , baseCtx . Reponame , headBranch ) )
2020-04-28 14:02:23 +05:30
t . Run ( "EnsureCanSeePull" , doEnsureCanSeePull ( baseCtx , pr ) )
2021-04-10 13:57:29 +05:30
t . Run ( "EnsureDiffNoChange" , doEnsureDiffNoChange ( baseCtx , pr , diffHash , diffLength ) )
2020-04-28 14:02:23 +05:30
// Delete the head repository & make sure that doesn't break the PR page or change its diff
t . Run ( "DeleteHeadRepository" , doAPIDeleteRepository ( ctx ) )
t . Run ( "EnsureCanSeePull" , doEnsureCanSeePull ( baseCtx , pr ) )
2021-04-10 13:57:29 +05:30
t . Run ( "EnsureDiffNoChange" , doEnsureDiffNoChange ( baseCtx , pr , diffHash , diffLength ) )
2020-04-28 14:02:23 +05:30
}
}
2021-03-04 09:11:23 +05:30
func doCreatePRAndSetManuallyMerged ( ctx , baseCtx APITestContext , dstPath , baseBranch , headBranch string ) func ( t * testing . T ) {
return func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2021-03-04 09:11:23 +05:30
var (
pr api . PullRequest
err error
lastCommitID string
)
trueBool := true
falseBool := false
t . Run ( "AllowSetManuallyMergedAndSwitchOffAutodetectManualMerge" , doAPIEditRepository ( baseCtx , & api . EditRepoOption {
HasPullRequests : & trueBool ,
AllowManualMerge : & trueBool ,
AutodetectManualMerge : & falseBool ,
} ) )
t . Run ( "CreateHeadBranch" , doGitCreateBranch ( dstPath , headBranch ) )
2022-10-23 20:14:45 +05:30
t . Run ( "PushToHeadBranch" , doGitPushTestRepository ( dstPath , "origin" , git . CmdArgCheck ( headBranch ) ) )
2021-03-04 09:11:23 +05:30
t . Run ( "CreateEmptyPullRequest" , func ( t * testing . T ) {
pr , err = doAPICreatePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , baseBranch , headBranch ) ( t )
assert . NoError ( t , err )
} )
lastCommitID = pr . Base . Sha
t . Run ( "ManuallyMergePR" , doAPIManuallyMergePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , lastCommitID , pr . Index ) )
}
}
2020-04-28 14:02:23 +05:30
func doEnsureCanSeePull ( ctx APITestContext , pr api . PullRequest ) func ( t * testing . T ) {
return func ( t * testing . T ) {
req := NewRequest ( t , "GET" , fmt . Sprintf ( "/%s/%s/pulls/%d" , url . PathEscape ( ctx . Username ) , url . PathEscape ( ctx . Reponame ) , pr . Index ) )
ctx . Session . MakeRequest ( t , req , http . StatusOK )
req = NewRequest ( t , "GET" , fmt . Sprintf ( "/%s/%s/pulls/%d/files" , url . PathEscape ( ctx . Username ) , url . PathEscape ( ctx . Reponame ) , pr . Index ) )
ctx . Session . MakeRequest ( t , req , http . StatusOK )
req = NewRequest ( t , "GET" , fmt . Sprintf ( "/%s/%s/pulls/%d/commits" , url . PathEscape ( ctx . Username ) , url . PathEscape ( ctx . Reponame ) , pr . Index ) )
ctx . Session . MakeRequest ( t , req , http . StatusOK )
}
}
2021-04-10 13:57:29 +05:30
func doEnsureDiffNoChange ( ctx APITestContext , pr api . PullRequest , diffHash string , diffLength int ) func ( t * testing . T ) {
2020-04-28 14:02:23 +05:30
return func ( t * testing . T ) {
req := NewRequest ( t , "GET" , fmt . Sprintf ( "/%s/%s/pulls/%d.diff" , url . PathEscape ( ctx . Username ) , url . PathEscape ( ctx . Reponame ) , pr . Index ) )
2021-04-06 22:14:02 +05:30
resp := ctx . Session . MakeRequestNilResponseHashSumRecorder ( t , req , http . StatusOK )
actual := string ( resp . Hash . Sum ( nil ) )
2021-04-10 13:57:29 +05:30
actualLength := resp . Length
2021-03-22 01:21:54 +05:30
2021-04-06 22:14:02 +05:30
equal := diffHash == actual
2021-04-10 13:57:29 +05:30
assert . True ( t , equal , "Unexpected change in the diff string: expected hash: %s size: %d but was actually: %s size: %d" , hex . EncodeToString ( [ ] byte ( diffHash ) ) , diffLength , hex . EncodeToString ( [ ] byte ( actual ) ) , actualLength )
2019-06-22 23:05:34 +05:30
}
}
2019-12-15 08:19:52 +05:30
func doPushCreate ( ctx APITestContext , u * url . URL ) func ( t * testing . T ) {
return func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2020-04-28 14:02:23 +05:30
// create a context for a currently non-existent repository
2019-12-15 08:19:52 +05:30
ctx . Reponame = fmt . Sprintf ( "repo-tmp-push-create-%s" , u . Scheme )
u . Path = ctx . GitPath ( )
2020-04-28 14:02:23 +05:30
// Create a temporary directory
2022-09-04 20:44:53 +05:30
tmpDir := t . TempDir ( )
2019-12-15 08:19:52 +05:30
2020-04-28 14:02:23 +05:30
// Now create local repository to push as our test and set its origin
t . Run ( "InitTestRepository" , doGitInitTestRepository ( tmpDir ) )
t . Run ( "AddRemote" , doGitAddRemote ( tmpDir , "origin" , u ) )
2019-12-15 08:19:52 +05:30
2020-04-28 14:02:23 +05:30
// Disable "Push To Create" and attempt to push
2019-12-15 08:19:52 +05:30
setting . Repository . EnablePushCreateUser = false
2020-04-28 14:02:23 +05:30
t . Run ( "FailToPushAndCreateTestRepository" , doGitPushTestRepositoryFail ( tmpDir , "origin" , "master" ) )
2019-12-15 08:19:52 +05:30
2020-04-28 14:02:23 +05:30
// Enable "Push To Create"
2019-12-15 08:19:52 +05:30
setting . Repository . EnablePushCreateUser = true
2020-02-05 15:10:35 +05:30
2020-04-28 14:02:23 +05:30
// Assert that cloning from a non-existent repository does not create it and that it definitely wasn't create above
t . Run ( "FailToCloneFromNonExistentRepository" , doGitCloneFail ( u ) )
2020-02-05 15:10:35 +05:30
2020-04-28 14:02:23 +05:30
// Then "Push To Create"x
t . Run ( "SuccessfullyPushAndCreateTestRepository" , doGitPushTestRepository ( tmpDir , "origin" , "master" ) )
2019-12-15 08:19:52 +05:30
2020-04-28 14:02:23 +05:30
// Finally, fetch repo from database and ensure the correct repository has been created
2021-12-10 06:57:50 +05:30
repo , err := repo_model . GetRepositoryByOwnerAndName ( ctx . Username , ctx . Reponame )
2019-12-15 08:19:52 +05:30
assert . NoError ( t , err )
assert . False ( t , repo . IsEmpty )
assert . True ( t , repo . IsPrivate )
2020-04-28 14:02:23 +05:30
// Now add a remote that is invalid to "Push To Create"
invalidCtx := ctx
invalidCtx . Reponame = fmt . Sprintf ( "invalid/repo-tmp-push-create-%s" , u . Scheme )
u . Path = invalidCtx . GitPath ( )
t . Run ( "AddInvalidRemote" , doGitAddRemote ( tmpDir , "invalid" , u ) )
// Fail to "Push To Create" the invalid
t . Run ( "FailToPushAndCreateInvalidTestRepository" , doGitPushTestRepositoryFail ( tmpDir , "invalid" , "master" ) )
2019-12-15 08:19:52 +05:30
}
}
2020-04-03 18:51:41 +05:30
func doBranchDelete ( ctx APITestContext , owner , repo , branch string ) func ( * testing . T ) {
return func ( t * testing . T ) {
csrf := GetCSRF ( t , ctx . Session , fmt . Sprintf ( "/%s/%s/branches" , url . PathEscape ( owner ) , url . PathEscape ( repo ) ) )
req := NewRequestWithValues ( t , "POST" , fmt . Sprintf ( "/%s/%s/branches/delete?name=%s" , url . PathEscape ( owner ) , url . PathEscape ( repo ) , url . QueryEscape ( branch ) ) , map [ string ] string {
"_csrf" : csrf ,
} )
ctx . Session . MakeRequest ( t , req , http . StatusOK )
}
}
2021-07-28 15:12:56 +05:30
2022-05-07 22:35:52 +05:30
func doAutoPRMerge ( baseCtx * APITestContext , dstPath string ) func ( t * testing . T ) {
return func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2022-05-07 22:35:52 +05:30
ctx := NewAPITestContext ( t , baseCtx . Username , baseCtx . Reponame )
t . Run ( "CheckoutProtected" , doGitCheckoutBranch ( dstPath , "protected" ) )
t . Run ( "PullProtected" , doGitPull ( dstPath , "origin" , "protected" ) )
t . Run ( "GenerateCommit" , func ( t * testing . T ) {
_ , err := generateCommitWithNewData ( littleSize , dstPath , "user2@example.com" , "User Two" , "branch-data-file-" )
assert . NoError ( t , err )
} )
t . Run ( "PushToUnprotectedBranch" , doGitPushTestRepository ( dstPath , "origin" , "protected:unprotected3" ) )
var pr api . PullRequest
var err error
t . Run ( "CreatePullRequest" , func ( t * testing . T ) {
pr , err = doAPICreatePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , "protected" , "unprotected3" ) ( t )
assert . NoError ( t , err )
} )
// Request repository commits page
req := NewRequest ( t , "GET" , fmt . Sprintf ( "/%s/%s/pulls/%d/commits" , baseCtx . Username , baseCtx . Reponame , pr . Index ) )
resp := ctx . Session . MakeRequest ( t , req , http . StatusOK )
doc := NewHTMLParser ( t , resp . Body )
// Get first commit URL
commitURL , exists := doc . doc . Find ( "#commits-table tbody tr td.sha a" ) . Last ( ) . Attr ( "href" )
assert . True ( t , exists )
assert . NotEmpty ( t , commitURL )
commitID := path . Base ( commitURL )
// Call API to add Pending status for commit
t . Run ( "CreateStatus" , doAPICreateCommitStatus ( ctx , commitID , api . CommitStatusPending ) )
// Cancel not existing auto merge
ctx . ExpectedCode = http . StatusNotFound
t . Run ( "CancelAutoMergePR" , doAPICancelAutoMergePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , pr . Index ) )
// Add auto merge request
ctx . ExpectedCode = http . StatusCreated
t . Run ( "AutoMergePR" , doAPIAutoMergePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , pr . Index ) )
// Can not create schedule twice
ctx . ExpectedCode = http . StatusConflict
t . Run ( "AutoMergePRTwice" , doAPIAutoMergePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , pr . Index ) )
// Cancel auto merge request
ctx . ExpectedCode = http . StatusNoContent
t . Run ( "CancelAutoMergePR" , doAPICancelAutoMergePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , pr . Index ) )
// Add auto merge request
ctx . ExpectedCode = http . StatusCreated
t . Run ( "AutoMergePR" , doAPIAutoMergePullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , pr . Index ) )
// Check pr status
ctx . ExpectedCode = 0
pr , err = doAPIGetPullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , pr . Index ) ( t )
assert . NoError ( t , err )
assert . False ( t , pr . HasMerged )
// Call API to add Failure status for commit
t . Run ( "CreateStatus" , doAPICreateCommitStatus ( ctx , commitID , api . CommitStatusFailure ) )
// Check pr status
pr , err = doAPIGetPullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , pr . Index ) ( t )
assert . NoError ( t , err )
assert . False ( t , pr . HasMerged )
// Call API to add Success status for commit
t . Run ( "CreateStatus" , doAPICreateCommitStatus ( ctx , commitID , api . CommitStatusSuccess ) )
// wait to let gitea merge stuff
time . Sleep ( time . Second )
// test pr status
pr , err = doAPIGetPullRequest ( ctx , baseCtx . Username , baseCtx . Reponame , pr . Index ) ( t )
assert . NoError ( t , err )
assert . True ( t , pr . HasMerged )
}
}
2021-07-28 15:12:56 +05:30
func doCreateAgitFlowPull ( dstPath string , ctx * APITestContext , baseBranch , headBranch string ) func ( t * testing . T ) {
return func ( t * testing . T ) {
2022-09-03 00:48:23 +05:30
defer tests . PrintCurrentTest ( t ) ( )
2021-07-28 15:12:56 +05:30
// skip this test if git version is low
if git . CheckGitVersionAtLeast ( "2.29" ) != nil {
return
}
2022-03-30 00:43:41 +05:30
gitRepo , err := git . OpenRepository ( git . DefaultContext , dstPath )
2021-07-28 15:12:56 +05:30
if ! assert . NoError ( t , err ) {
return
}
defer gitRepo . Close ( )
var (
2022-06-13 15:07:59 +05:30
pr1 , pr2 * issues_model . PullRequest
2021-07-28 15:12:56 +05:30
commit string
)
2021-12-10 06:57:50 +05:30
repo , err := repo_model . GetRepositoryByOwnerAndName ( ctx . Username , ctx . Reponame )
2021-07-28 15:12:56 +05:30
if ! assert . NoError ( t , err ) {
return
}
2022-06-13 15:07:59 +05:30
pullNum := unittest . GetCount ( t , & issues_model . PullRequest { } )
2021-07-28 15:12:56 +05:30
t . Run ( "CreateHeadBranch" , doGitCreateBranch ( dstPath , headBranch ) )
t . Run ( "AddCommit" , func ( t * testing . T ) {
2022-01-20 23:16:10 +05:30
err := os . WriteFile ( path . Join ( dstPath , "test_file" ) , [ ] byte ( "## test content" ) , 0 o666 )
2021-07-28 15:12:56 +05:30
if ! assert . NoError ( t , err ) {
return
}
err = git . AddChanges ( dstPath , true )
assert . NoError ( t , err )
err = git . CommitChanges ( dstPath , git . CommitChangesOptions {
Committer : & git . Signature {
Email : "user2@example.com" ,
Name : "user2" ,
When : time . Now ( ) ,
} ,
Author : & git . Signature {
Email : "user2@example.com" ,
Name : "user2" ,
When : time . Now ( ) ,
} ,
Message : "Testing commit 1" ,
} )
assert . NoError ( t , err )
commit , err = gitRepo . GetRefCommitID ( "HEAD" )
assert . NoError ( t , err )
} )
t . Run ( "Push" , func ( t * testing . T ) {
2022-10-23 20:14:45 +05:30
err := git . NewCommand ( git . DefaultContext , "push" , "origin" , "HEAD:refs/for/master" , "-o" ) . AddDynamicArguments ( "topic=" + headBranch ) . Run ( & git . RunOpts { Dir : dstPath } )
2021-07-28 15:12:56 +05:30
if ! assert . NoError ( t , err ) {
return
}
2022-06-13 15:07:59 +05:30
unittest . AssertCount ( t , & issues_model . PullRequest { } , pullNum + 1 )
pr1 = unittest . AssertExistsAndLoadBean ( t , & issues_model . PullRequest {
2021-07-28 15:12:56 +05:30
HeadRepoID : repo . ID ,
2022-06-13 15:07:59 +05:30
Flow : issues_model . PullRequestFlowAGit ,
2022-08-16 07:52:25 +05:30
} )
2021-07-28 15:12:56 +05:30
if ! assert . NotEmpty ( t , pr1 ) {
return
}
prMsg , err := doAPIGetPullRequest ( * ctx , ctx . Username , ctx . Reponame , pr1 . Index ) ( t )
if ! assert . NoError ( t , err ) {
return
}
assert . Equal ( t , "user2/" + headBranch , pr1 . HeadBranch )
assert . Equal ( t , false , prMsg . HasMerged )
assert . Contains ( t , "Testing commit 1" , prMsg . Body )
assert . Equal ( t , commit , prMsg . Head . Sha )
2022-10-23 20:14:45 +05:30
_ , _ , err = git . NewCommand ( git . DefaultContext , "push" , "origin" ) . AddDynamicArguments ( "HEAD:refs/for/master/test/" + headBranch ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2021-07-28 15:12:56 +05:30
if ! assert . NoError ( t , err ) {
return
}
2022-06-13 15:07:59 +05:30
unittest . AssertCount ( t , & issues_model . PullRequest { } , pullNum + 2 )
pr2 = unittest . AssertExistsAndLoadBean ( t , & issues_model . PullRequest {
2021-07-28 15:12:56 +05:30
HeadRepoID : repo . ID ,
Index : pr1 . Index + 1 ,
2022-06-13 15:07:59 +05:30
Flow : issues_model . PullRequestFlowAGit ,
2022-08-16 07:52:25 +05:30
} )
2021-07-28 15:12:56 +05:30
if ! assert . NotEmpty ( t , pr2 ) {
return
}
prMsg , err = doAPIGetPullRequest ( * ctx , ctx . Username , ctx . Reponame , pr2 . Index ) ( t )
if ! assert . NoError ( t , err ) {
return
}
assert . Equal ( t , "user2/test/" + headBranch , pr2 . HeadBranch )
assert . Equal ( t , false , prMsg . HasMerged )
} )
if pr1 == nil || pr2 == nil {
return
}
t . Run ( "AddCommit2" , func ( t * testing . T ) {
2022-01-20 23:16:10 +05:30
err := os . WriteFile ( path . Join ( dstPath , "test_file" ) , [ ] byte ( "## test content \n ## test content 2" ) , 0 o666 )
2021-07-28 15:12:56 +05:30
if ! assert . NoError ( t , err ) {
return
}
err = git . AddChanges ( dstPath , true )
assert . NoError ( t , err )
err = git . CommitChanges ( dstPath , git . CommitChangesOptions {
Committer : & git . Signature {
Email : "user2@example.com" ,
Name : "user2" ,
When : time . Now ( ) ,
} ,
Author : & git . Signature {
Email : "user2@example.com" ,
Name : "user2" ,
When : time . Now ( ) ,
} ,
Message : "Testing commit 2" ,
} )
assert . NoError ( t , err )
commit , err = gitRepo . GetRefCommitID ( "HEAD" )
assert . NoError ( t , err )
} )
t . Run ( "Push2" , func ( t * testing . T ) {
2022-10-23 20:14:45 +05:30
err := git . NewCommand ( git . DefaultContext , "push" , "origin" , "HEAD:refs/for/master" , "-o" ) . AddDynamicArguments ( "topic=" + headBranch ) . Run ( & git . RunOpts { Dir : dstPath } )
2021-07-28 15:12:56 +05:30
if ! assert . NoError ( t , err ) {
return
}
2022-06-13 15:07:59 +05:30
unittest . AssertCount ( t , & issues_model . PullRequest { } , pullNum + 2 )
2021-07-28 15:12:56 +05:30
prMsg , err := doAPIGetPullRequest ( * ctx , ctx . Username , ctx . Reponame , pr1 . Index ) ( t )
if ! assert . NoError ( t , err ) {
return
}
assert . Equal ( t , false , prMsg . HasMerged )
assert . Equal ( t , commit , prMsg . Head . Sha )
2022-10-23 20:14:45 +05:30
_ , _ , err = git . NewCommand ( git . DefaultContext , "push" , "origin" ) . AddDynamicArguments ( "HEAD:refs/for/master/test/" + headBranch ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2021-07-28 15:12:56 +05:30
if ! assert . NoError ( t , err ) {
return
}
2022-06-13 15:07:59 +05:30
unittest . AssertCount ( t , & issues_model . PullRequest { } , pullNum + 2 )
2021-07-28 15:12:56 +05:30
prMsg , err = doAPIGetPullRequest ( * ctx , ctx . Username , ctx . Reponame , pr2 . Index ) ( t )
if ! assert . NoError ( t , err ) {
return
}
assert . Equal ( t , false , prMsg . HasMerged )
assert . Equal ( t , commit , prMsg . Head . Sha )
} )
t . Run ( "Merge" , doAPIMergePullRequest ( * ctx , ctx . Username , ctx . Reponame , pr1 . Index ) )
t . Run ( "CheckoutMasterAgain" , doGitCheckoutBranch ( dstPath , "master" ) )
}
}