-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Dump github/gitlab/gitea repository data to a local directory and restore to gitea #12244
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 all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
7798250
Dump github/gitlab repository data to a local directory
lunny 9fa7df4
Fix lint
lunny 13d4b3a
Adjust directory structure
lunny 8c3f3db
Allow migration special units
lunny 9e7915c
Allow migration ignore release assets
lunny ed4e376
Fix lint
lunny c49020b
Add restore repository
lunny c13e745
stage the changes
lunny aa5ffb3
Merge
lunny 92fbde7
Fix lint
lunny 3999010
Update the interface
lunny 6d633ef
Add some restore methods
lunny 7006ba2
Finish restore
lunny d1746bc
Add comments
lunny 08e7770
Fix restore
lunny 3c7b2d2
Add a token flag
lunny 770aac9
Fix bug
lunny d367872
Fix test
lunny bbc289c
Fix test
lunny 4f43c44
Fix bug
lunny 0b7786f
Fix bug
lunny 7cfd9d8
Fix lint
lunny c43966b
Fix restore
lunny a8bdf48
refactor downloader
lunny 20cbc64
fmt
lunny 79f0a4a
Fix bug isEnd detection on getIssues
lunny ad60586
Refactor maxPerPage
lunny d33175d
Remove unused codes
lunny 04bebd6
Remove unused codes
lunny af0be0b
Fix bug
lunny e615c19
Fix restore
lunny ab53820
Fix dump
lunny 96ca8e4
Uploader should not depend downloader
lunny 3075168
use release attachment name but not id
lunny 97cc82c
Fix restore bug
lunny 6c5044d
Fix lint
lunny c09c7ed
Fix restore bug
lunny 8f4e741
Add a method of DownloadFunc for base.Release to make uploader not de…
lunny 2124a61
fix Release yml marshal
lunny 2e90a9f
Fix trace information
lunny 2b0fe43
Fix bug when dump & restore
lunny f91ba44
Save relative path on yml file
lunny eb5da17
Fix bug
lunny 3c5b2e1
Use relative path
lunny 80b5c9a
Update docs
lunny ceefd98
Use git service string but not int
lunny 695d23e
Recognize clone addr to service type
lunny 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,162 @@ | ||
// Copyright 2020 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 cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/convert" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/migrations" | ||
"code.gitea.io/gitea/modules/migrations/base" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/structs" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
// CmdDumpRepository represents the available dump repository sub-command. | ||
var CmdDumpRepository = cli.Command{ | ||
Name: "dump-repo", | ||
Usage: "Dump the repository from git/github/gitea/gitlab", | ||
Description: "This is a command for dumping the repository data.", | ||
Action: runDumpRepository, | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "git_service", | ||
Value: "", | ||
Usage: "Git service, git, github, gitea, gitlab. If clone_addr could be recognized, this could be ignored.", | ||
}, | ||
cli.StringFlag{ | ||
Name: "repo_dir, r", | ||
Value: "./data", | ||
Usage: "Repository dir path to store the data", | ||
}, | ||
cli.StringFlag{ | ||
Name: "clone_addr", | ||
Value: "", | ||
Usage: "The URL will be clone, currently could be a git/github/gitea/gitlab http/https URL", | ||
}, | ||
cli.StringFlag{ | ||
Name: "auth_username", | ||
Value: "", | ||
Usage: "The username to visit the clone_addr", | ||
}, | ||
cli.StringFlag{ | ||
Name: "auth_password", | ||
Value: "", | ||
Usage: "The password to visit the clone_addr", | ||
}, | ||
cli.StringFlag{ | ||
Name: "auth_token", | ||
Value: "", | ||
Usage: "The personal token to visit the clone_addr", | ||
}, | ||
cli.StringFlag{ | ||
Name: "owner_name", | ||
Value: "", | ||
Usage: "The data will be stored on a directory with owner name if not empty", | ||
}, | ||
cli.StringFlag{ | ||
Name: "repo_name", | ||
Value: "", | ||
Usage: "The data will be stored on a directory with repository name if not empty", | ||
}, | ||
cli.StringFlag{ | ||
Name: "units", | ||
Value: "", | ||
Usage: `Which items will be migrated, one or more units should be separated as comma. | ||
wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units.`, | ||
}, | ||
}, | ||
} | ||
|
||
func runDumpRepository(ctx *cli.Context) error { | ||
if err := initDB(); err != nil { | ||
return err | ||
} | ||
|
||
log.Trace("AppPath: %s", setting.AppPath) | ||
log.Trace("AppWorkPath: %s", setting.AppWorkPath) | ||
log.Trace("Custom path: %s", setting.CustomPath) | ||
log.Trace("Log path: %s", setting.LogRootPath) | ||
setting.InitDBConfig() | ||
|
||
var ( | ||
serviceType structs.GitServiceType | ||
cloneAddr = ctx.String("clone_addr") | ||
serviceStr = ctx.String("git_service") | ||
) | ||
|
||
if strings.HasPrefix(strings.ToLower(cloneAddr), "https://github.com/") { | ||
serviceStr = "github" | ||
} else if strings.HasPrefix(strings.ToLower(cloneAddr), "https://gitlab.com/") { | ||
serviceStr = "gitlab" | ||
} else if strings.HasPrefix(strings.ToLower(cloneAddr), "https://gitea.com/") { | ||
serviceStr = "gitea" | ||
} | ||
if serviceStr == "" { | ||
return errors.New("git_service missed or clone_addr cannot be recognized") | ||
} | ||
serviceType = convert.ToGitServiceType(serviceStr) | ||
|
||
var opts = base.MigrateOptions{ | ||
GitServiceType: serviceType, | ||
CloneAddr: cloneAddr, | ||
AuthUsername: ctx.String("auth_username"), | ||
AuthPassword: ctx.String("auth_password"), | ||
AuthToken: ctx.String("auth_token"), | ||
RepoName: ctx.String("repo_name"), | ||
} | ||
|
||
if len(ctx.String("units")) == 0 { | ||
opts.Wiki = true | ||
opts.Issues = true | ||
opts.Milestones = true | ||
opts.Labels = true | ||
opts.Releases = true | ||
opts.Comments = true | ||
opts.PullRequests = true | ||
opts.ReleaseAssets = true | ||
} else { | ||
units := strings.Split(ctx.String("units"), ",") | ||
for _, unit := range units { | ||
switch strings.ToLower(unit) { | ||
case "wiki": | ||
opts.Wiki = true | ||
case "issues": | ||
opts.Issues = true | ||
case "milestones": | ||
opts.Milestones = true | ||
case "labels": | ||
opts.Labels = true | ||
case "releases": | ||
opts.Releases = true | ||
case "release_assets": | ||
opts.ReleaseAssets = true | ||
case "comments": | ||
opts.Comments = true | ||
case "pull_requests": | ||
opts.PullRequests = true | ||
} | ||
} | ||
} | ||
|
||
if err := migrations.DumpRepository( | ||
context.Background(), | ||
ctx.String("repo_dir"), | ||
ctx.String("owner_name"), | ||
opts, | ||
); err != nil { | ||
log.Fatal("Failed to dump repository: %v", err) | ||
return err | ||
} | ||
|
||
log.Trace("Dump finished!!!") | ||
|
||
return nil | ||
} |
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,119 @@ | ||
// Copyright 2020 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 cmd | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/migrations" | ||
"code.gitea.io/gitea/modules/migrations/base" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/storage" | ||
pull_service "code.gitea.io/gitea/services/pull" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
// CmdRestoreRepository represents the available restore a repository sub-command. | ||
var CmdRestoreRepository = cli.Command{ | ||
Name: "restore-repo", | ||
Usage: "Restore the repository from disk", | ||
Description: "This is a command for restoring the repository data.", | ||
Action: runRestoreRepository, | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "repo_dir, r", | ||
Value: "./data", | ||
Usage: "Repository dir path to restore from", | ||
}, | ||
cli.StringFlag{ | ||
Name: "owner_name", | ||
Value: "", | ||
Usage: "Restore destination owner name", | ||
}, | ||
cli.StringFlag{ | ||
Name: "repo_name", | ||
Value: "", | ||
Usage: "Restore destination repository name", | ||
}, | ||
cli.StringFlag{ | ||
Name: "units", | ||
Value: "", | ||
Usage: `Which items will be restored, one or more units should be separated as comma. | ||
wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units.`, | ||
}, | ||
}, | ||
} | ||
|
||
func runRestoreRepository(ctx *cli.Context) error { | ||
if err := initDB(); err != nil { | ||
return err | ||
} | ||
|
||
log.Trace("AppPath: %s", setting.AppPath) | ||
log.Trace("AppWorkPath: %s", setting.AppWorkPath) | ||
log.Trace("Custom path: %s", setting.CustomPath) | ||
log.Trace("Log path: %s", setting.LogRootPath) | ||
setting.InitDBConfig() | ||
|
||
if err := storage.Init(); err != nil { | ||
return err | ||
} | ||
|
||
if err := pull_service.Init(); err != nil { | ||
return err | ||
} | ||
|
||
var opts = base.MigrateOptions{ | ||
RepoName: ctx.String("repo_name"), | ||
} | ||
|
||
if len(ctx.String("units")) == 0 { | ||
opts.Wiki = true | ||
opts.Issues = true | ||
opts.Milestones = true | ||
opts.Labels = true | ||
opts.Releases = true | ||
opts.Comments = true | ||
opts.PullRequests = true | ||
opts.ReleaseAssets = true | ||
} else { | ||
units := strings.Split(ctx.String("units"), ",") | ||
for _, unit := range units { | ||
switch strings.ToLower(unit) { | ||
case "wiki": | ||
opts.Wiki = true | ||
case "issues": | ||
opts.Issues = true | ||
case "milestones": | ||
opts.Milestones = true | ||
case "labels": | ||
opts.Labels = true | ||
case "releases": | ||
opts.Releases = true | ||
case "release_assets": | ||
opts.ReleaseAssets = true | ||
case "comments": | ||
opts.Comments = true | ||
case "pull_requests": | ||
opts.PullRequests = true | ||
} | ||
} | ||
} | ||
|
||
if err := migrations.RestoreRepository( | ||
context.Background(), | ||
ctx.String("repo_dir"), | ||
ctx.String("owner_name"), | ||
ctx.String("repo_name"), | ||
); err != nil { | ||
log.Fatal("Failed to restore repository: %v", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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
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.