Merge pull request #22 from thurt/add-ref-to-github

add ref (SHA, branch, or tag) to the github migration source
This commit is contained in:
Dale Hui 2018-05-04 10:13:18 +02:00 committed by GitHub
commit 81e9fff605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 6 deletions

View File

@ -1,6 +1,6 @@
# github # github
`github://user:personal-access-token@owner/repo/path` `github://user:personal-access-token@owner/repo/path#ref`
| URL Query | WithInstance Config | Description | | URL Query | WithInstance Config | Description |
|------------|---------------------|-------------| |------------|---------------------|-------------|
@ -9,3 +9,4 @@
| owner | | the repo owner | | owner | | the repo owner |
| repo | | the name of the repository | | repo | | the name of the repository |
| path | | path in repo to migrations | | path | | path in repo to migrations |
| ref | | (optional) can be a SHA, branch, or tag |

View File

@ -11,8 +11,8 @@ import (
"path" "path"
"strings" "strings"
"github.com/google/go-github/github"
"github.com/golang-migrate/migrate/source" "github.com/golang-migrate/migrate/source"
"github.com/google/go-github/github"
) )
func init() { func init() {
@ -34,6 +34,7 @@ type Github struct {
pathOwner string pathOwner string
pathRepo string pathRepo string
path string path string
options *github.RepositoryContentGetOptions
migrations *source.Migrations migrations *source.Migrations
} }
@ -64,6 +65,7 @@ func (g *Github) Open(url string) (source.Driver, error) {
client: github.NewClient(tr.Client()), client: github.NewClient(tr.Client()),
url: url, url: url,
migrations: source.NewMigrations(), migrations: source.NewMigrations(),
options: &github.RepositoryContentGetOptions{Ref: u.Fragment},
} }
// set owner, repo and path in repo // set owner, repo and path in repo
@ -96,7 +98,7 @@ func WithInstance(client *github.Client, config *Config) (source.Driver, error)
} }
func (g *Github) readDirectory() error { func (g *Github) readDirectory() error {
fileContent, dirContents, _, err := g.client.Repositories.GetContents(context.Background(), g.pathOwner, g.pathRepo, g.path, &github.RepositoryContentGetOptions{}) fileContent, dirContents, _, err := g.client.Repositories.GetContents(context.Background(), g.pathOwner, g.pathRepo, g.path, g.options)
if err != nil { if err != nil {
return err return err
} }
@ -147,7 +149,7 @@ func (g *Github) Next(version uint) (nextVersion uint, err error) {
func (g *Github) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) { func (g *Github) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) {
if m, ok := g.migrations.Up(version); ok { if m, ok := g.migrations.Up(version); ok {
file, _, _, err := g.client.Repositories.GetContents(context.Background(), g.pathOwner, g.pathRepo, path.Join(g.path, m.Raw), &github.RepositoryContentGetOptions{}) file, _, _, err := g.client.Repositories.GetContents(context.Background(), g.pathOwner, g.pathRepo, path.Join(g.path, m.Raw), g.options)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
@ -164,7 +166,7 @@ func (g *Github) ReadUp(version uint) (r io.ReadCloser, identifier string, err e
func (g *Github) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) { func (g *Github) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) {
if m, ok := g.migrations.Down(version); ok { if m, ok := g.migrations.Down(version); ok {
file, _, _, err := g.client.Repositories.GetContents(context.Background(), g.pathOwner, g.pathRepo, path.Join(g.path, m.Raw), &github.RepositoryContentGetOptions{}) file, _, _, err := g.client.Repositories.GetContents(context.Background(), g.pathOwner, g.pathRepo, path.Join(g.path, m.Raw), g.options)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }

View File

@ -23,7 +23,7 @@ func Test(t *testing.T) {
} }
g := &Github{} g := &Github{}
d, err := g.Open("github://" + GithubTestSecret + "@mattes/migrate_test_tmp/test") d, err := g.Open("github://" + GithubTestSecret + "@mattes/migrate_test_tmp/test#452b8003e7")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }