-
Notifications
You must be signed in to change notification settings - Fork 745
/
config.go
218 lines (176 loc) · 4.62 KB
/
config.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package github
import (
"context"
"net/http"
"net/url"
"path"
"github.com/google/go-github/v32/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/logging"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
type Config struct {
Token string
Owner string
Organization string
BaseURL string
Insecure bool
Individual bool
Anonymous bool
}
type Owner struct {
name string
id int64
v3client *github.Client
v4client *githubv4.Client
StopContext context.Context
IsOrganization bool
}
func RateLimitedHTTPClient(client *http.Client) *http.Client {
client.Transport = NewEtagTransport(client.Transport)
client.Transport = NewRateLimitTransport(client.Transport)
client.Transport = logging.NewTransport("Github", client.Transport)
return client
}
func (c *Config) AuthenticatedHTTPClient() *http.Client {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.Token},
)
client := oauth2.NewClient(ctx, ts)
return RateLimitedHTTPClient(client)
}
func (c *Config) AnonymousHTTPClient() *http.Client {
client := &http.Client{Transport: &http.Transport{}}
return RateLimitedHTTPClient(client)
}
func (c *Config) NewGraphQLClient(client *http.Client) (*githubv4.Client, error) {
uv4, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
if uv4.String() != "https://api.github.com/" {
uv4.Path = path.Join(uv4.Path, "api/graphql/")
} else {
uv4.Path = path.Join(uv4.Path, "graphql")
}
return githubv4.NewEnterpriseClient(uv4.String(), client), nil
}
func (c *Config) NewRESTClient(client *http.Client) (*github.Client, error) {
uv3, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
if uv3.String() != "https://api.github.com/" {
uv3.Path = uv3.Path + "api/v3/"
}
v3client, err := github.NewEnterpriseClient(uv3.String(), "", client)
if err != nil {
return nil, err
}
v3client.BaseURL = uv3
return v3client, nil
}
func (c *Config) ConfigureOwner(owner *Owner) (*Owner, error) {
ctx := context.Background()
owner.name = c.Owner
if owner.name == "" {
// Discover authenticated user
user, _, err := owner.v3client.Users.Get(ctx, "")
if err != nil {
return nil, err
}
owner.name = user.GetLogin()
} else {
remoteOrg, _, err := owner.v3client.Organizations.Get(ctx, owner.name)
if err == nil {
if remoteOrg != nil {
owner.id = remoteOrg.GetID()
owner.IsOrganization = true
}
}
}
return owner, nil
}
// Meta returns the meta parameter that is passed into subsequent resources
// https://godoc.org/github.com/hashicorp/terraform-plugin-sdk/helper/schema#ConfigureFunc
func (c *Config) Meta() (interface{}, error) {
var client *http.Client
if c.Anonymous {
client = c.AnonymousHTTPClient()
} else {
client = c.AuthenticatedHTTPClient()
}
v3client, err := c.NewRESTClient(client)
if err != nil {
return nil, err
}
v4client, err := c.NewGraphQLClient(client)
if err != nil {
return nil, err
}
var owner Owner
owner.v4client = v4client
owner.v3client = v3client
if c.Anonymous {
return &owner, nil
} else {
return c.ConfigureOwner(&owner)
}
}
// Clients configures and returns a fully initialized GithubClient and Githubv4Client
func (c *Config) Clients() (interface{}, error) {
var owner Owner
var ts oauth2.TokenSource
var tc *http.Client
ctx := context.Background()
ts = oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.Token},
)
tc = oauth2.NewClient(ctx, ts)
tc.Transport = NewEtagTransport(tc.Transport)
tc.Transport = NewRateLimitTransport(tc.Transport)
tc.Transport = logging.NewTransport("Github", tc.Transport)
// Create GraphQL Client
uv4, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
uv4.Path = path.Join(uv4.Path, "graphql")
v4client := githubv4.NewEnterpriseClient(uv4.String(), tc)
// Create Rest Client
uv3, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
if uv3.String() != "https://api.github.com/" {
uv3.Path = uv3.Path + "v3/"
}
v3client, err := github.NewEnterpriseClient(uv3.String(), "", tc)
if err != nil {
return nil, err
}
v3client.BaseURL = uv3
owner.v3client = v3client
owner.v4client = v4client
owner.name = c.Owner
if owner.name == "" {
// Discover authenticated user
user, _, err := owner.v3client.Users.Get(ctx, "")
if err != nil {
return nil, err
}
owner.name = user.GetLogin()
} else {
remoteOrg, _, err := owner.v3client.Organizations.Get(ctx, owner.name)
if err == nil {
if remoteOrg != nil {
owner.id = remoteOrg.GetID()
owner.IsOrganization = true
}
} else {
return nil, err
}
}
return &owner, nil
}