Skip to content
Merged
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
12 changes: 12 additions & 0 deletions pkg/cmd/repo/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
Delete a GitHub repository.

With no argument, deletes the current repository. Otherwise, deletes the specified repository.

For safety, when no repository argument is provided, the %[1]s--yes%[1]s flag is ignored
and you will be prompted for confirmation. To delete the current repository non-interactively,
specify it explicitly (e.g., %[1]sgh repo delete owner/repo --yes%[1]s).

Deletion requires authorization with the %[1]sdelete_repo%[1]s scope.
To authorize, run %[1]sgh auth refresh -s delete_repo%[1]s
Expand All @@ -53,6 +57,14 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
opts.RepoArg = args[0]
}

// Ignore --yes when no argument provided to prevent accidental deletion
if len(args) == 0 && opts.Confirmed {
if !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("cannot non-interactively delete current repository. Please specify a repository or run interactively")
}
opts.Confirmed = false
}

if !opts.IO.CanPrompt() && !opts.Confirmed {
return cmdutil.FlagErrorf("--yes required when not running interactively")
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/cmd/repo/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ func TestNewCmdDelete(t *testing.T) {
tty: true,
output: DeleteOptions{},
},
{
name: "yes flag ignored when no argument tty",
tty: true,
input: "--yes",
output: DeleteOptions{Confirmed: false}, // --yes should be ignored
},
{
name: "yes flag error when no argument notty",
Comment thread
BagToad marked this conversation as resolved.
input: "--yes",
wantErr: true,
errMsg: "cannot non-interactively delete current repository. Please specify a repository or run interactively",
},
{
name: "confirm flag error when no argument notty",
input: "--confirm",
wantErr: true,
errMsg: "cannot non-interactively delete current repository. Please specify a repository or run interactively",
},
{
name: "confirm flag also ignored when no argument tty",
tty: true,
input: "--confirm",
output: DeleteOptions{Confirmed: false}, // --confirm should also be ignored
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading