Skip to content

Commit 762d956

Browse files
author
Nate Smith
authored
Merge pull request cli#4655 from cli/issue-4589
add base repo resolution to `gh repo delete`
2 parents c5c83bf + a1426ac commit 762d956

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

pkg/cmd/repo/delete/delete.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
type DeleteOptions struct {
2020
HttpClient func() (*http.Client, error)
21+
BaseRepo func() (ghrepo.Interface, error)
2122
IO *iostreams.IOStreams
2223
RepoArg string
2324
Confirmed bool
@@ -27,21 +28,27 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
2728
opts := &DeleteOptions{
2829
IO: f.IOStreams,
2930
HttpClient: f.HttpClient,
31+
BaseRepo: f.BaseRepo,
3032
}
3133

3234
cmd := &cobra.Command{
33-
Use: "delete <repository>",
35+
Use: "delete [<repository>]",
3436
Short: "Delete a repository",
3537
Long: `Delete a GitHub repository.
3638
39+
With no argument, deletes the current repository. Otherwise, deletes the specified repository.
40+
3741
Deletion requires authorization with the "delete_repo" scope.
3842
To authorize, run "gh auth refresh -s delete_repo"`,
39-
Args: cmdutil.ExactArgs(1, "cannot delete: repository argument required"),
43+
Args: cobra.MaximumNArgs(1),
4044
RunE: func(cmd *cobra.Command, args []string) error {
41-
opts.RepoArg = args[0]
45+
if len(args) > 0 {
46+
opts.RepoArg = args[0]
47+
}
4248
if !opts.IO.CanPrompt() && !opts.Confirmed {
43-
return cmdutil.FlagErrorf("could not prompt: confirmation with prompt or --confirm flag required")
49+
return cmdutil.FlagErrorf("--confirm required when not running interactively")
4450
}
51+
4552
if runF != nil {
4653
return runF(opts)
4754
}
@@ -60,21 +67,27 @@ func deleteRun(opts *DeleteOptions) error {
6067
}
6168
apiClient := api.NewClientFromHTTP(httpClient)
6269

63-
repoSelector := opts.RepoArg
6470
var toDelete ghrepo.Interface
6571

66-
if !strings.Contains(repoSelector, "/") {
67-
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
72+
if opts.RepoArg == "" {
73+
toDelete, err = opts.BaseRepo()
6874
if err != nil {
6975
return err
7076
}
71-
repoSelector = currentUser + "/" + repoSelector
72-
}
73-
toDelete, err = ghrepo.FromFullName(repoSelector)
74-
if err != nil {
75-
return fmt.Errorf("argument error: %w", err)
77+
} else {
78+
repoSelector := opts.RepoArg
79+
if !strings.Contains(repoSelector, "/") {
80+
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
81+
if err != nil {
82+
return err
83+
}
84+
repoSelector = currentUser + "/" + repoSelector
85+
}
86+
toDelete, err = ghrepo.FromFullName(repoSelector)
87+
if err != nil {
88+
return fmt.Errorf("argument error: %w", err)
89+
}
7690
}
77-
7891
fullName := ghrepo.FullName(toDelete)
7992

8093
if !opts.Confirmed {

pkg/cmd/repo/delete/delete_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"testing"
77

8+
"github.com/cli/cli/v2/internal/ghrepo"
89
"github.com/cli/cli/v2/pkg/cmdutil"
910
"github.com/cli/cli/v2/pkg/httpmock"
1011
"github.com/cli/cli/v2/pkg/iostreams"
@@ -24,21 +25,22 @@ func TestNewCmdDelete(t *testing.T) {
2425
}{
2526
{
2627
name: "confirm flag",
28+
tty: true,
2729
input: "OWNER/REPO --confirm",
2830
output: DeleteOptions{RepoArg: "OWNER/REPO", Confirmed: true},
2931
},
3032
{
31-
name: "no confirmation no tty",
33+
name: "no confirmation notty",
3234
input: "OWNER/REPO",
3335
output: DeleteOptions{RepoArg: "OWNER/REPO"},
3436
wantErr: true,
35-
errMsg: "could not prompt: confirmation with prompt or --confirm flag required"},
37+
errMsg: "--confirm required when not running interactively",
38+
},
3639
{
37-
name: "no argument",
38-
input: "",
39-
wantErr: true,
40-
errMsg: "cannot delete: repository argument required",
41-
tty: true,
40+
name: "base repo resolution",
41+
input: "",
42+
tty: true,
43+
output: DeleteOptions{},
4244
},
4345
}
4446
for _, tt := range tests {
@@ -101,7 +103,21 @@ func Test_deleteRun(t *testing.T) {
101103
},
102104
},
103105
{
104-
name: "confimation no tty",
106+
name: "infer base repo",
107+
tty: true,
108+
opts: &DeleteOptions{},
109+
wantStdout: "✓ Deleted repository OWNER/REPO\n",
110+
askStubs: func(q *prompt.AskStubber) {
111+
q.StubOne("OWNER/REPO")
112+
},
113+
httpStubs: func(reg *httpmock.Registry) {
114+
reg.Register(
115+
httpmock.REST("DELETE", "repos/OWNER/REPO"),
116+
httpmock.StatusStringResponse(204, "{}"))
117+
},
118+
},
119+
{
120+
name: "confirmation no tty",
105121
opts: &DeleteOptions{
106122
RepoArg: "OWNER/REPO",
107123
Confirmed: true,
@@ -137,6 +153,10 @@ func Test_deleteRun(t *testing.T) {
137153
tt.askStubs(q)
138154
}
139155

156+
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
157+
return ghrepo.New("OWNER", "REPO"), nil
158+
}
159+
140160
reg := &httpmock.Registry{}
141161
if tt.httpStubs != nil {
142162
tt.httpStubs(reg)

0 commit comments

Comments
 (0)