From f8bf602ecd30ef73c13695c2399149767f2f23eb Mon Sep 17 00:00:00 2001 From: Yuto <39439017+utouto97@users.noreply.github.com> Date: Wed, 30 Aug 2023 07:52:57 +0900 Subject: [PATCH] Allow --disable-wiki flag with --template flag in repo create command (#7886) --- pkg/cmd/repo/create/create.go | 4 +- pkg/cmd/repo/create/http.go | 26 +++++++++++++ pkg/cmd/repo/create/http_test.go | 64 ++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/repo/create/create.go b/pkg/cmd/repo/create/create.go index 097bd1935..3cc1d2f62 100644 --- a/pkg/cmd/repo/create/create.go +++ b/pkg/cmd/repo/create/create.go @@ -164,8 +164,8 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co if cmd.Flags().Changed("enable-wiki") { opts.DisableWiki = !enableWiki } - if opts.Template != "" && (opts.Homepage != "" || opts.Team != "" || opts.DisableIssues || opts.DisableWiki) { - return cmdutil.FlagErrorf("the `--template` option is not supported with `--homepage`, `--team`, `--disable-issues`, or `--disable-wiki`") + if opts.Template != "" && (opts.Homepage != "" || opts.Team != "" || opts.DisableIssues) { + return cmdutil.FlagErrorf("the `--template` option is not supported with `--homepage`, `--team`, or `--disable-issues`") } if opts.Template == "" && opts.IncludeAllBranches { diff --git a/pkg/cmd/repo/create/http.go b/pkg/cmd/repo/create/http.go index dc8e708be..d3a077300 100644 --- a/pkg/cmd/repo/create/http.go +++ b/pkg/cmd/repo/create/http.go @@ -64,6 +64,11 @@ type cloneTemplateRepositoryInput struct { IncludeAllBranches bool `json:"includeAllBranches"` } +type updateRepositoryInput struct { + RepositoryID string `json:"repositoryId"` + HasWikiEnabled bool `json:"hasWikiEnabled"` +} + // repoCreate creates a new GitHub repository func repoCreate(client *http.Client, hostname string, input repoCreateInput) (*api.Repository, error) { isOrg := false @@ -133,6 +138,27 @@ func repoCreate(client *http.Client, hostname string, input repoCreateInput) (*a return nil, err } + if !input.HasWikiEnabled { + updateVariables := map[string]interface{}{ + "input": updateRepositoryInput{ + RepositoryID: response.CloneTemplateRepository.Repository.ID, + HasWikiEnabled: input.HasWikiEnabled, + }, + } + + if err := apiClient.GraphQL(hostname, ` + mutation UpdateRepository($input: UpdateRepositoryInput!) { + updateRepository(input: $input) { + repository { + id + } + } + } + `, updateVariables, nil); err != nil { + return nil, err + } + } + return api.InitRepoHostname(&response.CloneTemplateRepository.Repository, hostname), nil } diff --git a/pkg/cmd/repo/create/http_test.go b/pkg/cmd/repo/create/http_test.go index 058901738..7ebeb93b8 100644 --- a/pkg/cmd/repo/create/http_test.go +++ b/pkg/cmd/repo/create/http_test.go @@ -231,6 +231,70 @@ func Test_repoCreate(t *testing.T) { }, wantRepo: "https://github.com/OWNER/REPO", }, + { + name: "create personal repo from template repo, and disable wiki", + hostname: "github.com", + input: repoCreateInput{ + Name: "gen-project", + Description: "my generated project", + Visibility: "private", + TemplateRepositoryID: "TPLID", + HasIssuesEnabled: true, + HasWikiEnabled: false, + IncludeAllBranches: false, + }, + stubs: func(t *testing.T, r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query UserCurrent\b`), + httpmock.StringResponse(`{"data": {"viewer": {"id":"USERID"} } }`)) + r.Register( + httpmock.GraphQL(`mutation CloneTemplateRepository\b`), + httpmock.GraphQLMutation( + `{ + "data": { + "cloneTemplateRepository": { + "repository": { + "id": "REPOID", + "name": "REPO", + "owner": {"login":"OWNER"}, + "url": "the://URL" + } + } + } + }`, + func(inputs map[string]interface{}) { + assert.Equal(t, map[string]interface{}{ + "name": "gen-project", + "description": "my generated project", + "visibility": "PRIVATE", + "ownerId": "USERID", + "repositoryId": "TPLID", + "includeAllBranches": false, + }, inputs) + }), + ) + r.Register( + httpmock.GraphQL(`mutation UpdateRepository\b`), + httpmock.GraphQLMutation( + `{ + "data": { + "updateRepository": { + "repository": { + "id": "REPOID" + } + } + } + }`, + func(inputs map[string]interface{}) { + assert.Equal(t, map[string]interface{}{ + "repositoryId": "REPOID", + "hasWikiEnabled": false, + }, inputs) + }), + ) + }, + wantRepo: "https://github.com/OWNER/REPO", + }, { name: "create org repo from template repo", hostname: "github.com",