Skip to content

Commit

Permalink
Add default GitHub API client (golang-migrate#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
ganboonhong authored Jan 31, 2021
1 parent eb62a38 commit dab829b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
8 changes: 5 additions & 3 deletions source/github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

This driver is catered for those that want to source migrations from [github.com](https://github.com). The URL scheme doesn't require a hostname, as it just simply defaults to `github.com`.

`github://user:personal-access-token@owner/repo/path#ref`
Authenticated client: `github://user:personal-access-token@owner/repo/path#ref`

Unauthenticated client: `github://owner/repo/path#ref`

| URL Query | WithInstance Config | Description |
|------------|---------------------|-------------|
| user | | The username of the user connecting |
| personal-access-token | | An access token from GitHub (https://github.com/settings/tokens) |
| user | | (optional) The username of the user connecting |
| personal-access-token | | (optional) An access token from GitHub (https://github.com/settings/tokens) |
| owner | | the repo owner |
| repo | | the name of the repository |
| path | | path in repo to migrations |
Expand Down
28 changes: 15 additions & 13 deletions source/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
nurl "net/url"
"os"
"path"
Expand Down Expand Up @@ -48,22 +49,23 @@ func (g *Github) Open(url string) (source.Driver, error) {
return nil, err
}

if u.User == nil {
return nil, ErrNoUserInfo
}

password, ok := u.User.Password()
if !ok {
return nil, ErrNoUserInfo
}
// client defaults to http.DefaultClient
var client *http.Client
if u.User != nil {
password, ok := u.User.Password()
if !ok {
return nil, ErrNoUserInfo
}

tr := &github.BasicAuthTransport{
Username: u.User.Username(),
Password: password,
tr := &github.BasicAuthTransport{
Username: u.User.Username(),
Password: password,
}
client = tr.Client()
}

gn := &Github{
client: github.NewClient(tr.Client()),
client: github.NewClient(client),
migrations: source.NewMigrations(),
options: &github.RepositoryContentGetOptions{Ref: u.Fragment},
}
Expand Down Expand Up @@ -144,7 +146,7 @@ func (g *Github) Close() error {
return nil
}

func (g *Github) First() (version uint, er error) {
func (g *Github) First() (version uint, err error) {
g.ensureFields()

if v, ok := g.migrations.First(); !ok {
Expand Down
27 changes: 27 additions & 0 deletions source/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package github

import (
"bytes"
"fmt"
"io/ioutil"
"testing"

st "github.com/golang-migrate/migrate/v4/source/testing"
"github.com/stretchr/testify/assert"
)

var GithubTestSecret = "" // username:token
Expand All @@ -30,3 +32,28 @@ func Test(t *testing.T) {

st.Test(t, d)
}

func TestDefaultClient(t *testing.T) {
g := &Github{}
owner := "golang-migrate"
repo := "migrate"
path := "source/github/examples/migrations"

url := fmt.Sprintf("github://%s/%s/%s", owner, repo, path)
d, err := g.Open(url)
if err != nil {
t.Fatal(err)
}

ver, err := d.First()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, uint(1085649617), ver)

ver, err = d.Next(ver)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, uint(1185749658), ver)
}

0 comments on commit dab829b

Please sign in to comment.