Skip to content

Commit

Permalink
fix: added a factory helper to create the client
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Jul 11, 2019
1 parent e52dce4 commit 04cc1ce
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A small library with minimal depenencies for working with Webhooks, Commits, Iss
* [GitLab](https://github.com/jenkins-x/go-scm/blob/master/scm/driver/gitlab/gitlab.go#L19)
* [Gitea](https://github.com/jenkins-x/go-scm/blob/master/scm/driver/gitea/gitea.go#L22)
* [Gogs](https://github.com/jenkins-x/go-scm/blob/master/scm/driver/gogs/gogs.go#L22)

* [Fake](https://github.com/jenkins-x/go-scm/blob/master/scm/driver/fake/fake.go)

## Working on the code

Expand All @@ -21,3 +21,10 @@ cd go-scm
go test ./...
```

## Fake driver for testing

When testing the use of go-scm its really handy to use the [fake](https://github.com/jenkins-x/go-scm/blob/master/scm/driver/fake/fake.go) provider which lets you populate the in memory resources inside the driver or query resources after a test has run.

```go
client, data := fake.NewDefault()
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module github.com/jenkins-x/go-scm
require (
github.com/google/go-cmp v0.3.0
github.com/h2non/gock v1.0.9
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
k8s.io/apimachinery v0.0.0-20190703205208-4cfb76a8bf76
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
Expand Down Expand Up @@ -33,8 +34,13 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -43,6 +49,7 @@ golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
Expand Down
10 changes: 3 additions & 7 deletions scm/driver/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"github.com/jenkins-x/go-scm/scm"
)

type fakeClient struct {
}

// NewDefault returns a new GitHub API client using the
// default api.github.com address.
// NewDefault returns a new fake client.
// The Data object lets you pre-load resources into the fake driver or check for results after the
// scm operations have been performed
func NewDefault() (*scm.Client, *Data) {
data := NewData()
client := &wrapper{new(scm.Client)}
Expand All @@ -37,8 +35,6 @@ func NewDefault() (*scm.Client, *Data) {
return client.Client, data
}

// wraper wraps the Client to provide high level helper functions
// for making http requests and unmarshaling the response.
type wrapper struct {
*scm.Client
}
77 changes: 77 additions & 0 deletions scm/factory/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package factory

import (
"context"
"fmt"

"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/go-scm/scm/driver/bitbucket"
"github.com/jenkins-x/go-scm/scm/driver/fake"
"github.com/jenkins-x/go-scm/scm/driver/gitea"
"github.com/jenkins-x/go-scm/scm/driver/github"
"github.com/jenkins-x/go-scm/scm/driver/gitlab"
"github.com/jenkins-x/go-scm/scm/driver/gogs"
"github.com/jenkins-x/go-scm/scm/driver/stash"
"golang.org/x/oauth2"
)

// MissingGitServerURL the error returned if you use a git driver that needs a git server URL
var MissingGitServerURL = fmt.Errorf("No git serverURL was specified")

func NewClient(kind, serverURL, oauthToken string) (*scm.Client, error) {
if kind == "" {
kind = "github"
}
var client *scm.Client
var err error

switch kind {
case "bitbucket":
if serverURL != "" {
client, err = bitbucket.New(serverURL)
} else {
client = bitbucket.NewDefault()
}
case "fake":
client, _ = fake.NewDefault()
case "gitea":
if serverURL == "" {
return nil, MissingGitServerURL
}
client, err = gitea.New(serverURL)
case "github":
if serverURL != "" {
client, err = github.New(serverURL)
} else {
client = github.NewDefault()
}
case "gitlab":
if serverURL != "" {
client, err = gitlab.New(serverURL)
} else {
client = gitlab.NewDefault()
}
case "gogs":
if serverURL == "" {
return nil, MissingGitServerURL
}
client, err = gogs.New(serverURL)
case "stash":
if serverURL == "" {
return nil, MissingGitServerURL
}
client, err = stash.New(serverURL)
default:
return nil, fmt.Errorf("Unsupported $GIT_KIND value: %s", kind)
}
if err != nil {
return client, err
}
if oauthToken != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: oauthToken},
)
client.Client = oauth2.NewClient(context.Background(), ts)
}
return client, err
}
13 changes: 13 additions & 0 deletions scm/factory/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package factory

import "testing"

func TestNewClient(t *testing.T) {
client, err := NewClient("fake", "", "")
if client == nil {
t.Errorf("no client created")
}
if err != nil {
t.Errorf("failed to create client %s", err.Error())
}
}

0 comments on commit 04cc1ce

Please sign in to comment.