Fix cli command restore-repo: "units" should be parsed as StringSlice (#19953)

* Fix cli command restore-repo: "units" should be parsed as StringSlice because after  #15790 it's read by c.StringSlice("units").  Before, the "units" were processed by strings.Split
* Add checking for invalid unit names

Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
wxiaoguang 2022-06-15 20:28:03 +08:00 committed by GitHub
parent 97548d2722
commit 9f87b60b46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 5 deletions

View file

@ -37,10 +37,10 @@ var CmdRestoreRepository = cli.Command{
Value: "", Value: "",
Usage: "Restore destination repository name", Usage: "Restore destination repository name",
}, },
cli.StringFlag{ cli.StringSliceFlag{
Name: "units", Name: "units",
Value: "", Value: nil,
Usage: `Which items will be restored, one or more units should be separated as comma. Usage: `Which items will be restored, one or more units should be repeated with this flag.
wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units.`, wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units.`,
}, },
cli.BoolFlag{ cli.BoolFlag{

View file

@ -6,6 +6,7 @@ package migrations
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -572,7 +573,7 @@ func DumpRepository(ctx context.Context, baseDir, ownerName string, opts base.Mi
return nil return nil
} }
func updateOptionsUnits(opts *base.MigrateOptions, units []string) { func updateOptionsUnits(opts *base.MigrateOptions, units []string) error {
if len(units) == 0 { if len(units) == 0 {
opts.Wiki = true opts.Wiki = true
opts.Issues = true opts.Issues = true
@ -585,6 +586,8 @@ func updateOptionsUnits(opts *base.MigrateOptions, units []string) {
} else { } else {
for _, unit := range units { for _, unit := range units {
switch strings.ToLower(unit) { switch strings.ToLower(unit) {
case "":
continue
case "wiki": case "wiki":
opts.Wiki = true opts.Wiki = true
case "issues": case "issues":
@ -601,9 +604,12 @@ func updateOptionsUnits(opts *base.MigrateOptions, units []string) {
opts.Comments = true opts.Comments = true
case "pull_requests": case "pull_requests":
opts.PullRequests = true opts.PullRequests = true
default:
return errors.New("invalid unit: " + unit)
} }
} }
} }
return nil
} }
// RestoreRepository restore a repository from the disk directory // RestoreRepository restore a repository from the disk directory
@ -626,7 +632,9 @@ func RestoreRepository(ctx context.Context, baseDir, ownerName, repoName string,
migrateOpts := base.MigrateOptions{ migrateOpts := base.MigrateOptions{
GitServiceType: structs.GitServiceType(tp), GitServiceType: structs.GitServiceType(tp),
} }
updateOptionsUnits(&migrateOpts, units) if err := updateOptionsUnits(&migrateOpts, units); err != nil {
return err
}
if err = migrateRepository(downloader, uploader, migrateOpts, nil); err != nil { if err = migrateRepository(downloader, uploader, migrateOpts, nil); err != nil {
if err1 := uploader.Rollback(); err1 != nil { if err1 := uploader.Rollback(); err1 != nil {