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 1/2] 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) +} From 39a0a8c57c851a2763248550348043cf2af7e163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 15 Dec 2020 17:38:21 +0100 Subject: [PATCH 2/2] Improve clone wiki test --- pkg/cmd/repo/clone/clone.go | 11 ++++--- pkg/cmd/repo/clone/clone_test.go | 53 ++++++++------------------------ 2 files changed, 18 insertions(+), 46 deletions(-) diff --git a/pkg/cmd/repo/clone/clone.go b/pkg/cmd/repo/clone/clone.go index 4e74dbc3c..ee2a2ecdf 100644 --- a/pkg/cmd/repo/clone/clone.go +++ b/pkg/cmd/repo/clone/clone.go @@ -123,12 +123,13 @@ 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 + + wantsWiki := strings.HasSuffix(repo.RepoName(), ".wiki") + if wantsWiki { + repoName := strings.TrimSuffix(repo.RepoName(), ".wiki") + repo = ghrepo.NewWithHost(repo.RepoOwner(), repoName, repo.RepoHost()) } + // Load the repo from the API to get the username/repo name in its // canonical capitalization canonicalRepo, err := api.GitHubRepo(apiClient, repo) diff --git a/pkg/cmd/repo/clone/clone_test.go b/pkg/cmd/repo/clone/clone_test.go index 88c653a89..f6b612202 100644 --- a/pkg/cmd/repo/clone/clone_test.go +++ b/pkg/cmd/repo/clone/clone_test.go @@ -56,22 +56,6 @@ 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) { @@ -184,6 +168,16 @@ func Test_RepoClone(t *testing.T) { args: "Owner/Repo", want: "git clone https://github.com/OWNER/REPO.git", }, + { + name: "clone wiki", + args: "Owner/Repo.wiki", + want: "git clone https://github.com/OWNER/REPO.wiki.git", + }, + { + name: "wiki URL", + args: "https://github.com/owner/repo.wiki", + want: "git clone https://github.com/OWNER/REPO.wiki.git", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -195,7 +189,8 @@ func Test_RepoClone(t *testing.T) { "name": "REPO", "owner": { "login": "OWNER" - } + }, + "hasWikiEnabled": true } } } `)) @@ -298,27 +293,3 @@ 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) -}