"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "pkg/cmd/repo/create/http.go" between
gh-cli-1.12.1.tar.gz and gh-cli-1.13.1.tar.gz

About: GitHub CLI is GitHub’s official command line tool.

http.go  (gh-cli-1.12.1):http.go  (gh-cli-1.13.1)
skipping to change at line 13 skipping to change at line 13
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
"github.com/cli/cli/api" "github.com/cli/cli/api"
) )
// repoCreateInput represents input parameters for repoCreate // repoCreateInput is input parameters for the repoCreate method
type repoCreateInput struct { type repoCreateInput struct {
Name string `json:"name"` Name string
Visibility string `json:"visibility"` HomepageURL string
HomepageURL string `json:"homepageUrl,omitempty"` Description string
Description string `json:"description,omitempty"` Visibility string
OwnerLogin string
OwnerID string `json:"ownerId,omitempty"` TeamSlug string
TeamID string `json:"teamId,omitempty"` TemplateRepositoryID string
HasIssuesEnabled bool
HasWikiEnabled bool
GitIgnoreTemplate string
LicenseTemplate string
}
HasIssuesEnabled bool `json:"hasIssuesEnabled"` // createRepositoryInputV3 is the payload for the repo create REST API
HasWikiEnabled bool `json:"hasWikiEnabled"` type createRepositoryInputV3 struct {
Name string `json:"name"`
HomepageURL string `json:"homepage,omitempty"`
Description string `json:"description,omitempty"`
IsPrivate bool `json:"private"`
Visibility string `json:"visibility,omitempty"`
TeamID uint64 `json:"team_id,omitempty"`
HasIssuesEnabled bool `json:"has_issues"`
HasWikiEnabled bool `json:"has_wiki"`
GitIgnoreTemplate string `json:"gitignore_template,omitempty"` GitIgnoreTemplate string `json:"gitignore_template,omitempty"`
LicenseTemplate string `json:"license_template,omitempty"` LicenseTemplate string `json:"license_template,omitempty"`
} }
type repoTemplateInput struct { // createRepositoryInput is the payload for the repo create GraphQL mutation
Name string `json:"name"` type createRepositoryInput struct {
Visibility string `json:"visibility"` Name string `json:"name"`
OwnerID string `json:"ownerId,omitempty"` HomepageURL string `json:"homepageUrl,omitempty"`
Description string `json:"description,omitempty"`
Visibility string `json:"visibility"`
OwnerID string `json:"ownerId,omitempty"`
TeamID string `json:"teamId,omitempty"`
HasIssuesEnabled bool `json:"hasIssuesEnabled"`
HasWikiEnabled bool `json:"hasWikiEnabled"`
}
RepositoryID string `json:"repositoryId,omitempty"` // cloneTemplateRepositoryInput is the payload for creating a repo from a templa
te using GraphQL
type cloneTemplateRepositoryInput struct {
Name string `json:"name"`
Visibility string `json:"visibility"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
OwnerID string `json:"ownerId"`
RepositoryID string `json:"repositoryId"`
} }
// repoCreate creates a new GitHub repository // repoCreate creates a new GitHub repository
func repoCreate(client *http.Client, hostname string, input repoCreateInput, tem func repoCreate(client *http.Client, hostname string, input repoCreateInput) (*a
plateRepositoryID string) (*api.Repository, error) { pi.Repository, error) {
apiClient := api.NewClientFromHTTP(client)
ownerName := input.OwnerID
isOrg := false isOrg := false
var ownerID string
var teamID string
var teamIDv3 uint64
apiClient := api.NewClientFromHTTP(client)
if input.TeamID != "" { if input.TeamSlug != "" {
orgID, teamID, err := resolveOrganizationTeam(apiClient, hostname team, err := resolveOrganizationTeam(apiClient, hostname, input.O
, input.OwnerID, input.TeamID) wnerLogin, input.TeamSlug)
if err != nil { if err != nil {
return nil, err return nil, err
} }
input.TeamID = teamID teamIDv3 = team.ID
input.OwnerID = orgID teamID = team.NodeID
} else if input.OwnerID != "" { ownerID = team.Organization.NodeID
var orgID string isOrg = true
var err error } else if input.OwnerLogin != "" {
orgID, isOrg, err = resolveOrganization(apiClient, hostname, inpu owner, err := resolveOwner(apiClient, hostname, input.OwnerLogin)
t.OwnerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
input.OwnerID = orgID ownerID = owner.NodeID
isOrg = owner.IsOrganization()
} }
if templateRepositoryID != "" { if input.TemplateRepositoryID != "" {
var response struct { var response struct {
CloneTemplateRepository struct { CloneTemplateRepository struct {
Repository api.Repository Repository api.Repository
} }
} }
if input.OwnerID == "" { if ownerID == "" {
var err error var err error
input.OwnerID, err = api.CurrentUserID(apiClient, hostnam e) ownerID, err = api.CurrentUserID(apiClient, hostname)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
templateInput := repoTemplateInput{
Name: input.Name,
Visibility: input.Visibility,
OwnerID: input.OwnerID,
RepositoryID: templateRepositoryID,
}
variables := map[string]interface{}{ variables := map[string]interface{}{
"input": templateInput, "input": cloneTemplateRepositoryInput{
Name: input.Name,
Description: input.Description,
Visibility: strings.ToUpper(input.Visibility),
OwnerID: ownerID,
RepositoryID: input.TemplateRepositoryID,
},
} }
err := apiClient.GraphQL(hostname, ` err := apiClient.GraphQL(hostname, `
mutation CloneTemplateRepository($input: CloneTemplateRepositoryI nput!) { mutation CloneTemplateRepository($input: CloneTemplateRepositoryI nput!) {
cloneTemplateRepository(input: $input) { cloneTemplateRepository(input: $input) {
repository { repository {
id id
name name
owner { login } owner { login }
url url
skipping to change at line 107 skipping to change at line 134
} }
} }
`, variables, &response) `, variables, &response)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return api.InitRepoHostname(&response.CloneTemplateRepository.Rep ository, hostname), nil return api.InitRepoHostname(&response.CloneTemplateRepository.Rep ository, hostname), nil
} }
var response struct { if input.GitIgnoreTemplate != "" || input.LicenseTemplate != "" {
CreateRepository struct { inputv3 := createRepositoryInputV3{
Repository api.Repository Name: input.Name,
HomepageURL: input.HomepageURL,
Description: input.Description,
IsPrivate: strings.EqualFold(input.Visibility, "P
RIVATE"),
TeamID: teamIDv3,
HasIssuesEnabled: input.HasIssuesEnabled,
HasWikiEnabled: input.HasWikiEnabled,
GitIgnoreTemplate: input.GitIgnoreTemplate,
LicenseTemplate: input.LicenseTemplate,
} }
}
variables := map[string]interface{}{ path := "user/repos"
"input": input, if isOrg {
} path = fmt.Sprintf("orgs/%s/repos", input.OwnerLogin)
inputv3.Visibility = strings.ToLower(input.Visibility)
}
if input.GitIgnoreTemplate != "" || input.LicenseTemplate != "" {
input.Visibility = strings.ToLower(input.Visibility)
body := &bytes.Buffer{} body := &bytes.Buffer{}
enc := json.NewEncoder(body) enc := json.NewEncoder(body)
if err := enc.Encode(input); err != nil { if err := enc.Encode(inputv3); err != nil {
return nil, err return nil, err
} }
path := "user/repos"
if isOrg {
path = fmt.Sprintf("orgs/%s/repos", ownerName)
}
repo, err := api.CreateRepoTransformToV4(apiClient, hostname, "PO ST", path, body) repo, err := api.CreateRepoTransformToV4(apiClient, hostname, "PO ST", path, body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return api.InitRepoHostname(repo, hostname), nil return repo, nil
}
var response struct {
CreateRepository struct {
Repository api.Repository
}
}
variables := map[string]interface{}{
"input": createRepositoryInput{
Name: input.Name,
Description: input.Description,
HomepageURL: input.HomepageURL,
Visibility: strings.ToUpper(input.Visibility),
OwnerID: ownerID,
TeamID: teamID,
HasIssuesEnabled: input.HasIssuesEnabled,
HasWikiEnabled: input.HasWikiEnabled,
},
} }
err := apiClient.GraphQL(hostname, ` err := apiClient.GraphQL(hostname, `
mutation RepositoryCreate($input: CreateRepositoryInput!) { mutation RepositoryCreate($input: CreateRepositoryInput!) {
createRepository(input: $input) { createRepository(input: $input) {
repository { repository {
id id
name name
owner { login } owner { login }
url url
skipping to change at line 156 skipping to change at line 204
} }
} }
`, variables, &response) `, variables, &response)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return api.InitRepoHostname(&response.CreateRepository.Repository, hostna me), nil return api.InitRepoHostname(&response.CreateRepository.Repository, hostna me), nil
} }
// using API v3 here because the equivalent in GraphQL needs `read:org` scope type ownerResponse struct {
func resolveOrganization(client *api.Client, hostname, orgName string) (string, NodeID string `json:"node_id"`
bool, error) { Type string `json:"type"`
var response struct { }
NodeID string `json:"node_id"`
Type string func (r *ownerResponse) IsOrganization() bool {
} return r.Type == "Organization"
}
func resolveOwner(client *api.Client, hostname, orgName string) (*ownerResponse,
error) {
var response ownerResponse
err := client.REST(hostname, "GET", fmt.Sprintf("users/%s", orgName), nil , &response) err := client.REST(hostname, "GET", fmt.Sprintf("users/%s", orgName), nil , &response)
return response.NodeID, response.Type == "Organization", err return &response, err
} }
// using API v3 here because the equivalent in GraphQL needs `read:org` scope type teamResponse struct {
func resolveOrganizationTeam(client *api.Client, hostname, orgName, teamSlug str ID uint64 `json:"id"`
ing) (string, string, error) { NodeID string `json:"node_id"`
var response struct { Organization struct {
NodeID string `json:"node_id"` NodeID string `json:"node_id"`
Organization struct {
NodeID string `json:"node_id"`
}
} }
}
func resolveOrganizationTeam(client *api.Client, hostname, orgName, teamSlug str
ing) (*teamResponse, error) {
var response teamResponse
err := client.REST(hostname, "GET", fmt.Sprintf("orgs/%s/teams/%s", orgNa me, teamSlug), nil, &response) err := client.REST(hostname, "GET", fmt.Sprintf("orgs/%s/teams/%s", orgNa me, teamSlug), nil, &response)
return response.Organization.NodeID, response.NodeID, err return &response, err
} }
// ListGitIgnoreTemplates uses API v3 here because gitignore template isn't supp orted by GraphQL yet. // ListGitIgnoreTemplates uses API v3 here because gitignore template isn't supp orted by GraphQL yet.
func ListGitIgnoreTemplates(client *api.Client, hostname string) ([]string, erro r) { func ListGitIgnoreTemplates(client *api.Client, hostname string) ([]string, erro r) {
var gitIgnoreTemplates []string var gitIgnoreTemplates []string
err := client.REST(hostname, "GET", "gitignore/templates", nil, &gitIgnor eTemplates) err := client.REST(hostname, "GET", "gitignore/templates", nil, &gitIgnor eTemplates)
if err != nil { if err != nil {
return []string{}, err return []string{}, err
} }
return gitIgnoreTemplates, nil return gitIgnoreTemplates, nil
 End of changes. 28 change blocks. 
75 lines changed or deleted 131 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)