Merge pull request #10364 from timrogers/timrogers/fix-10034

Error when `gh repo rename` is used with a new repo name that contains an owner
This commit is contained in:
Tyler McGoffin 2025-02-05 15:32:20 -08:00 committed by GitHub
commit 9255658ccc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 0 deletions

View file

@ -0,0 +1,9 @@
# Create a repository with a file so it has a default branch
exec gh repo create $ORG/$SCRIPT_NAME-$RANDOM_STRING --add-readme --private
# Attempt to rename the repo with a slash in the name
! exec gh repo rename $ORG/new-name --repo=$ORG/$SCRIPT_NAME-$RANDOM_STRING --yes
stderr 'New repository name cannot contain '/' character - to transfer a repository to a new owner, you must follow additional steps on GitHub.com. For more information on transferring repository ownership, see <https://docs.github.com/en/repositories/creating-and-managing-repositories/transferring-a-repository>.'
# Defer repo deletion
defer gh repo delete $ORG/$SCRIPT_NAME-$RANDOM_STRING --yes

View file

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strings"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
@ -124,6 +125,10 @@ func renameRun(opts *RenameOptions) error {
}
}
if strings.Contains(newRepoName, "/") {
return fmt.Errorf("New repository name cannot contain '/' character - to transfer a repository to a new owner, you must follow additional steps on GitHub.com. For more information on transferring repository ownership, see <https://docs.github.com/en/repositories/creating-and-managing-repositories/transferring-a-repository>.")
}
if opts.DoConfirm {
var confirmed bool
if confirmed, err = opts.Prompter.Confirm(fmt.Sprintf(

View file

@ -113,6 +113,8 @@ func TestRenameRun(t *testing.T) {
promptStubs func(*prompter.MockPrompter)
wantOut string
tty bool
wantErr bool
errMsg string
}{
{
name: "none argument",
@ -217,6 +219,16 @@ func TestRenameRun(t *testing.T) {
},
wantOut: "",
},
{
name: "error on name with slash",
tty: true,
opts: RenameOptions{
newRepoSelector: "org/new-name",
},
wantErr: true,
errMsg: "New repository name cannot contain '/' character - to transfer a repository to a new owner, you must follow additional steps on GitHub.com. For more information on transferring repository ownership, see <https://docs.github.com/en/repositories/creating-and-managing-repositories/transferring-a-repository>.",
},
}
for _, tt := range testCases {
@ -268,6 +280,10 @@ func TestRenameRun(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
defer reg.Verify(t)
err := renameRun(&tt.opts)
if tt.wantErr {
assert.EqualError(t, err, tt.errMsg)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wantOut, stdout.String())
})