/
server_api_context.go
66 lines (54 loc) 路 1.73 KB
/
server_api_context.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
package bitbucket
import (
"context"
"fmt"
"net/url"
insights "github.com/reva2/bitbucket-insights-api"
)
// BuildServerAPIContext builds context.Context used to call Bitbucket Server Code Insights API
func BuildServerAPIContext(ctx context.Context, bbURL, user, password, token string) (context.Context, error) {
var err error
ctx, err = withServerVariables(ctx, bbURL)
if err != nil {
return ctx, err
}
if user != "" && password != "" {
ctx = withServerBasicAuth(ctx, user, password)
}
if token != "" {
ctx = withServerAccessToken(ctx, token)
}
return ctx, nil
}
// WithServerBasicAuth adds basic auth credentials to context
func withServerBasicAuth(ctx context.Context, user, password string) context.Context {
return context.WithValue(ctx, insights.ContextBasicAuth, insights.BasicAuth{
UserName: user,
Password: password,
})
}
// WithServerAccessToken adds basic auth credentials to context
func withServerAccessToken(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, insights.ContextAccessToken, token)
}
// WithServerVariables adds server variable to context
func withServerVariables(ctx context.Context, bbURL string) (context.Context, error) {
parsed, err := url.Parse(bbURL)
if err != nil {
return ctx, fmt.Errorf("failed to parse Bitbucket Server URL: %w", err)
}
if parsed.Scheme == "" {
return ctx, fmt.Errorf("unable to determine scheme of Bitbucket Server URL: %w", err)
}
if parsed.Host == "" {
return ctx, fmt.Errorf("unable to determine host of Bitbucket Server URL: %w", err)
}
return context.WithValue(
ctx,
insights.ContextServerVariables,
map[string]string{
"protocol": parsed.Scheme,
"bitbucketDomain": parsed.Host + parsed.Path,
},
), nil
}