-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add push to remote mirror repository #15157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 64 commits
Commits
Show all changes
73 commits
Select commit
Hold shift + click to select a range
4e0e107
Added push mirror model.
KN4CK3R 5797f96
Integrated push mirror into queue.
KN4CK3R 00bf264
Moved methods into own file.
KN4CK3R 000cf56
Added basic implementation.
KN4CK3R 5e701eb
Mirror wiki too.
KN4CK3R ecbbc67
Removed duplicated method.
KN4CK3R f6f4dba
Get url for different remotes.
KN4CK3R 2365760
Added migration.
KN4CK3R aff13d6
Unified remote url access.
KN4CK3R ff4b4ab
Add/Remove push mirror remotes.
KN4CK3R 382be07
Prevent hangs with missing credentials.
KN4CK3R bf22cd1
Moved code between files.
KN4CK3R 5604087
Lint
KN4CK3R 84a9bd8
Changed sanitizer interface.
KN4CK3R ca14354
Added push mirror backend methods.
KN4CK3R a9b1961
Fix
KN4CK3R ca2c530
Only update the mirror remote.
KN4CK3R 84d1352
Limit refs on push.
KN4CK3R 806408a
Added UI part.
KN4CK3R 4eed55d
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
KN4CK3R 79201d7
Added missing table.
KN4CK3R b78b83e
Delete mirror if repository gets removed.
KN4CK3R 96dab54
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
KN4CK3R 07ad389
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
KN4CK3R bafeba1
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
KN4CK3R c0d3c78
Changed signature. Handle object errors.
KN4CK3R 20de69c
Added upload method.
KN4CK3R 92e2573
Added "upload" unit tests.
KN4CK3R 0d9c384
Added transfer adapter unit tests.
KN4CK3R 5746665
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
KN4CK3R 3bc2300
Send correct headers.
KN4CK3R c4ff5d2
Added pushing of LFS objects.
KN4CK3R 0012f34
Go 1.14
KN4CK3R bacae71
Added more logging.
KN4CK3R 0b3e91a
Simpler body handling.
KN4CK3R f2cbd62
Process files in batches to reduce HTTP calls.
KN4CK3R 3688138
Merge branch 'master' into feature-push-mirror
6543 656c347
Fixed unit tests.
KN4CK3R a6088cb
Added created timestamp.
KN4CK3R 1923b51
Fixed name.
KN4CK3R 1eb9c74
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
KN4CK3R 5739008
Fixed invalid column name.
KN4CK3R 980548a
Changed name to prevent xorm auto setting.
KN4CK3R 310fdb9
Remove table header im empty.
KN4CK3R 33d0244
Strip exit code from error message.
KN4CK3R ffaf55c
Added docs page about mirroring.
KN4CK3R 180a61c
Fixed date.
KN4CK3R 835f228
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R b83497b
Use new method.
KN4CK3R 2a01584
Use jsoniter.
KN4CK3R d92a8ba
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R 882e958
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R 902a730
Fixed merge errors.
KN4CK3R 68e0c00
Use syncPullMirror in test.
KN4CK3R ac879f8
Fixed merge error.
KN4CK3R 65f47da
Moved test to integrations.
KN4CK3R 2bc6102
Added push mirror test.
KN4CK3R d7a3719
Removed NextUpdateUnix.
KN4CK3R dad9adf
and here
KN4CK3R c876f05
Merge branch 'main' into feature-push-mirror
6543 687302a
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R c950877
Merge branch 'main' into feature-push-mirror
6543 c76cdc6
Merge branch 'main' into feature-push-mirror
6543 ec872c0
Merge branch 'main' into feature-push-mirror
6543 4e39abb
Apply suggestions from code review
6543 e482136
Merge branch 'main' into feature-push-mirror
6543 19d7911
Fixed sql error.
KN4CK3R 444b96c
Added test.
KN4CK3R 27b30d6
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R 567dccc
untouch models/migrations/v182.go
6543 c305bca
Merge branch 'main' into feature-push-mirror
6543 9a66e72
Merge branch 'master' into feature-push-mirror
6543 2c05692
`xorm:"text"`
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
--- | ||
date: "2021-05-13T00:00:00-00:00" | ||
title: "Repository Mirror" | ||
slug: "repo-mirror" | ||
weight: 45 | ||
toc: false | ||
draft: false | ||
menu: | ||
sidebar: | ||
parent: "advanced" | ||
name: "Repository Mirror" | ||
weight: 45 | ||
identifier: "repo-mirror" | ||
--- | ||
|
||
# Repository Mirror | ||
|
||
Repository mirroring allows for the mirroring of repositories to and from external sources. You can use it to mirror branches, tags, and commits between repositories. | ||
|
||
**Table of Contents** | ||
|
||
{{< toc >}} | ||
|
||
## Use cases | ||
|
||
The following are some possible use cases for repository mirroring: | ||
|
||
- You migrated to Gitea but still need to keep your project in another source. In that case, you can simply set it up to mirror to Gitea (pull) and all the essential history of commits, tags, and branches are available in your Gitea instance. | ||
- You have old projects in another source that you don’t use actively anymore, but don’t want to remove for archiving purposes. In that case, you can create a push mirror so that your active Gitea repository can push its changes to the old location. | ||
|
||
## Pulling from a remote repository | ||
|
||
For an existing remote repository, you can set up pull mirroring as follows: | ||
|
||
1. Select **New Migration** in the **Create...** menu on the top right. | ||
2. Select the remote repository service. | ||
3. Enter a repository URL. | ||
4. If the repository needs authentication fill in your authentication information. | ||
5. Check the box **This repository will be a mirror**. | ||
5. Select **Migrate repository** to save the configuration. | ||
|
||
The repository now gets mirrored periodically from the remote repository. You can force a sync by selecting **Synchronize Now** in the repository settings. | ||
|
||
## Pushing to a remote repository | ||
|
||
For an existing repository, you can set up push mirroring as follows: | ||
|
||
1. In your repository, go to **Settings** > **Repository**, and then the **Mirror Settings** section. | ||
2. Enter a repository URL. | ||
3. If the repository needs authentication expand the **Authorization** section and fill in your authentication information. | ||
4. Select **Add Push Mirror** to save the configuration. | ||
|
||
The repository now gets mirrored periodically to the remote repository. You can force a sync by selecting **Synchronize Now**. In case of an error a message displayed to help you resolve it. | ||
|
||
:exclamation::exclamation: **NOTE:** This will force push to the remote repository. This will overwrite any changes in the remote repository! :exclamation::exclamation: | ||
|
||
### Setting up a push mirror from Gitea to GitHub | ||
|
||
To set up a mirror from Gitea to GitHub, you need to follow these steps: | ||
|
||
1. Create a [GitHub personal access token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) with the *public_repo* box checked. | ||
2. Fill in the **Git Remote Repository URL**: `https://github.com/<your_github_group>/<your_github_project>.git`. | ||
3. Fill in the **Authorization** fields with your GitHub username and the personal access token. | ||
4. Select **Add Push Mirror** to save the configuration. | ||
|
||
The repository pushes shortly thereafter. To force a push, select the **Synchronize Now** button. | ||
|
||
### Setting up a push mirror from Gitea to GitLab | ||
|
||
To set up a mirror from Gitea to GitLab, you need to follow these steps: | ||
|
||
1. Create a [GitLab personal access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) with *write_repository* scope. | ||
2. Fill in the **Git Remote Repository URL**: `https://<destination host>/<your_gitlab_group_or_name>/<your_gitlab_project>.git`. | ||
3. Fill in the **Authorization** fields with `oauth2` as **Username** and your GitLab personal access token as **Password**. | ||
4. Select **Add Push Mirror** to save the configuration. | ||
|
||
The repository pushes shortly thereafter. To force a push, select the **Synchronize Now** button. | ||
|
||
### Setting up a push mirror from Gitea to Bitbucket | ||
|
||
To set up a mirror from Gitea to Bitbucket, you need to follow these steps: | ||
|
||
1. Create a [Bitbucket app password](https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/) with the *Repository Write* box checked. | ||
2. Fill in the **Git Remote Repository URL**: `https://bitbucket.org/<your_bitbucket_group_or_name>/<your_bitbucket_project>.git`. | ||
3. Fill in the **Authorization** fields with your Bitbucket username and the app password as **Password**. | ||
4. Select **Add Push Mirror** to save the configuration. | ||
|
||
The repository pushes shortly thereafter. To force a push, select the **Synchronize Now** button. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2021 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 ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/git" | ||
"code.gitea.io/gitea/modules/repository" | ||
"code.gitea.io/gitea/modules/setting" | ||
mirror_service "code.gitea.io/gitea/services/mirror" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMirrorPush(t *testing.T) { | ||
onGiteaRun(t, testMirrorPush) | ||
} | ||
|
||
func testMirrorPush(t *testing.T, u *url.URL) { | ||
defer prepareTestEnv(t)() | ||
|
||
setting.Migrations.AllowLocalNetworks = true | ||
|
||
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) | ||
srcRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) | ||
|
||
mirrorRepo, err := repository.CreateRepository(user, user, models.CreateRepoOptions{ | ||
Name: "test-push-mirror", | ||
}) | ||
assert.NoError(t, err) | ||
|
||
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) | ||
|
||
mirrors, err := models.GetPushMirrorsByRepoID(srcRepo.ID) | ||
assert.NoError(t, err) | ||
assert.Len(t, mirrors, 1) | ||
|
||
ok := mirror_service.SyncPushMirror(context.Background(), mirrors[0].ID) | ||
assert.True(t, ok) | ||
|
||
srcGitRepo, err := git.OpenRepository(srcRepo.RepoPath()) | ||
assert.NoError(t, err) | ||
defer srcGitRepo.Close() | ||
|
||
srcCommit, err := srcGitRepo.GetBranchCommit("master") | ||
assert.NoError(t, err) | ||
|
||
mirrorGitRepo, err := git.OpenRepository(mirrorRepo.RepoPath()) | ||
assert.NoError(t, err) | ||
defer mirrorGitRepo.Close() | ||
|
||
mirrorCommit, err := mirrorGitRepo.GetBranchCommit("master") | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, srcCommit.ID, mirrorCommit.ID) | ||
} | ||
|
||
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", | ||
}) | ||
ctx.Session.MakeRequest(t, req, http.StatusFound) | ||
|
||
flashCookie := ctx.Session.GetCookie("macaron_flash") | ||
assert.NotNil(t, flashCookie) | ||
assert.Contains(t, flashCookie.Value, "success") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2021 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 migrations | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/timeutil" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func createPushMirrorTable(x *xorm.Engine) error { | ||
type PushMirror struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"INDEX"` | ||
RemoteName string | ||
|
||
Interval time.Duration | ||
CreatedUnix timeutil.TimeStamp `xorm:"created"` | ||
LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"` | ||
LastError string | ||
} | ||
|
||
sess := x.NewSession() | ||
defer sess.Close() | ||
if err := sess.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
if err := sess.Sync2(new(PushMirror)); err != nil { | ||
return fmt.Errorf("Sync2: %v", err) | ||
} | ||
|
||
return sess.Commit() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.