2021-06-14 20:20:43 +03:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-14 20:20:43 +03:00
|
|
|
|
2022-09-02 22:18:23 +03:00
|
|
|
package integration
|
2021-06-14 20:20:43 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-12-21 06:12:27 +03:00
|
|
|
"strconv"
|
2021-06-14 20:20:43 +03:00
|
|
|
"testing"
|
|
|
|
|
2022-07-30 19:45:59 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 04:27:50 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-16 11:53:21 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 12:49:20 +03:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-06-14 20:20:43 +03:00
|
|
|
"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 23:09:51 +03:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2021-06-14 20:20:43 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-02-27 10:12:22 +03:00
|
|
|
gitea_context "code.gitea.io/gitea/services/context"
|
2023-12-01 16:07:36 +03:00
|
|
|
doctor "code.gitea.io/gitea/services/doctor"
|
2021-11-20 12:34:05 +03:00
|
|
|
"code.gitea.io/gitea/services/migrations"
|
2021-06-14 20:20:43 +03:00
|
|
|
mirror_service "code.gitea.io/gitea/services/mirror"
|
2023-09-06 15:08:51 +03:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2022-09-02 22:18:23 +03:00
|
|
|
"code.gitea.io/gitea/tests"
|
2021-06-14 20:20:43 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:41:27 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-14 20:20:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMirrorPush(t *testing.T) {
|
|
|
|
onGiteaRun(t, testMirrorPush)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMirrorPush(t *testing.T, u *url.URL) {
|
2022-09-02 22:18:23 +03:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-06-14 20:20:43 +03:00
|
|
|
|
|
|
|
setting.Migrations.AllowLocalNetworks = true
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, migrations.Init())
|
2021-06-14 20:20:43 +03:00
|
|
|
|
2022-08-16 05:22:25 +03:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-06-14 20:20:43 +03:00
|
|
|
|
2023-09-08 07:51:15 +03:00
|
|
|
mirrorRepo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
|
2021-06-14 20:20:43 +03:00
|
|
|
Name: "test-push-mirror",
|
|
|
|
})
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
2021-06-14 20:20:43 +03:00
|
|
|
|
|
|
|
ctx := NewAPITestContext(t, user.LowerName, srcRepo.Name)
|
|
|
|
|
|
|
|
doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword)(t)
|
2023-12-01 16:07:36 +03:00
|
|
|
doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape("does-not-matter")), user.LowerName, userPassword)(t)
|
2021-06-14 20:20:43 +03:00
|
|
|
|
2022-07-30 19:45:59 +03:00
|
|
|
mirrors, _, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
2023-12-01 16:07:36 +03:00
|
|
|
assert.Len(t, mirrors, 2)
|
2021-06-14 20:20:43 +03:00
|
|
|
|
|
|
|
ok := mirror_service.SyncPushMirror(context.Background(), mirrors[0].ID)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
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 23:09:51 +03:00
|
|
|
srcGitRepo, err := gitrepo.OpenRepository(git.DefaultContext, srcRepo)
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
2021-06-14 20:20:43 +03:00
|
|
|
defer srcGitRepo.Close()
|
|
|
|
|
|
|
|
srcCommit, err := srcGitRepo.GetBranchCommit("master")
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
2021-06-14 20:20:43 +03:00
|
|
|
|
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 23:09:51 +03:00
|
|
|
mirrorGitRepo, err := gitrepo.OpenRepository(git.DefaultContext, mirrorRepo)
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
2021-06-14 20:20:43 +03:00
|
|
|
defer mirrorGitRepo.Close()
|
|
|
|
|
|
|
|
mirrorCommit, err := mirrorGitRepo.GetBranchCommit("master")
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
2021-06-14 20:20:43 +03:00
|
|
|
|
|
|
|
assert.Equal(t, srcCommit.ID, mirrorCommit.ID)
|
2021-12-21 06:12:27 +03:00
|
|
|
|
2023-12-01 16:07:36 +03:00
|
|
|
// Test that we can "repair" push mirrors where the remote doesn't exist in git's state.
|
|
|
|
// To do that, we artificially remove the remote...
|
|
|
|
cmd := git.NewCommand(db.DefaultContext, "remote", "rm").AddDynamicArguments(mirrors[0].RemoteName)
|
|
|
|
_, _, err = cmd.RunStdString(&git.RunOpts{Dir: srcRepo.RepoPath()})
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
2023-12-01 16:07:36 +03:00
|
|
|
|
|
|
|
// ...then ensure that trying to get its remote address fails
|
|
|
|
_, err = repo_model.GetPushMirrorRemoteAddress(srcRepo.OwnerName, srcRepo.Name, mirrors[0].RemoteName)
|
2024-07-30 22:41:27 +03:00
|
|
|
require.Error(t, err)
|
2023-12-01 16:07:36 +03:00
|
|
|
|
|
|
|
// ...and that we can fix it.
|
|
|
|
err = doctor.FixPushMirrorsWithoutGitRemote(db.DefaultContext, nil, true)
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
2023-12-01 16:07:36 +03:00
|
|
|
|
|
|
|
// ...and after fixing, we only have one remote
|
|
|
|
mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
2023-12-01 16:07:36 +03:00
|
|
|
assert.Len(t, mirrors, 1)
|
|
|
|
|
|
|
|
// ...one we can get the address of, and it's not the one we removed
|
|
|
|
remoteAddress, err := repo_model.GetPushMirrorRemoteAddress(srcRepo.OwnerName, srcRepo.Name, mirrors[0].RemoteName)
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
2023-12-01 16:07:36 +03:00
|
|
|
assert.Contains(t, remoteAddress, "does-not-matter")
|
|
|
|
|
2021-12-21 06:12:27 +03:00
|
|
|
// Cleanup
|
|
|
|
doRemovePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword, int(mirrors[0].ID))(t)
|
2022-07-30 19:45:59 +03:00
|
|
|
mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
2024-07-30 22:41:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, mirrors)
|
2021-06-14 20:20:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func doCreatePushMirror(ctx APITestContext, address, username, password string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
"action": "push-mirror-add",
|
|
|
|
"push_mirror_address": address,
|
|
|
|
"push_mirror_username": username,
|
|
|
|
"push_mirror_password": password,
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
2022-03-23 07:54:07 +03:00
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-06-14 20:20:43 +03:00
|
|
|
|
2023-04-13 22:45:33 +03:00
|
|
|
flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash)
|
2021-06-14 20:20:43 +03:00
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 06:12:27 +03:00
|
|
|
|
|
|
|
func doRemovePushMirror(ctx APITestContext, address, username, password string, pushMirrorID int) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
"action": "push-mirror-remove",
|
|
|
|
"push_mirror_id": strconv.Itoa(pushMirrorID),
|
|
|
|
"push_mirror_address": address,
|
|
|
|
"push_mirror_username": username,
|
|
|
|
"push_mirror_password": password,
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
2022-03-23 07:54:07 +03:00
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-12-21 06:12:27 +03:00
|
|
|
|
2023-04-13 22:45:33 +03:00
|
|
|
flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash)
|
2021-12-21 06:12:27 +03:00
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
}
|
|
|
|
}
|