Skip to content

Commit 9f87b60

Browse files
wxiaoguang6543
andauthored
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 <[email protected]>
1 parent 97548d2 commit 9f87b60

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

cmd/restore_repo.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ var CmdRestoreRepository = cli.Command{
3737
Value: "",
3838
Usage: "Restore destination repository name",
3939
},
40-
cli.StringFlag{
40+
cli.StringSliceFlag{
4141
Name: "units",
42-
Value: "",
43-
Usage: `Which items will be restored, one or more units should be separated as comma.
42+
Value: nil,
43+
Usage: `Which items will be restored, one or more units should be repeated with this flag.
4444
wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units.`,
4545
},
4646
cli.BoolFlag{

services/migrations/dump.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package migrations
66

77
import (
88
"context"
9+
"errors"
910
"fmt"
1011
"io"
1112
"net/http"
@@ -572,7 +573,7 @@ func DumpRepository(ctx context.Context, baseDir, ownerName string, opts base.Mi
572573
return nil
573574
}
574575

575-
func updateOptionsUnits(opts *base.MigrateOptions, units []string) {
576+
func updateOptionsUnits(opts *base.MigrateOptions, units []string) error {
576577
if len(units) == 0 {
577578
opts.Wiki = true
578579
opts.Issues = true
@@ -585,6 +586,8 @@ func updateOptionsUnits(opts *base.MigrateOptions, units []string) {
585586
} else {
586587
for _, unit := range units {
587588
switch strings.ToLower(unit) {
589+
case "":
590+
continue
588591
case "wiki":
589592
opts.Wiki = true
590593
case "issues":
@@ -601,9 +604,12 @@ func updateOptionsUnits(opts *base.MigrateOptions, units []string) {
601604
opts.Comments = true
602605
case "pull_requests":
603606
opts.PullRequests = true
607+
default:
608+
return errors.New("invalid unit: " + unit)
604609
}
605610
}
606611
}
612+
return nil
607613
}
608614

609615
// RestoreRepository restore a repository from the disk directory
@@ -626,7 +632,9 @@ func RestoreRepository(ctx context.Context, baseDir, ownerName, repoName string,
626632
migrateOpts := base.MigrateOptions{
627633
GitServiceType: structs.GitServiceType(tp),
628634
}
629-
updateOptionsUnits(&migrateOpts, units)
635+
if err := updateOptionsUnits(&migrateOpts, units); err != nil {
636+
return err
637+
}
630638

631639
if err = migrateRepository(downloader, uploader, migrateOpts, nil); err != nil {
632640
if err1 := uploader.Rollback(); err1 != nil {

0 commit comments

Comments
 (0)