@@ -22,6 +22,7 @@ import (
22
22
"go.opentelemetry.io/otel/attribute"
23
23
"go.opentelemetry.io/otel/codes"
24
24
sdkTrace "go.opentelemetry.io/otel/sdk/trace"
25
+ "go.opentelemetry.io/otel/trace"
25
26
)
26
27
27
28
const (
@@ -161,40 +162,27 @@ func checkToken(ctx context.Context, name, token string) (happy bool, err error)
161
162
162
163
fmt .Printf ("Checking %q...\n " , name )
163
164
164
- // Make request to e.g. 'https://api.github.com/user' with token
165
- userURL := flags .BaseURL .JoinPath ("user" ).String ()
166
- req , err := http .NewRequestWithContext (ctx , "GET" , userURL , nil )
167
- if err != nil {
168
- return false , fmt .Errorf ("new request: %w" , err )
169
- }
170
- req .Header .Set ("Authorization" , "Bearer " + token )
171
- resp , err := http .DefaultClient .Do (req )
165
+ // Make request to check token
166
+ resp , _ , err := request (ctx , flags .BaseURL .String (), token )
172
167
if err != nil {
173
- return false , fmt .Errorf ("do request: %w" , err )
174
- }
175
- defer resp .Body .Close ()
176
- body , err := io .ReadAll (resp .Body )
177
- if err != nil {
178
- return false , fmt .Errorf ("reading body: %w" , err )
179
- }
180
- if resp .StatusCode != 200 {
181
- if len (body ) > 1024 {
182
- body = body [:1024 ]
183
- }
184
- span .SetAttributes (attribute .String ("ghtokmon.error_body" , strconv .QuoteToASCII (string (body ))))
185
- return false , fmt .Errorf ("got status code %d != 200" , resp .StatusCode )
168
+ return false , fmt .Errorf ("checking token: %w" , err )
186
169
}
187
170
188
- // Parse user login
189
- var user struct {
190
- Login string `json:"login"`
191
- }
192
- err = json .Unmarshal (body , & user )
193
- if err != nil {
194
- return false , fmt .Errorf ("deserializing user: %w" , err )
171
+ // Get user info (if permitted)
172
+ userURL := flags .BaseURL .JoinPath ("user" ).String ()
173
+ _ , userJSON , err := request (ctx , userURL , token )
174
+ if err == nil {
175
+ // Parse user login
176
+ var user struct {
177
+ Login string `json:"login"`
178
+ }
179
+ err = json .Unmarshal (userJSON , & user )
180
+ if err != nil {
181
+ return false , fmt .Errorf ("deserializing user: %w" , err )
182
+ }
183
+ span .SetAttributes (attribute .String ("ghtokmon.token.login" , user .Login ))
184
+ fmt .Printf ("Token user login: %s\n " , user .Login )
195
185
}
196
- span .SetAttributes (attribute .String ("ghtokmon.token.login" , user .Login ))
197
- fmt .Printf ("Token user login: %s\n " , user .Login )
198
186
199
187
happy = true
200
188
@@ -249,6 +237,43 @@ func checkToken(ctx context.Context, name, token string) (happy bool, err error)
249
237
return happy , nil
250
238
}
251
239
240
+ func request (ctx context.Context , url , token string ) (resp * http.Response , body []byte , err error ) {
241
+ ctx , span := otel .Tracer ("" ).Start (ctx , url )
242
+ defer func () {
243
+ if err != nil {
244
+ span .SetStatus (codes .Error , err .Error ())
245
+ }
246
+ span .End ()
247
+ }()
248
+
249
+ req , err := http .NewRequestWithContext (ctx , "GET" , url , nil )
250
+ if err != nil {
251
+ return nil , nil , fmt .Errorf ("new request: %w" , err )
252
+ }
253
+
254
+ req .Header .Set ("Authorization" , "Bearer " + token )
255
+
256
+ resp , err = http .DefaultClient .Do (req )
257
+ if err != nil {
258
+ return nil , nil , fmt .Errorf ("do request: %w" , err )
259
+ }
260
+ defer resp .Body .Close ()
261
+
262
+ body , err = io .ReadAll (resp .Body )
263
+ if err != nil {
264
+ return nil , nil , fmt .Errorf ("reading body: %w" , err )
265
+ }
266
+
267
+ if resp .StatusCode != 200 {
268
+ if len (body ) > 1024 {
269
+ body = body [:1024 ]
270
+ }
271
+ trace .SpanFromContext (ctx ).SetAttributes (attribute .String ("ghtokmon.error_body" , strconv .QuoteToASCII (string (body ))))
272
+ return nil , nil , fmt .Errorf ("got status code %d != 200" , resp .StatusCode )
273
+ }
274
+ return
275
+ }
276
+
252
277
type failedChecksError []string
253
278
254
279
func (ut failedChecksError ) Error () string {
0 commit comments