-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0fbb1f
commit 4aed1ae
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |