Skip to content

cmd: restore-repo --units is repeated #19935

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2022 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 (
"testing"

"code.gitea.io/gitea/models/unittest"
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: "..",
})
}
18 changes: 14 additions & 4 deletions cmd/restore_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import (
"github.com/urfave/cli"
)

var defaultUnits = cli.StringSlice{
"wiki",
"issues",
"labels",
"releases",
"release_assets",
"milestones",
"pull_requests",
"comments",
}

Copy link
Contributor

@wxiaoguang wxiaoguang Jun 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this default value is not ideal for the server code.

The default value should be 0-length slice to indicate all units (there may be more in the future).

update: if it guarantees that the units always contains the units to be restored, the support for len(units) == 0 should be deleted to make the logic consistent.

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good idea to reduce the possibility to forget the places when we support more units.

// CmdRestoreRepository represents the available restore a repository sub-command.
var CmdRestoreRepository = cli.Command{
Name: "restore-repo",
Expand All @@ -37,11 +48,10 @@ var CmdRestoreRepository = cli.Command{
Value: "",
Usage: "Restore destination repository name",
},
cli.StringFlag{
cli.StringSliceFlag{
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.`,
Value: &defaultUnits,
Usage: "Which items will be restored, can be repeated",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Usage: "Which items will be restored, can be repeated",
Usage: "Which items (wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments) will be restored, can be repeated. Not providing this flag means all units.",

},
cli.BoolFlag{
Name: "validation",
Expand Down
42 changes: 42 additions & 0 deletions cmd/restore_repo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 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 (
"testing"

"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)

func TestRestoreRepoFlags(t *testing.T) {
app := cli.NewApp()
called := false
CmdRestoreRepository.Action = func(c *cli.Context) {
assert.EqualValues(t, []string{"issues", "labels"}, c.StringSlice("units"))
called = true
}
app.Commands = []cli.Command{
CmdRestoreRepository,
}
err := app.Run([]string{"gitea", "restore-repo", "--units", "issues", "--units", "labels"})
assert.True(t, called, "CmdRestoreRepository.Action")
assert.NoError(t, err)
}

func TestRestoreRepoFlagsDefaults(t *testing.T) {
app := cli.NewApp()
called := false
CmdRestoreRepository.Action = func(c *cli.Context) {
assert.EqualValues(t, ([]string)(defaultUnits), c.StringSlice("units"))
called = true
}
app.Commands = []cli.Command{
CmdRestoreRepository,
}
err := app.Run([]string{"gitea", "restore-repo"})
assert.True(t, called, "CmdRestoreRepository.Action")
assert.NoError(t, err)
}