snapshot of https://github.com/golang-migrate/migrate.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
1
source/github_ee/.gitignore
vendored
Normal file
1
source/github_ee/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.github_test_secrets
|
||||
21
source/github_ee/README.md
Normal file
21
source/github_ee/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# github ee
|
||||
|
||||
## GitHub Enterprise Edition
|
||||
|
||||
This driver is catered for those who run GitHub Enterprise under private infrastructure.
|
||||
|
||||
The below URL scheme illustrates how to source migration files from GitHub Enterprise.
|
||||
|
||||
GitHub client for Go requires API and Uploads endpoint hosts in order to create an instance of GitHub Enterprise Client. We're making an assumption that the API and Uploads are available under `https://api.*` and `https://uploads.*` respectively. [GitHub Enterprise Installation Guide](https://help.github.com/en/enterprise/2.15/admin/installation/enabling-subdomain-isolation) recommends that you enable Subdomain isolation feature.
|
||||
|
||||
`github-ee://user:personal-access-token@host/owner/repo/path?verify-tls=true#ref`
|
||||
|
||||
| URL Query | WithInstance Config | Description |
|
||||
|------------|---------------------|-------------|
|
||||
| user | | The username of the user connecting |
|
||||
| personal-access-token | | Personal access token from your GitHub Enterprise instance |
|
||||
| owner | | the repo owner |
|
||||
| repo | | the name of the repository |
|
||||
| path | | path in repo to migrations |
|
||||
| ref | | (optional) can be a SHA, branch, or tag |
|
||||
| verify-tls | | (optional) defaults to `true`. This option sets `tls.Config.InsecureSkipVerify` accordingly |
|
||||
97
source/github_ee/github_ee.go
Normal file
97
source/github_ee/github_ee.go
Normal file
@@ -0,0 +1,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
|
||||
}
|
||||
44
source/github_ee/github_ee_test.go
Normal file
44
source/github_ee/github_ee_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package github_ee
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
nurl "net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) {
|
||||
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/v3/repos/mattes/migrate_test_tmp/contents/test" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if ref := r.URL.Query().Get("ref"); ref != "452b8003e7" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
_, err := w.Write([]byte("[]"))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
u, err := nurl.Parse(ts.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
g := &GithubEE{}
|
||||
_, err = g.Open("github-ee://foo:bar@" + u.Host + "/mattes/migrate_test_tmp/test?verify-tls=false#452b8003e7")
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user