2019-12-14 22:28:51 -05:00
// Copyright 2019 The Gitea Authors.
// All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2019-12-14 22:28:51 -05:00
package pull
2021-06-25 12:01:43 -05:00
import (
"testing"
2022-11-19 03:12:33 -05:00
"code.gitea.io/gitea/models/db"
2022-06-13 04:37:59 -05:00
issues_model "code.gitea.io/gitea/models/issues"
2022-05-08 07:32:45 -05:00
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git"
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 15:09:51 -05:00
"code.gitea.io/gitea/modules/gitrepo"
2022-05-08 07:32:45 -05:00
2021-06-25 12:01:43 -05:00
"github.com/stretchr/testify/assert"
2024-07-30 14:41:10 -05:00
"github.com/stretchr/testify/require"
2021-06-25 12:01:43 -05:00
)
2019-12-14 22:28:51 -05:00
// TODO TestPullRequest_PushToBaseRepo
2021-06-25 12:01:43 -05:00
func TestPullRequest_CommitMessageTrailersPattern ( t * testing . T ) {
// Not a valid trailer section
assert . False ( t , commitMessageTrailersPattern . MatchString ( "" ) )
assert . False ( t , commitMessageTrailersPattern . MatchString ( "No trailer." ) )
assert . False ( t , commitMessageTrailersPattern . MatchString ( "Signed-off-by: Bob <bob@example.com>\nNot a trailer due to following text." ) )
assert . False ( t , commitMessageTrailersPattern . MatchString ( "Message body not correctly separated from trailer section by empty line.\nSigned-off-by: Bob <bob@example.com>" ) )
// Valid trailer section
assert . True ( t , commitMessageTrailersPattern . MatchString ( "Signed-off-by: Bob <bob@example.com>" ) )
assert . True ( t , commitMessageTrailersPattern . MatchString ( "Signed-off-by: Bob <bob@example.com>\nOther-Trailer: Value" ) )
assert . True ( t , commitMessageTrailersPattern . MatchString ( "Message body correctly separated from trailer section by empty line.\n\nSigned-off-by: Bob <bob@example.com>" ) )
assert . True ( t , commitMessageTrailersPattern . MatchString ( "Multiple trailers.\n\nSigned-off-by: Bob <bob@example.com>\nOther-Trailer: Value" ) )
assert . True ( t , commitMessageTrailersPattern . MatchString ( "Newline after trailer section.\n\nSigned-off-by: Bob <bob@example.com>\n" ) )
assert . True ( t , commitMessageTrailersPattern . MatchString ( "No space after colon is accepted.\n\nSigned-off-by:Bob <bob@example.com>" ) )
assert . True ( t , commitMessageTrailersPattern . MatchString ( "Additional whitespace is accepted.\n\nSigned-off-by \t : \tBob <bob@example.com> " ) )
assert . True ( t , commitMessageTrailersPattern . MatchString ( "Folded value.\n\nFolded-trailer: This is\n a folded\n trailer value\nOther-Trailer: Value" ) )
}
2022-05-08 07:32:45 -05:00
func TestPullRequest_GetDefaultMergeMessage_InternalTracker ( t * testing . T ) {
2024-07-30 14:41:10 -05:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-15 21:22:25 -05:00
pr := unittest . AssertExistsAndLoadBean ( t , & issues_model . PullRequest { ID : 2 } )
2022-05-08 07:32:45 -05:00
2024-07-30 14:41:10 -05:00
require . NoError ( t , pr . LoadBaseRepo ( db . DefaultContext ) )
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 15:09:51 -05:00
gitRepo , err := gitrepo . OpenRepository ( git . DefaultContext , pr . BaseRepo )
2024-07-30 14:41:10 -05:00
require . NoError ( t , err )
2022-05-08 07:32:45 -05:00
defer gitRepo . Close ( )
2024-07-31 06:55:14 -05:00
mergeMessage , body , err := GetDefaultMergeMessage ( db . DefaultContext , gitRepo , pr , "" )
2024-07-30 14:41:10 -05:00
require . NoError ( t , err )
2022-05-08 07:32:45 -05:00
assert . Equal ( t , "Merge pull request 'issue3' (#3) from branch2 into master" , mergeMessage )
2024-07-31 06:55:14 -05:00
assert . Equal ( t , "Reviewed-on: https://try.gitea.io/user2/repo1/pulls/3\n" , body )
2022-05-08 07:32:45 -05:00
pr . BaseRepoID = 1
pr . HeadRepoID = 2
2022-12-29 07:40:20 -05:00
mergeMessage , _ , err = GetDefaultMergeMessage ( db . DefaultContext , gitRepo , pr , "" )
2024-07-30 14:41:10 -05:00
require . NoError ( t , err )
2022-05-08 07:32:45 -05:00
assert . Equal ( t , "Merge pull request 'issue3' (#3) from user2/repo1:branch2 into master" , mergeMessage )
}
func TestPullRequest_GetDefaultMergeMessage_ExternalTracker ( t * testing . T ) {
2024-07-30 14:41:10 -05:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-05-08 07:32:45 -05:00
externalTracker := repo_model . RepoUnit {
Type : unit . TypeExternalTracker ,
Config : & repo_model . ExternalTrackerConfig {
ExternalTrackerFormat : "https://someurl.com/{user}/{repo}/{issue}" ,
} ,
}
2022-08-15 21:22:25 -05:00
baseRepo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } )
2022-05-08 07:32:45 -05:00
baseRepo . Units = [ ] * repo_model . RepoUnit { & externalTracker }
2022-08-15 21:22:25 -05:00
pr := unittest . AssertExistsAndLoadBean ( t , & issues_model . PullRequest { ID : 2 , BaseRepo : baseRepo } )
2022-05-08 07:32:45 -05:00
2024-07-30 14:41:10 -05:00
require . NoError ( t , pr . LoadBaseRepo ( db . DefaultContext ) )
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 15:09:51 -05:00
gitRepo , err := gitrepo . OpenRepository ( git . DefaultContext , pr . BaseRepo )
2024-07-30 14:41:10 -05:00
require . NoError ( t , err )
2022-05-08 07:32:45 -05:00
defer gitRepo . Close ( )
2022-12-29 07:40:20 -05:00
mergeMessage , _ , err := GetDefaultMergeMessage ( db . DefaultContext , gitRepo , pr , "" )
2024-07-30 14:41:10 -05:00
require . NoError ( t , err )
2022-05-08 07:32:45 -05:00
assert . Equal ( t , "Merge pull request 'issue3' (!3) from branch2 into master" , mergeMessage )
pr . BaseRepoID = 1
pr . HeadRepoID = 2
pr . BaseRepo = nil
pr . HeadRepo = nil
2022-12-29 07:40:20 -05:00
mergeMessage , _ , err = GetDefaultMergeMessage ( db . DefaultContext , gitRepo , pr , "" )
2024-07-30 14:41:10 -05:00
require . NoError ( t , err )
2022-05-08 07:32:45 -05:00
assert . Equal ( t , "Merge pull request 'issue3' (#3) from user2/repo2:branch2 into master" , mergeMessage )
}