forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
145 lines (124 loc) · 3.58 KB
/
http.go
File metadata and controls
145 lines (124 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package create
import (
"fmt"
"net/http"
"github.com/cli/cli/api"
)
// repoCreateInput represents input parameters for repoCreate
type repoCreateInput struct {
Name string `json:"name"`
Visibility string `json:"visibility"`
HomepageURL string `json:"homepageUrl,omitempty"`
Description string `json:"description,omitempty"`
OwnerID string `json:"ownerId,omitempty"`
TeamID string `json:"teamId,omitempty"`
HasIssuesEnabled bool `json:"hasIssuesEnabled"`
HasWikiEnabled bool `json:"hasWikiEnabled"`
}
type repoTemplateInput struct {
Name string `json:"name"`
Visibility string `json:"visibility"`
OwnerID string `json:"ownerId,omitempty"`
RepositoryID string `json:"repositoryId,omitempty"`
Description string `json:"description,omitempty"`
}
// repoCreate creates a new GitHub repository
func repoCreate(client *http.Client, hostname string, input repoCreateInput, templateRepositoryID string) (*api.Repository, error) {
apiClient := api.NewClientFromHTTP(client)
if 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, hostname, input.OwnerID)
if err != nil {
return nil, err
}
input.OwnerID = orgID
}
if templateRepositoryID != "" {
var response struct {
CloneTemplateRepository struct {
Repository api.Repository
}
}
if input.OwnerID == "" {
var err error
input.OwnerID, err = api.CurrentUserID(apiClient, hostname)
if err != nil {
return nil, err
}
}
templateInput := repoTemplateInput{
Name: input.Name,
Visibility: input.Visibility,
OwnerID: input.OwnerID,
RepositoryID: templateRepositoryID,
}
variables := map[string]interface{}{
"input": templateInput,
}
err := apiClient.GraphQL(hostname, `
mutation CloneTemplateRepository($input: CloneTemplateRepositoryInput!) {
cloneTemplateRepository(input: $input) {
repository {
id
name
owner { login }
url
}
}
}
`, variables, &response)
if err != nil {
return nil, err
}
return api.InitRepoHostname(&response.CloneTemplateRepository.Repository, hostname), nil
}
var response struct {
CreateRepository struct {
Repository api.Repository
}
}
variables := map[string]interface{}{
"input": input,
}
err := apiClient.GraphQL(hostname, `
mutation RepositoryCreate($input: CreateRepositoryInput!) {
createRepository(input: $input) {
repository {
id
name
owner { login }
url
}
}
}
`, variables, &response)
if err != nil {
return nil, err
}
return api.InitRepoHostname(&response.CreateRepository.Repository, hostname), nil
}
// using API v3 here because the equivalent in GraphQL needs `read:org` scope
func resolveOrganization(client *api.Client, hostname, orgName string) (string, error) {
var response struct {
NodeID string `json:"node_id"`
}
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, hostname, orgName, teamSlug string) (string, string, error) {
var response struct {
NodeID string `json:"node_id"`
Organization struct {
NodeID string `json:"node_id"`
}
}
err := client.REST(hostname, "GET", fmt.Sprintf("orgs/%s/teams/%s", orgName, teamSlug), nil, &response)
return response.Organization.NodeID, response.NodeID, err
}