Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more detail to fork failure message #8614

Merged
merged 2 commits into from Feb 12, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/queries_repo.go
Expand Up @@ -548,7 +548,7 @@ func ForkRepo(client *Client, repo ghrepo.Interface, org, newName string, defaul
// The GitHub API will happily return a HTTP 200 when attempting to fork own repo even though no forking
// actually took place. Ensure that we raise an error instead.
if ghrepo.IsSame(repo, newRepo) {
return newRepo, fmt.Errorf("%s cannot be forked", ghrepo.FullName(repo))
return newRepo, fmt.Errorf("%s cannot be forked. A single user account cannot own both a parent and fork.", ghrepo.FullName(repo))
}

return newRepo, nil
Expand Down
29 changes: 29 additions & 0 deletions api/queries_repo_test.go
@@ -1,6 +1,7 @@
package api

import (
"fmt"
"io"
"net/http"
"strings"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGitHubRepo_notFound(t *testing.T) {
Expand Down Expand Up @@ -537,3 +539,30 @@ func TestRepoExists(t *testing.T) {
})
}
}

func TestForkRepoReturnsErrorWhenForkIsNotPossible(t *testing.T) {
// Given our API returns 202 with a Fork that is the same as
// the repo we provided
repoName := "test-repo"
ownerLogin := "test-owner"
stubbedForkResponse := repositoryV3{
Name: repoName,
Owner: struct{ Login string }{
Login: ownerLogin,
},
}

reg := &httpmock.Registry{}
reg.Register(
httpmock.REST("POST", fmt.Sprintf("repos/%s/%s/forks", ownerLogin, repoName)),
httpmock.StatusJSONResponse(202, stubbedForkResponse),
)

client := newTestClient(reg)

// When we fork the repo
_, err := ForkRepo(client, ghrepo.New(ownerLogin, repoName), ownerLogin, "", false)

// Then it provides a useful error message
require.Equal(t, fmt.Errorf("%s/%s cannot be forked. A single user account cannot own both a parent and fork.", ownerLogin, repoName), err)
}