Skip to content

Commit

Permalink
add a github wrapper package
Browse files Browse the repository at this point in the history
  • Loading branch information
northdpole committed Oct 15, 2024
1 parent e0fbb1f commit 4aed1ae
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/github/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package clientmock

import (
"context"

"github.com/google/go-github/v65/github"
)

// MockClient mocks the two methods of google's github client we use
type MockClient struct {
ListRepoAlertsCallback func(string, string, *github.AlertListOptions) ([]*github.Alert, *github.Response, error)
ListRepoDependabotAlertsCallback func(string, string, *github.ListAlertsOptions) ([]*github.DependabotAlert, *github.Response, error)
}

// NewMockClient returns a mock github client
func NewMockClient() MockClient {
return MockClient{}
}

// ListRepoAlerts calls the mocked callback
func (m MockClient) ListRepoAlerts(_ context.Context, owner string, repo string, opt *github.AlertListOptions) ([]*github.Alert, *github.Response, error) {
return m.ListRepoAlertsCallback(owner, repo, opt)
}

// ListRepoDependabotAlerts calls the mocked callback
func (m MockClient) ListRepoDependabotAlerts(_ context.Context, owner string, repo string, opt *github.ListAlertsOptions) ([]*github.DependabotAlert, *github.Response, error) {
return m.ListRepoDependabotAlertsCallback(owner, repo, opt)
}
49 changes: 49 additions & 0 deletions pkg/github/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package wrapper

import (
"context"
"log/slog"

"github.com/google/go-github/v65/github"
"golang.org/x/oauth2"
)

// Wrapper is the wrapper interface that allows the github client to be pluggable
type Wrapper interface {
// ListRepoAlerts is a thin wrapper around github's equivalent
ListRepoAlerts(ctx context.Context, owner string, repo string, opt *github.AlertListOptions) ([]*github.Alert, *github.Response, error)

// ListRepoDependabotAlerts is a thin wrapper around github's equivalent
ListRepoDependabotAlerts(ctx context.Context, owner string, repo string, opt *github.ListAlertsOptions) ([]*github.DependabotAlert, *github.Response, error)
}

// Client is the wrapper around google's go-github client
type Client struct {
Client *github.Client
}

// NewClient returns an actual github client
func NewClient(token string) Client {
if token == "" {
slog.Error("Cannot authenticate to github, token is empty")
}
// authenticate to github, start a client
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
oAuthClient := oauth2.NewClient(context.Background(), ts)
// create new github client with accessToken
return Client{
Client: github.NewClient(oAuthClient),
}
}

// ListRepoAlerts is a thin wrapper around github's ListRepoAlerts
func (gc Client) ListRepoAlerts(ctx context.Context, owner string, repo string, opt *github.AlertListOptions) ([]*github.Alert, *github.Response, error) {
return gc.Client.CodeScanning.ListAlertsForRepo(ctx, owner, repo, opt)
}

// ListRepoDependabotAlerts is a thin wrapper around github's ListRepoDependabotAlerts
func (gc Client) ListRepoDependabotAlerts(ctx context.Context, owner string, repo string, opt *github.ListAlertsOptions) ([]*github.DependabotAlert, *github.Response, error) {
return gc.Client.Dependabot.ListRepoAlerts(ctx, owner, repo, opt)
}

0 comments on commit 4aed1ae

Please sign in to comment.