Merge pull request #4654 from cli/issue-4590
add base repo resolution to archive
This commit is contained in:
commit
c5c83bfb9d
2 changed files with 98 additions and 72 deletions
|
|
@ -5,10 +5,13 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/internal/config"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/prompt"
|
||||
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -16,8 +19,10 @@ import (
|
|||
|
||||
type ArchiveOptions struct {
|
||||
HttpClient func() (*http.Client, error)
|
||||
IO *iostreams.IOStreams
|
||||
Config func() (config.Config, error)
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
Confirmed bool
|
||||
IO *iostreams.IOStreams
|
||||
RepoArg string
|
||||
}
|
||||
|
||||
|
|
@ -26,17 +31,24 @@ func NewCmdArchive(f *cmdutil.Factory, runF func(*ArchiveOptions) error) *cobra.
|
|||
IO: f.IOStreams,
|
||||
HttpClient: f.HttpClient,
|
||||
Config: f.Config,
|
||||
BaseRepo: f.BaseRepo,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
DisableFlagsInUseLine: true,
|
||||
|
||||
Use: "archive <repository>",
|
||||
Use: "archive [<repository>]",
|
||||
Short: "Archive a repository",
|
||||
Long: "Archive a GitHub repository.",
|
||||
Args: cmdutil.ExactArgs(1, "cannot archive: repository argument required"),
|
||||
Long: heredoc.Doc(`Archive a GitHub repository.
|
||||
|
||||
With no argument, archives the current repository.`),
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.RepoArg = args[0]
|
||||
if len(args) > 0 {
|
||||
opts.RepoArg = args[0]
|
||||
}
|
||||
|
||||
if !opts.Confirmed && !opts.IO.CanPrompt() {
|
||||
return cmdutil.FlagErrorf("--confirm required when not running interactively")
|
||||
}
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
}
|
||||
|
|
@ -44,6 +56,7 @@ func NewCmdArchive(f *cmdutil.Factory, runF func(*ArchiveOptions) error) *cobra.
|
|||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVarP(&opts.Confirmed, "confirm", "y", false, "Skip the confirmation prompt")
|
||||
return cmd
|
||||
}
|
||||
|
||||
|
|
@ -57,26 +70,35 @@ func archiveRun(opts *ArchiveOptions) error {
|
|||
|
||||
var toArchive ghrepo.Interface
|
||||
|
||||
archiveURL := opts.RepoArg
|
||||
if !strings.Contains(archiveURL, "/") {
|
||||
cfg, err := opts.Config()
|
||||
if opts.RepoArg == "" {
|
||||
toArchive, err = opts.BaseRepo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hostname, err := cfg.DefaultHost()
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
repoSelector := opts.RepoArg
|
||||
if !strings.Contains(repoSelector, "/") {
|
||||
cfg, err := opts.Config()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hostname, err := cfg.DefaultHost()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
currentUser, err := api.CurrentLoginName(apiClient, hostname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repoSelector = currentUser + "/" + repoSelector
|
||||
}
|
||||
|
||||
currentUser, err := api.CurrentLoginName(apiClient, hostname)
|
||||
toArchive, err = ghrepo.FromFullName(repoSelector)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
archiveURL = currentUser + "/" + archiveURL
|
||||
}
|
||||
toArchive, err = ghrepo.FromFullName(archiveURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("argument error: %w", err)
|
||||
}
|
||||
|
||||
fields := []string{"name", "owner", "isArchived", "id"}
|
||||
|
|
@ -87,16 +109,27 @@ func archiveRun(opts *ArchiveOptions) error {
|
|||
|
||||
fullName := ghrepo.FullName(toArchive)
|
||||
if repo.IsArchived {
|
||||
fmt.Fprintf(opts.IO.ErrOut,
|
||||
"%s Repository %s is already archived\n",
|
||||
cs.WarningIcon(),
|
||||
fullName)
|
||||
fmt.Fprintf(opts.IO.ErrOut, "%s Repository %s is already archived\n", cs.WarningIcon(), fullName)
|
||||
return nil
|
||||
}
|
||||
|
||||
if !opts.Confirmed {
|
||||
p := &survey.Confirm{
|
||||
Message: fmt.Sprintf("Archive %s?", fullName),
|
||||
Default: false,
|
||||
}
|
||||
err = prompt.SurveyAskOne(p, &opts.Confirmed)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to prompt: %w", err)
|
||||
}
|
||||
if !opts.Confirmed {
|
||||
return cmdutil.CancelError
|
||||
}
|
||||
}
|
||||
|
||||
err = archiveRepo(httpClient, repo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("API called failed: %w", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if opts.IO.IsStdoutTTY() {
|
||||
|
|
|
|||
|
|
@ -2,49 +2,42 @@ package archive
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/httpmock"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/cli/cli/v2/pkg/prompt"
|
||||
"github.com/google/shlex"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// probably redundant
|
||||
func TestNewCmdArchive(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
tty bool
|
||||
output ArchiveOptions
|
||||
wantErr bool
|
||||
output ArchiveOptions
|
||||
errMsg string
|
||||
}{
|
||||
{
|
||||
name: "valid repo",
|
||||
input: "cli/cli",
|
||||
tty: true,
|
||||
output: ArchiveOptions{
|
||||
RepoArg: "cli/cli",
|
||||
},
|
||||
name: "no arguments no tty",
|
||||
input: "",
|
||||
errMsg: "--confirm required when not running interactively",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "no argument",
|
||||
input: "",
|
||||
wantErr: true,
|
||||
tty: true,
|
||||
output: ArchiveOptions{
|
||||
RepoArg: "",
|
||||
},
|
||||
name: "repo argument tty",
|
||||
input: "OWNER/REPO --confirm",
|
||||
output: ArchiveOptions{RepoArg: "OWNER/REPO", Confirmed: true},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
io, _, _, _ := iostreams.Test()
|
||||
io.SetStdinTTY(tt.tty)
|
||||
io.SetStdoutTTY(tt.tty)
|
||||
f := &cmdutil.Factory{
|
||||
IOStreams: io,
|
||||
}
|
||||
|
|
@ -67,45 +60,51 @@ func TestNewCmdArchive(t *testing.T) {
|
|||
}
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.output.RepoArg, gotOpts.RepoArg)
|
||||
assert.Equal(t, tt.output.Confirmed, gotOpts.Confirmed)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ArchiveRun(t *testing.T) {
|
||||
queryResponse := `{ "data": { "repository": { "id": "THE-ID","isArchived": %s} } }`
|
||||
tests := []struct {
|
||||
name string
|
||||
opts ArchiveOptions
|
||||
httpStubs func(*httpmock.Registry)
|
||||
askStubs func(*prompt.AskStubber)
|
||||
isTTY bool
|
||||
wantStdout string
|
||||
wantStderr string
|
||||
}{
|
||||
{
|
||||
name: "unarchived repo tty",
|
||||
opts: ArchiveOptions{RepoArg: "OWNER/REPO"},
|
||||
wantStdout: "✓ Archived repository OWNER/REPO\n",
|
||||
isTTY: true,
|
||||
askStubs: func(q *prompt.AskStubber) {
|
||||
q.StubOne(true)
|
||||
},
|
||||
isTTY: true,
|
||||
opts: ArchiveOptions{RepoArg: "OWNER/REPO"},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`query RepositoryInfo\b`),
|
||||
httpmock.StringResponse(`{ "data": { "repository": {
|
||||
"id": "THE-ID",
|
||||
"isArchived": false} } }`))
|
||||
httpmock.StringResponse(fmt.Sprintf(queryResponse, "false")))
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`mutation ArchiveRepository\b`),
|
||||
httpmock.StringResponse(`{}`))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "unarchived repo notty",
|
||||
opts: ArchiveOptions{RepoArg: "OWNER/REPO"},
|
||||
isTTY: false,
|
||||
name: "infer base repo",
|
||||
wantStdout: "✓ Archived repository OWNER/REPO\n",
|
||||
opts: ArchiveOptions{},
|
||||
askStubs: func(q *prompt.AskStubber) {
|
||||
q.StubOne(true)
|
||||
},
|
||||
isTTY: true,
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`query RepositoryInfo\b`),
|
||||
httpmock.StringResponse(`{ "data": { "repository": {
|
||||
"id": "THE-ID",
|
||||
"isArchived": false} } }`))
|
||||
httpmock.StringResponse(fmt.Sprintf(queryResponse, "false")))
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`mutation ArchiveRepository\b`),
|
||||
httpmock.StringResponse(`{}`))
|
||||
|
|
@ -113,44 +112,38 @@ func Test_ArchiveRun(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "archived repo tty",
|
||||
opts: ArchiveOptions{RepoArg: "OWNER/REPO"},
|
||||
wantStderr: "! Repository OWNER/REPO is already archived\n",
|
||||
isTTY: true,
|
||||
opts: ArchiveOptions{RepoArg: "OWNER/REPO"},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`query RepositoryInfo\b`),
|
||||
httpmock.StringResponse(`{ "data": { "repository": {
|
||||
"id": "THE-ID",
|
||||
"isArchived": true } } }`))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "archived repo notty",
|
||||
opts: ArchiveOptions{RepoArg: "OWNER/REPO"},
|
||||
isTTY: false,
|
||||
wantStderr: "! Repository OWNER/REPO is already archived\n",
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`query RepositoryInfo\b`),
|
||||
httpmock.StringResponse(`{ "data": { "repository": {
|
||||
"id": "THE-ID",
|
||||
"isArchived": true } } }`))
|
||||
httpmock.StringResponse(fmt.Sprintf(queryResponse, "true")))
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
repo, _ := ghrepo.FromFullName("OWNER/REPO")
|
||||
reg := &httpmock.Registry{}
|
||||
if tt.httpStubs != nil {
|
||||
tt.httpStubs(reg)
|
||||
}
|
||||
|
||||
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
|
||||
return repo, nil
|
||||
}
|
||||
tt.opts.HttpClient = func() (*http.Client, error) {
|
||||
return &http.Client{Transport: reg}, nil
|
||||
}
|
||||
|
||||
io, _, stdout, stderr := iostreams.Test()
|
||||
tt.opts.IO = io
|
||||
|
||||
q, teardown := prompt.InitAskStubber()
|
||||
defer teardown()
|
||||
if tt.askStubs != nil {
|
||||
tt.askStubs(q)
|
||||
}
|
||||
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
defer reg.Verify(t)
|
||||
io.SetStdoutTTY(tt.isTTY)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue