Repository: cli/cli
Pull request #8188 (2023-10-14 02:04:36)

update(repo create): Support for `--homepage` flag for template-based repository creation flow
Fixes https://github.com/cli/cli/issues/7934

This PR adds support for:

- Using `--homepage` flag to set the repository's homepage URL in the template-based repository creation flow

Similar to the work in [Disable wiki with template](https://github.com/cli/cli/pull/7886) and [Disable issues with template](https://github.com/cli/cli/pull/7918), this is done using [updateRepository](https://docs.github.com/en/graphql/reference/mutations#updaterepository) after cloning repo from template repo.

Testing instructions:

- With this branch checked out locally, run `repo create <repo-name> --template <template-repo-owner>/<template-repo-slug> --private --disable-issues --homepage "https://example.com"`
- Homepage URL should be set properly 

Test template to try with: https://github.com/moul/golang-repo-template

arunsathiya: >From what I can see here this really only addresses allowing the --homepage flag to be used in conjunction with the --template flag

Ah, I see what happened here @samcoe. I was working off of an older version of the `trunk` branch that I didn't realize support for `--disable-issues`, and failed to notice that even after rebasing with the latest `trunk`. 

I have updated the PR title and body to reflect that this PR adds support only for the homepage flag.

Thanks for the review and please let me know if you have any other comments!

Patch:
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,
 				},
 			}
 


Test patch:
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",
+								"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)
 						}),
 				)