Merge pull request #8614 from chrisroat/patch-1

Add more detail to fork failure message
This commit is contained in:
William Martin 2024-02-12 11:33:23 +01:00 committed by GitHub
commit ae2a361d76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 1 deletions

View file

@ -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 // 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. // actually took place. Ensure that we raise an error instead.
if ghrepo.IsSame(repo, newRepo) { 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 return newRepo, nil

View file

@ -1,6 +1,7 @@
package api package api
import ( import (
"fmt"
"io" "io"
"net/http" "net/http"
"strings" "strings"
@ -9,6 +10,7 @@ import (
"github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/httpmock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestGitHubRepo_notFound(t *testing.T) { func TestGitHubRepo_notFound(t *testing.T) {
@ -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)
}