Skip to content

Commit 2723a01

Browse files
author
vilmibm
committed
fix repo generation in org with license/ignore
1 parent 3cc4c40 commit 2723a01

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

pkg/cmd/repo/create/http.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/http"
8+
"strings"
89

910
"github.com/cli/cli/api"
1011
)
@@ -38,6 +39,9 @@ type repoTemplateInput struct {
3839
func repoCreate(client *http.Client, hostname string, input repoCreateInput, templateRepositoryID string) (*api.Repository, error) {
3940
apiClient := api.NewClientFromHTTP(client)
4041

42+
ownerName := input.OwnerID
43+
isOrg := false
44+
4145
if input.TeamID != "" {
4246
orgID, teamID, err := resolveOrganizationTeam(apiClient, hostname, input.OwnerID, input.TeamID)
4347
if err != nil {
@@ -46,7 +50,9 @@ func repoCreate(client *http.Client, hostname string, input repoCreateInput, tem
4650
input.TeamID = teamID
4751
input.OwnerID = orgID
4852
} else if input.OwnerID != "" {
49-
orgID, err := resolveOrganization(apiClient, hostname, input.OwnerID)
53+
var orgID string
54+
var err error
55+
orgID, isOrg, err = resolveOrganization(apiClient, hostname, input.OwnerID)
5056
if err != nil {
5157
return nil, err
5258
}
@@ -109,12 +115,19 @@ func repoCreate(client *http.Client, hostname string, input repoCreateInput, tem
109115
}
110116

111117
if input.GitIgnoreTemplate != "" || input.LicenseTemplate != "" {
118+
input.Visibility = strings.ToLower(input.Visibility)
112119
body := &bytes.Buffer{}
113120
enc := json.NewEncoder(body)
114121
if err := enc.Encode(input); err != nil {
115122
return nil, err
116123
}
117-
repo, err := api.CreateRepoTransformToV4(apiClient, hostname, "POST", "user/repos", body)
124+
125+
path := "user/repos"
126+
if isOrg {
127+
path = fmt.Sprintf("orgs/%s/repos", ownerName)
128+
}
129+
130+
repo, err := api.CreateRepoTransformToV4(apiClient, hostname, "POST", path, body)
118131
if err != nil {
119132
return nil, err
120133
}
@@ -141,12 +154,13 @@ func repoCreate(client *http.Client, hostname string, input repoCreateInput, tem
141154
}
142155

143156
// using API v3 here because the equivalent in GraphQL needs `read:org` scope
144-
func resolveOrganization(client *api.Client, hostname, orgName string) (string, error) {
157+
func resolveOrganization(client *api.Client, hostname, orgName string) (string, bool, error) {
145158
var response struct {
146159
NodeID string `json:"node_id"`
160+
Type string
147161
}
148162
err := client.REST(hostname, "GET", fmt.Sprintf("users/%s", orgName), nil, &response)
149-
return response.NodeID, err
163+
return response.NodeID, response.Type == "Organization", err
150164
}
151165

152166
// using API v3 here because the equivalent in GraphQL needs `read:org` scope

0 commit comments

Comments
 (0)