2021-06-15 01:20:43 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-15 01:20:43 +08:00
|
|
|
|
2022-09-03 03:18:23 +08:00
|
|
|
package integration
|
2021-06-15 01:20:43 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-12-21 11:12:27 +08:00
|
|
|
"strconv"
|
2024-11-18 13:59:04 +08:00
|
|
|
"strings"
|
2021-06-15 01:20:43 +08:00
|
|
|
"testing"
|
2024-11-18 13:59:04 +08:00
|
|
|
"time"
|
2021-06-15 01:20:43 +08:00
|
|
|
|
2022-07-31 00:45:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-16 16:53:21 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-06-15 01:20:43 +08: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-28 04:09:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2021-06-15 01:20:43 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-02-27 15:12:22 +08:00
|
|
|
gitea_context "code.gitea.io/gitea/services/context"
|
2021-11-20 17:34:05 +08:00
|
|
|
"code.gitea.io/gitea/services/migrations"
|
2021-06-15 01:20:43 +08:00
|
|
|
mirror_service "code.gitea.io/gitea/services/mirror"
|
2023-09-06 20:08:51 +08:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2022-09-03 03:18:23 +08:00
|
|
|
"code.gitea.io/gitea/tests"
|
2021-06-15 01:20:43 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMirrorPush(t *testing.T) {
|
|
|
|
onGiteaRun(t, testMirrorPush)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMirrorPush(t *testing.T, u *url.URL) {
|
|
|
|
setting.Migrations.AllowLocalNetworks = true
|
2021-11-20 17:34:05 +08:00
|
|
|
assert.NoError(t, migrations.Init())
|
2021-06-15 01:20:43 +08:00
|
|
|
|
2024-11-18 13:59:04 +08:00
|
|
|
_ = db.TruncateBeans(db.DefaultContext, &repo_model.PushMirror{})
|
2022-08-16 10:22:25 +08:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-06-15 01:20:43 +08:00
|
|
|
|
2023-09-08 12:51:15 +08:00
|
|
|
mirrorRepo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
|
2021-06-15 01:20:43 +08:00
|
|
|
Name: "test-push-mirror",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-11-18 13:59:04 +08:00
|
|
|
session := loginUser(t, user.Name)
|
2021-06-15 01:20:43 +08:00
|
|
|
|
2024-11-18 13:59:04 +08:00
|
|
|
pushMirrorURL := fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(user.Name), url.PathEscape(mirrorRepo.Name))
|
|
|
|
testCreatePushMirror(t, session, user.Name, srcRepo.Name, pushMirrorURL, user.LowerName, userPassword, "0")
|
2021-06-15 01:20:43 +08:00
|
|
|
|
2022-07-31 00:45:59 +08:00
|
|
|
mirrors, _, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
2021-06-15 01:20:43 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, mirrors, 1)
|
|
|
|
|
|
|
|
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-28 04:09:51 +08:00
|
|
|
srcGitRepo, err := gitrepo.OpenRepository(git.DefaultContext, srcRepo)
|
2021-06-15 01:20:43 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer srcGitRepo.Close()
|
|
|
|
|
|
|
|
srcCommit, err := srcGitRepo.GetBranchCommit("master")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
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-28 04:09:51 +08:00
|
|
|
mirrorGitRepo, err := gitrepo.OpenRepository(git.DefaultContext, mirrorRepo)
|
2021-06-15 01:20:43 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer mirrorGitRepo.Close()
|
|
|
|
|
|
|
|
mirrorCommit, err := mirrorGitRepo.GetBranchCommit("master")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, srcCommit.ID, mirrorCommit.ID)
|
2021-12-21 11:12:27 +08:00
|
|
|
|
|
|
|
// Cleanup
|
2024-11-18 13:59:04 +08:00
|
|
|
assert.True(t, doRemovePushMirror(t, session, user.Name, srcRepo.Name, mirrors[0].ID))
|
2022-07-31 00:45:59 +08:00
|
|
|
mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
2021-12-21 11:12:27 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, mirrors, 0)
|
2021-06-15 01:20:43 +08:00
|
|
|
}
|
|
|
|
|
2024-11-18 13:59:04 +08:00
|
|
|
func testCreatePushMirror(t *testing.T, session *TestSession, owner, repo, address, username, password, interval string) {
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(owner), url.PathEscape(repo)), map[string]string{
|
|
|
|
"_csrf": GetUserCSRFToken(t, session),
|
|
|
|
"action": "push-mirror-add",
|
|
|
|
"push_mirror_address": address,
|
|
|
|
"push_mirror_username": username,
|
|
|
|
"push_mirror_password": password,
|
|
|
|
"push_mirror_interval": interval,
|
|
|
|
})
|
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
|
|
|
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
|
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
}
|
|
|
|
|
|
|
|
func doRemovePushMirror(t *testing.T, session *TestSession, owner, repo string, pushMirrorID int64) bool {
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(owner), url.PathEscape(repo)), map[string]string{
|
|
|
|
"_csrf": GetUserCSRFToken(t, session),
|
|
|
|
"action": "push-mirror-remove",
|
|
|
|
"push_mirror_id": strconv.FormatInt(pushMirrorID, 10),
|
|
|
|
})
|
|
|
|
resp := session.MakeRequest(t, req, NoExpectedStatus)
|
|
|
|
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
|
|
|
|
return resp.Code == http.StatusSeeOther && flashCookie != nil && strings.Contains(flashCookie.Value, "success")
|
|
|
|
}
|
|
|
|
|
|
|
|
func doUpdatePushMirror(t *testing.T, session *TestSession, owner, repo string, pushMirrorID int64, interval string) bool {
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", owner, repo), map[string]string{
|
|
|
|
"_csrf": GetUserCSRFToken(t, session),
|
|
|
|
"action": "push-mirror-update",
|
|
|
|
"push_mirror_id": strconv.FormatInt(pushMirrorID, 10),
|
|
|
|
"push_mirror_interval": interval,
|
|
|
|
"push_mirror_defer_sync": "true",
|
|
|
|
})
|
|
|
|
resp := session.MakeRequest(t, req, NoExpectedStatus)
|
|
|
|
return resp.Code == http.StatusSeeOther
|
2021-06-15 01:20:43 +08:00
|
|
|
}
|
2021-12-21 11:12:27 +08:00
|
|
|
|
2024-11-18 13:59:04 +08:00
|
|
|
func TestRepoSettingPushMirrorUpdate(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
setting.Migrations.AllowLocalNetworks = true
|
|
|
|
assert.NoError(t, migrations.Init())
|
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
|
|
|
testCreatePushMirror(t, session, "user2", "repo2", "https://127.0.0.1/user1/repo1.git", "", "", "24h")
|
|
|
|
|
|
|
|
pushMirrors, cnt, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, repo2.ID, db.ListOptions{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1, cnt)
|
|
|
|
assert.EqualValues(t, 24*time.Hour, pushMirrors[0].Interval)
|
|
|
|
repo2PushMirrorID := pushMirrors[0].ID
|
|
|
|
|
|
|
|
// update repo2 push mirror
|
|
|
|
assert.True(t, doUpdatePushMirror(t, session, "user2", "repo2", repo2PushMirrorID, "10m0s"))
|
|
|
|
pushMirror := unittest.AssertExistsAndLoadBean(t, &repo_model.PushMirror{ID: repo2PushMirrorID})
|
|
|
|
assert.EqualValues(t, 10*time.Minute, pushMirror.Interval)
|
|
|
|
|
|
|
|
// avoid updating repo2 push mirror from repo1
|
|
|
|
assert.False(t, doUpdatePushMirror(t, session, "user2", "repo1", repo2PushMirrorID, "20m0s"))
|
|
|
|
pushMirror = unittest.AssertExistsAndLoadBean(t, &repo_model.PushMirror{ID: repo2PushMirrorID})
|
|
|
|
assert.EqualValues(t, 10*time.Minute, pushMirror.Interval) // not changed
|
|
|
|
|
|
|
|
// avoid deleting repo2 push mirror from repo1
|
|
|
|
assert.False(t, doRemovePushMirror(t, session, "user2", "repo1", repo2PushMirrorID))
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.PushMirror{ID: repo2PushMirrorID})
|
|
|
|
|
|
|
|
// delete repo2 push mirror
|
|
|
|
assert.True(t, doRemovePushMirror(t, session, "user2", "repo2", repo2PushMirrorID))
|
|
|
|
unittest.AssertNotExistsBean(t, &repo_model.PushMirror{ID: repo2PushMirrorID})
|
2021-12-21 11:12:27 +08:00
|
|
|
}
|