Skip to content

Commit 5eddf8d

Browse files
authored
Merge pull request cli#11536 from cli/copilot/fix-11535
Fix `gh repo delete --yes` safety issue when no repository argument provided
1 parent f0f9987 commit 5eddf8d

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

pkg/cmd/repo/delete/delete.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
4343
Delete a GitHub repository.
4444
4545
With no argument, deletes the current repository. Otherwise, deletes the specified repository.
46+
47+
For safety, when no repository argument is provided, the %[1]s--yes%[1]s flag is ignored
48+
and you will be prompted for confirmation. To delete the current repository non-interactively,
49+
specify it explicitly (e.g., %[1]sgh repo delete owner/repo --yes%[1]s).
4650
4751
Deletion requires authorization with the %[1]sdelete_repo%[1]s scope.
4852
To authorize, run %[1]sgh auth refresh -s delete_repo%[1]s
@@ -53,6 +57,14 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
5357
opts.RepoArg = args[0]
5458
}
5559

60+
// Ignore --yes when no argument provided to prevent accidental deletion
61+
if len(args) == 0 && opts.Confirmed {
62+
if !opts.IO.CanPrompt() {
63+
return cmdutil.FlagErrorf("cannot non-interactively delete current repository. Please specify a repository or run interactively")
64+
}
65+
opts.Confirmed = false
66+
}
67+
5668
if !opts.IO.CanPrompt() && !opts.Confirmed {
5769
return cmdutil.FlagErrorf("--yes required when not running interactively")
5870
}

pkg/cmd/repo/delete/delete_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ func TestNewCmdDelete(t *testing.T) {
4848
tty: true,
4949
output: DeleteOptions{},
5050
},
51+
{
52+
name: "yes flag ignored when no argument tty",
53+
tty: true,
54+
input: "--yes",
55+
output: DeleteOptions{Confirmed: false}, // --yes should be ignored
56+
},
57+
{
58+
name: "yes flag error when no argument notty",
59+
input: "--yes",
60+
wantErr: true,
61+
errMsg: "cannot non-interactively delete current repository. Please specify a repository or run interactively",
62+
},
63+
{
64+
name: "confirm flag error when no argument notty",
65+
input: "--confirm",
66+
wantErr: true,
67+
errMsg: "cannot non-interactively delete current repository. Please specify a repository or run interactively",
68+
},
69+
{
70+
name: "confirm flag also ignored when no argument tty",
71+
tty: true,
72+
input: "--confirm",
73+
output: DeleteOptions{Confirmed: false}, // --confirm should also be ignored
74+
},
5175
}
5276
for _, tt := range tests {
5377
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)