From fd57835bb9d620bc06b5e682a7484442535912cc Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Fri, 27 Nov 2020 16:20:19 +0530 Subject: [PATCH] Fix repo clone wiki --- api/queries_repo.go | 2 ++ pkg/cmd/repo/clone/clone.go | 16 ++++++++++++- pkg/cmd/repo/clone/clone_test.go | 40 ++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/api/queries_repo.go b/api/queries_repo.go index 54f88a7e1..7ed7abb79 100644 --- a/api/queries_repo.go +++ b/api/queries_repo.go @@ -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 diff --git a/pkg/cmd/repo/clone/clone.go b/pkg/cmd/repo/clone/clone.go index 73f4d7187..4e74dbc3c 100644 --- a/pkg/cmd/repo/clone/clone.go +++ b/pkg/cmd/repo/clone/clone.go @@ -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 diff --git a/pkg/cmd/repo/clone/clone_test.go b/pkg/cmd/repo/clone/clone_test.go index e7aa57b08..88c653a89 100644 --- a/pkg/cmd/repo/clone/clone_test.go +++ b/pkg/cmd/repo/clone/clone_test.go @@ -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) +}