From 9b037ffac1850eae4c413174411726db0542a4fd Mon Sep 17 00:00:00 2001 From: Arun Sathiya Date: Tue, 17 Oct 2023 00:01:32 -0700 Subject: [PATCH] update(repo create): Support for `--homepage` flag for template-based repository creation flow (#8188) --- pkg/cmd/repo/create/create.go | 4 +- pkg/cmd/repo/create/http.go | 4 +- pkg/cmd/repo/create/http_test.go | 136 ++++++++++++++++++++++++++++++- 3 files changed, 139 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/repo/create/create.go b/pkg/cmd/repo/create/create.go index 4a94c05a2..c050607ed 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 != "") { - return cmdutil.FlagErrorf("the `--template` option is not supported with `--homepage`, or `--team`") + if opts.Template != "" && opts.Team != "" { + return cmdutil.FlagErrorf("the `--template` option is not supported with `--team`") } if opts.Template == "" && opts.IncludeAllBranches { diff --git a/pkg/cmd/repo/create/http.go b/pkg/cmd/repo/create/http.go index 86024a254..ffe9dbb6f 100644 --- a/pkg/cmd/repo/create/http.go +++ b/pkg/cmd/repo/create/http.go @@ -69,6 +69,7 @@ type updateRepositoryInput struct { RepositoryID string `json:"repositoryId"` HasWikiEnabled bool `json:"hasWikiEnabled"` HasIssuesEnabled bool `json:"hasIssuesEnabled"` + HomepageURL string `json:"homepageUrl,omitempty"` } // repoCreate creates a new GitHub repository @@ -140,12 +141,13 @@ func repoCreate(client *http.Client, hostname string, input repoCreateInput) (*a return nil, err } - if !input.HasWikiEnabled || !input.HasIssuesEnabled { + if !input.HasWikiEnabled || !input.HasIssuesEnabled || input.HomepageURL != "" { updateVariables := map[string]interface{}{ "input": updateRepositoryInput{ RepositoryID: response.CloneTemplateRepository.Repository.ID, HasWikiEnabled: input.HasWikiEnabled, HasIssuesEnabled: input.HasIssuesEnabled, + HomepageURL: input.HomepageURL, }, } diff --git a/pkg/cmd/repo/create/http_test.go b/pkg/cmd/repo/create/http_test.go index 86831899d..cfe14563a 100644 --- a/pkg/cmd/repo/create/http_test.go +++ b/pkg/cmd/repo/create/http_test.go @@ -288,8 +288,8 @@ func Test_repoCreate(t *testing.T) { func(inputs map[string]interface{}) { assert.Equal(t, map[string]interface{}{ "repositoryId": "REPOID", - "hasWikiEnabled": false, "hasIssuesEnabled": true, + "hasWikiEnabled": false, }, inputs) }), ) @@ -353,8 +353,140 @@ func Test_repoCreate(t *testing.T) { func(inputs map[string]interface{}) { assert.Equal(t, map[string]interface{}{ "repositoryId": "REPOID", - "hasWikiEnabled": true, "hasIssuesEnabled": false, + "hasWikiEnabled": true, + }, inputs) + }), + ) + }, + wantRepo: "https://github.com/OWNER/REPO", + }, + { + name: "create personal repo from template repo, and disable both wiki and issues", + hostname: "github.com", + input: repoCreateInput{ + Name: "gen-project", + Description: "my generated project", + Visibility: "private", + TemplateRepositoryID: "TPLID", + HasIssuesEnabled: false, + 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", + "hasIssuesEnabled": false, + "hasWikiEnabled": false, + }, inputs) + }), + ) + }, + wantRepo: "https://github.com/OWNER/REPO", + }, + { + name: "create personal repo from template repo, and set homepage url", + hostname: "github.com", + input: repoCreateInput{ + Name: "gen-project", + Description: "my generated project", + Visibility: "private", + TemplateRepositoryID: "TPLID", + HasIssuesEnabled: true, + HasWikiEnabled: true, + IncludeAllBranches: false, + HomepageURL: "https://example.com", + }, + 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", + "hasIssuesEnabled": true, + "hasWikiEnabled": true, + "homepageUrl": "https://example.com", }, inputs) }), )