/
azure_devops.go
47 lines (40 loc) · 1.19 KB
/
azure_devops.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
package auth
import (
"context"
b64 "encoding/base64"
"fmt"
"regexp"
)
type azureDevops struct {
pat string
}
func newAzureDevops(pat string) *azureDevops {
return &azureDevops{
pat: pat,
}
}
func (a *azureDevops) getPathRegex(organization, project, repository string) ([]*regexp.Regexp, error) {
baseApi, err := regexp.Compile(fmt.Sprintf(`(?i)/%s/_apis\b`, organization))
if err != nil {
return nil, fmt.Errorf("invalid base api regex: %w", err)
}
git, err := regexp.Compile(fmt.Sprintf(`(?i)/%s/%s/_git/%s(/.*)?\b`, organization, project, repository))
if err != nil {
return nil, err
}
api, err := regexp.Compile(fmt.Sprintf(`(?i)/%s/%s/_apis/git/repositories/%s(/.*)?\b`, organization, project, repository))
if err != nil {
return nil, err
}
return []*regexp.Regexp{baseApi, git, api}, nil
}
func (a *azureDevops) getAuthorizationHeader(ctx context.Context, path string) (string, error) {
tokenB64 := b64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("pat:%s", a.pat)))
return fmt.Sprintf("Basic %s", tokenB64), nil
}
func (a *azureDevops) getHost(e *Endpoint, path string) string {
return e.host
}
func (a *azureDevops) getPath(e *Endpoint, path string) string {
return path
}