Skip to content

Commit

Permalink
Add Test for verbose logging on github.Client
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofranssen committed Aug 6, 2020
1 parent 0b66005 commit d610adf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 26 deletions.
34 changes: 34 additions & 0 deletions lib/github/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package github

import (
"context"
"io"
"net/http"

"github.com/google/go-github/v32/github"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"

"github.com/philips-labs/tabia/lib/transport"
)

type Client struct {
httpClient *http.Client
restClient *github.Client
*githubv4.Client
}

func NewClientWithTokenAuth(token string, writer io.Writer) *Client {
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
httpClient := oauth2.NewClient(context.Background(), src)
if writer != nil {
httpClient.Transport = transport.TeeRoundTripper{
RoundTripper: httpClient.Transport,
Writer: writer,
}
}
client := githubv4.NewClient(httpClient)
restClient := github.NewClient(httpClient)

return &Client{httpClient, restClient, client}
}
24 changes: 24 additions & 0 deletions lib/github/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package github_test

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/philips-labs/tabia/lib/github"
)

func TestClient(t *testing.T) {
assert := assert.New(t)

var buf strings.Builder

client := github.NewClientWithTokenAuth("token", &buf)
var q struct{}
client.Client.Query(context.Background(), q, nil)

assert.NotEmpty(buf)
assert.Equal("POST: https://api.github.com/graphql {\"query\":\"{}\"}\n", buf.String())
}
26 changes: 0 additions & 26 deletions lib/github/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,15 @@ package github
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"time"

"golang.org/x/oauth2"

"github.com/google/go-github/v32/github"
"github.com/shurcooL/githubv4"

"github.com/philips-labs/tabia/lib/github/graphql"
"github.com/philips-labs/tabia/lib/transport"
)

type Client struct {
httpClient *http.Client
restClient *github.Client
*githubv4.Client
}

func NewClientWithTokenAuth(token string, writer io.Writer) *Client {
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
httpClient := oauth2.NewClient(context.Background(), src)
if writer != nil {
httpClient.Transport = transport.TeeRoundTripper{
RoundTripper: httpClient.Transport,
Writer: writer,
}
}
client := githubv4.NewClient(httpClient)
restClient := github.NewClient(httpClient)

return &Client{httpClient, restClient, client}
}

//go:generate stringer -type=Visibility

// Visibility indicates repository visibility
Expand Down

0 comments on commit d610adf

Please sign in to comment.