Skip to content

Commit 85f0f3a

Browse files
committed
Enable repo create for GHE
1 parent 4315c09 commit 85f0f3a

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

pkg/cmd/gist/create/create.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ func createRun(opts *CreateOptions) error {
104104
return err
105105
}
106106

107-
// TODO: GHE support
108107
gist, err := apiCreate(httpClient, ghinstance.OverridableDefault(), opts.Description, opts.Public, files)
109108
if err != nil {
110109
var httpError api.HTTPError

pkg/cmd/repo/create/create.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,23 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
8686
func createRun(opts *CreateOptions) error {
8787
projectDir, projectDirErr := git.ToplevelDir()
8888

89-
orgName := ""
90-
name := opts.Name
89+
var repoToCreate ghrepo.Interface
9190

92-
if name != "" {
93-
if strings.Contains(name, "/") {
94-
newRepo, err := ghrepo.FromFullName(name)
91+
if opts.Name != "" {
92+
if strings.Contains(opts.Name, "/") {
93+
var err error
94+
repoToCreate, err = ghrepo.FromFullName(opts.Name)
9595
if err != nil {
9696
return fmt.Errorf("argument error: %w", err)
9797
}
98-
orgName = newRepo.RepoOwner()
99-
name = newRepo.RepoName()
98+
} else {
99+
repoToCreate = ghrepo.New("", opts.Name)
100100
}
101101
} else {
102102
if projectDirErr != nil {
103103
return projectDirErr
104104
}
105-
name = path.Base(projectDir)
105+
repoToCreate = ghrepo.New("", path.Base(projectDir))
106106
}
107107

108108
visibility := "PRIVATE"
@@ -111,9 +111,9 @@ func createRun(opts *CreateOptions) error {
111111
}
112112

113113
input := repoCreateInput{
114-
Name: name,
114+
Name: repoToCreate.RepoName(),
115115
Visibility: visibility,
116-
OwnerID: orgName,
116+
OwnerID: repoToCreate.RepoOwner(),
117117
TeamID: opts.Team,
118118
Description: opts.Description,
119119
HomepageURL: opts.Homepage,
@@ -126,7 +126,7 @@ func createRun(opts *CreateOptions) error {
126126
return err
127127
}
128128

129-
repo, err := repoCreate(httpClient, input)
129+
repo, err := repoCreate(httpClient, repoToCreate.RepoHost(), input)
130130
if err != nil {
131131
return err
132132
}
@@ -146,8 +146,7 @@ func createRun(opts *CreateOptions) error {
146146
if err != nil {
147147
return err
148148
}
149-
// TODO: GHE support
150-
protocol, err := cfg.Get("", "git_protocol")
149+
protocol, err := cfg.Get(repo.RepoHost(), "git_protocol")
151150
if err != nil {
152151
return err
153152
}

pkg/cmd/repo/create/http.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66

77
"github.com/cli/cli/api"
8-
"github.com/cli/cli/internal/ghinstance"
98
)
109

1110
// repoCreateInput represents input parameters for repoCreate
@@ -23,7 +22,7 @@ type repoCreateInput struct {
2322
}
2423

2524
// repoCreate creates a new GitHub repository
26-
func repoCreate(client *http.Client, input repoCreateInput) (*api.Repository, error) {
25+
func repoCreate(client *http.Client, hostname string, input repoCreateInput) (*api.Repository, error) {
2726
apiClient := api.NewClientFromHTTP(client)
2827

2928
var response struct {
@@ -33,14 +32,14 @@ func repoCreate(client *http.Client, input repoCreateInput) (*api.Repository, er
3332
}
3433

3534
if input.TeamID != "" {
36-
orgID, teamID, err := resolveOrganizationTeam(apiClient, input.OwnerID, input.TeamID)
35+
orgID, teamID, err := resolveOrganizationTeam(apiClient, hostname, input.OwnerID, input.TeamID)
3736
if err != nil {
3837
return nil, err
3938
}
4039
input.TeamID = teamID
4140
input.OwnerID = orgID
4241
} else if input.OwnerID != "" {
43-
orgID, err := resolveOrganization(apiClient, input.OwnerID)
42+
orgID, err := resolveOrganization(apiClient, hostname, input.OwnerID)
4443
if err != nil {
4544
return nil, err
4645
}
@@ -51,9 +50,6 @@ func repoCreate(client *http.Client, input repoCreateInput) (*api.Repository, er
5150
"input": input,
5251
}
5352

54-
// TODO: GHE support
55-
hostname := ghinstance.Default()
56-
5753
err := apiClient.GraphQL(hostname, `
5854
mutation RepositoryCreate($input: CreateRepositoryInput!) {
5955
createRepository(input: $input) {
@@ -74,24 +70,22 @@ func repoCreate(client *http.Client, input repoCreateInput) (*api.Repository, er
7470
}
7571

7672
// using API v3 here because the equivalent in GraphQL needs `read:org` scope
77-
func resolveOrganization(client *api.Client, orgName string) (string, error) {
73+
func resolveOrganization(client *api.Client, hostname, orgName string) (string, error) {
7874
var response struct {
7975
NodeID string `json:"node_id"`
8076
}
81-
// TODO: GHE support
82-
err := client.REST(ghinstance.Default(), "GET", fmt.Sprintf("users/%s", orgName), nil, &response)
77+
err := client.REST(hostname, "GET", fmt.Sprintf("users/%s", orgName), nil, &response)
8378
return response.NodeID, err
8479
}
8580

8681
// using API v3 here because the equivalent in GraphQL needs `read:org` scope
87-
func resolveOrganizationTeam(client *api.Client, orgName, teamSlug string) (string, string, error) {
82+
func resolveOrganizationTeam(client *api.Client, hostname, orgName, teamSlug string) (string, string, error) {
8883
var response struct {
8984
NodeID string `json:"node_id"`
9085
Organization struct {
9186
NodeID string `json:"node_id"`
9287
}
9388
}
94-
// TODO: GHE support
95-
err := client.REST(ghinstance.Default(), "GET", fmt.Sprintf("orgs/%s/teams/%s", orgName, teamSlug), nil, &response)
89+
err := client.REST(hostname, "GET", fmt.Sprintf("orgs/%s/teams/%s", orgName, teamSlug), nil, &response)
9690
return response.Organization.NodeID, response.NodeID, err
9791
}

pkg/cmd/repo/create/http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func Test_RepoCreate(t *testing.T) {
2121
HomepageURL: "http://example.com",
2222
}
2323

24-
_, err := repoCreate(httpClient, input)
24+
_, err := repoCreate(httpClient, "github.com", input)
2525
if err != nil {
2626
t.Fatalf("unexpected error: %v", err)
2727
}

0 commit comments

Comments
 (0)