-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* exported Github struct fields and ReadDirectory method * github ee implementation, tests and docs * build fixes * Github Enterprise API endpoint based on docs * addressing PR comments * code review * make linter happy * parseBool() takes fallback * pr comments * tweaks to Config{}
- Loading branch information
Showing
10 changed files
with
238 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build github | ||
|
||
package cli | ||
|
||
import ( | ||
_ "github.com/golang-migrate/migrate/v4/source/github_ee" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.github_test_secrets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
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/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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |