Enable repo create for GHE

This commit is contained in:
Mislav Marohnić 2020-08-14 18:55:11 +02:00
parent 4315c09501
commit 85f0f3aad7
4 changed files with 20 additions and 28 deletions

View file

@ -104,7 +104,6 @@ func createRun(opts *CreateOptions) error {
return err
}
// TODO: GHE support
gist, err := apiCreate(httpClient, ghinstance.OverridableDefault(), opts.Description, opts.Public, files)
if err != nil {
var httpError api.HTTPError

View file

@ -86,23 +86,23 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
func createRun(opts *CreateOptions) error {
projectDir, projectDirErr := git.ToplevelDir()
orgName := ""
name := opts.Name
var repoToCreate ghrepo.Interface
if name != "" {
if strings.Contains(name, "/") {
newRepo, err := ghrepo.FromFullName(name)
if opts.Name != "" {
if strings.Contains(opts.Name, "/") {
var err error
repoToCreate, err = ghrepo.FromFullName(opts.Name)
if err != nil {
return fmt.Errorf("argument error: %w", err)
}
orgName = newRepo.RepoOwner()
name = newRepo.RepoName()
} else {
repoToCreate = ghrepo.New("", opts.Name)
}
} else {
if projectDirErr != nil {
return projectDirErr
}
name = path.Base(projectDir)
repoToCreate = ghrepo.New("", path.Base(projectDir))
}
visibility := "PRIVATE"
@ -111,9 +111,9 @@ func createRun(opts *CreateOptions) error {
}
input := repoCreateInput{
Name: name,
Name: repoToCreate.RepoName(),
Visibility: visibility,
OwnerID: orgName,
OwnerID: repoToCreate.RepoOwner(),
TeamID: opts.Team,
Description: opts.Description,
HomepageURL: opts.Homepage,
@ -126,7 +126,7 @@ func createRun(opts *CreateOptions) error {
return err
}
repo, err := repoCreate(httpClient, input)
repo, err := repoCreate(httpClient, repoToCreate.RepoHost(), input)
if err != nil {
return err
}
@ -146,8 +146,7 @@ func createRun(opts *CreateOptions) error {
if err != nil {
return err
}
// TODO: GHE support
protocol, err := cfg.Get("", "git_protocol")
protocol, err := cfg.Get(repo.RepoHost(), "git_protocol")
if err != nil {
return err
}

View file

@ -5,7 +5,6 @@ import (
"net/http"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghinstance"
)
// repoCreateInput represents input parameters for repoCreate
@ -23,7 +22,7 @@ type repoCreateInput struct {
}
// repoCreate creates a new GitHub repository
func repoCreate(client *http.Client, input repoCreateInput) (*api.Repository, error) {
func repoCreate(client *http.Client, hostname string, input repoCreateInput) (*api.Repository, error) {
apiClient := api.NewClientFromHTTP(client)
var response struct {
@ -33,14 +32,14 @@ func repoCreate(client *http.Client, input repoCreateInput) (*api.Repository, er
}
if input.TeamID != "" {
orgID, teamID, err := resolveOrganizationTeam(apiClient, input.OwnerID, input.TeamID)
orgID, teamID, err := resolveOrganizationTeam(apiClient, hostname, input.OwnerID, input.TeamID)
if err != nil {
return nil, err
}
input.TeamID = teamID
input.OwnerID = orgID
} else if input.OwnerID != "" {
orgID, err := resolveOrganization(apiClient, input.OwnerID)
orgID, err := resolveOrganization(apiClient, hostname, input.OwnerID)
if err != nil {
return nil, err
}
@ -51,9 +50,6 @@ func repoCreate(client *http.Client, input repoCreateInput) (*api.Repository, er
"input": input,
}
// TODO: GHE support
hostname := ghinstance.Default()
err := apiClient.GraphQL(hostname, `
mutation RepositoryCreate($input: CreateRepositoryInput!) {
createRepository(input: $input) {
@ -74,24 +70,22 @@ func repoCreate(client *http.Client, input repoCreateInput) (*api.Repository, er
}
// using API v3 here because the equivalent in GraphQL needs `read:org` scope
func resolveOrganization(client *api.Client, orgName string) (string, error) {
func resolveOrganization(client *api.Client, hostname, orgName string) (string, error) {
var response struct {
NodeID string `json:"node_id"`
}
// TODO: GHE support
err := client.REST(ghinstance.Default(), "GET", fmt.Sprintf("users/%s", orgName), nil, &response)
err := client.REST(hostname, "GET", fmt.Sprintf("users/%s", orgName), nil, &response)
return response.NodeID, err
}
// using API v3 here because the equivalent in GraphQL needs `read:org` scope
func resolveOrganizationTeam(client *api.Client, orgName, teamSlug string) (string, string, error) {
func resolveOrganizationTeam(client *api.Client, hostname, orgName, teamSlug string) (string, string, error) {
var response struct {
NodeID string `json:"node_id"`
Organization struct {
NodeID string `json:"node_id"`
}
}
// TODO: GHE support
err := client.REST(ghinstance.Default(), "GET", fmt.Sprintf("orgs/%s/teams/%s", orgName, teamSlug), nil, &response)
err := client.REST(hostname, "GET", fmt.Sprintf("orgs/%s/teams/%s", orgName, teamSlug), nil, &response)
return response.Organization.NodeID, response.NodeID, err
}

View file

@ -21,7 +21,7 @@ func Test_RepoCreate(t *testing.T) {
HomepageURL: "http://example.com",
}
_, err := repoCreate(httpClient, input)
_, err := repoCreate(httpClient, "github.com", input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}