Fix repo clone wiki

This commit is contained in:
gunadhya 2020-11-27 16:20:19 +05:30 committed by Mislav Marohnić
parent 34d549e7b6
commit fd57835bb9
3 changed files with 57 additions and 1 deletions

View file

@ -27,6 +27,7 @@ type Repository struct {
IsPrivate bool
HasIssuesEnabled bool
HasWikiEnabled bool
ViewerPermission string
DefaultBranchRef BranchRef
@ -94,6 +95,7 @@ func GitHubRepo(client *Client, repo ghrepo.Interface) (*Repository, error) {
owner { login }
hasIssuesEnabled
description
hasWikiEnabled
viewerPermission
defaultBranchRef {
name

View file

@ -87,6 +87,7 @@ func cloneRun(opts *CloneOptions) error {
var repo ghrepo.Interface
var protocol string
if repositoryIsURL {
repoURL, err := git.ParseURL(opts.Repository)
if err != nil {
@ -122,7 +123,12 @@ func cloneRun(opts *CloneOptions) error {
return err
}
}
// Check if the repo name has .wiki extension
wantsWiki := false
if strings.HasSuffix(repo.RepoName(), ".wiki") {
repo = ghrepo.NewWithHost(repo.RepoOwner(), strings.TrimSuffix(repo.RepoName(), ".wiki"), repo.RepoHost())
wantsWiki = true
}
// Load the repo from the API to get the username/repo name in its
// canonical capitalization
canonicalRepo, err := api.GitHubRepo(apiClient, repo)
@ -131,6 +137,14 @@ func cloneRun(opts *CloneOptions) error {
}
canonicalCloneURL := ghrepo.FormatRemoteURL(canonicalRepo, protocol)
// If repo HasWikiEnabled and wantsWiki is true then create a new clone URL
if wantsWiki {
if !canonicalRepo.HasWikiEnabled {
return fmt.Errorf("The '%s' repository does not have a wiki", ghrepo.FullName(canonicalRepo))
}
canonicalCloneURL = strings.TrimSuffix(canonicalCloneURL, ".git") + ".wiki.git"
}
cloneDir, err := git.RunClone(canonicalCloneURL, opts.GitArgs)
if err != nil {
return err

View file

@ -56,6 +56,22 @@ func TestNewCmdClone(t *testing.T) {
args: "OWNER/REPO --depth 1",
wantErr: "unknown flag: --depth\nSeparate git clone flags with '--'.",
},
{
name: "wiki with HTTPS URL",
args: "https://github.com/OWNER/REPO.wiki.git",
wantOpts: CloneOptions{
Repository: "https://github.com/OWNER/REPO.wiki.git",
GitArgs: []string{},
},
},
{
name: "wiki with Full Name URL",
args: "OWNER/REPO.wiki",
wantOpts: CloneOptions{
Repository: "OWNER/REPO.wiki",
GitArgs: []string{},
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
@ -282,3 +298,27 @@ func Test_RepoClone_withoutUsername(t *testing.T) {
assert.Equal(t, 1, cs.Count)
assert.Equal(t, "git clone https://github.com/OWNER/REPO.git", strings.Join(cs.Calls[0].Args, " "))
}
func Test_RepoClone_wiki(t *testing.T) {
reg := &httpmock.Registry{}
reg.Register(
httpmock.GraphQL(`query RepositoryInfo\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"name": "REPO",
"owner": {
"login": "OWNER"
}
} } }
`))
httpClient := &http.Client{Transport: reg}
_, err := runCloneCommand(httpClient, "Owner/repo.wiki")
if err != nil {
assert.Equal(t, "The 'OWNER/REPO' repository does not have a wiki", err.Error())
} else {
t.Fatalf("error running command `repo clone`: %v", err)
}
reg.Verify(t)
}