forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_ee.go
97 lines (75 loc) · 1.77 KB
/
github_ee.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
package github_ee
import (
"crypto/tls"
"fmt"
"net/http"
nurl "net/url"
"strconv"
"strings"
"github.com/golang-migrate/migrate/v4/source"
gh "github.com/golang-migrate/migrate/v4/source/github"
"github.com/google/go-github/v39/github"
)
func init() {
source.Register("github-ee", &GithubEE{})
}
type GithubEE struct {
source.Driver
}
func (g *GithubEE) Open(url string) (source.Driver, error) {
verifyTLS := true
u, err := nurl.Parse(url)
if err != nil {
return nil, err
}
if o := u.Query().Get("verify-tls"); o != "" {
verifyTLS = parseBool(o, verifyTLS)
}
if u.User == nil {
return nil, gh.ErrNoUserInfo
}
password, ok := u.User.Password()
if !ok {
return nil, gh.ErrNoUserInfo
}
ghc, err := g.createGithubClient(u.Host, u.User.Username(), password, verifyTLS)
if err != nil {
return nil, err
}
pe := strings.Split(strings.Trim(u.Path, "/"), "/")
if len(pe) < 1 {
return nil, gh.ErrInvalidRepo
}
cfg := &gh.Config{
Owner: pe[0],
Repo: pe[1],
Ref: u.Fragment,
}
if len(pe) > 2 {
cfg.Path = strings.Join(pe[2:], "/")
}
i, err := gh.WithInstance(ghc, cfg)
if err != nil {
return nil, err
}
return &GithubEE{Driver: i}, nil
}
func (g *GithubEE) createGithubClient(host, username, password string, verifyTLS bool) (*github.Client, error) {
tr := &github.BasicAuthTransport{
Username: username,
Password: password,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifyTLS},
},
}
apiHost := fmt.Sprintf("https://%s/api/v3", host)
uploadHost := fmt.Sprintf("https://uploads.%s", host)
return github.NewEnterpriseClient(apiHost, uploadHost, tr.Client())
}
func parseBool(val string, fallback bool) bool {
b, err := strconv.ParseBool(val)
if err != nil {
return fallback
}
return b
}