From a6933b7563bb4b8fe3d4f8a9fde95bedef94b377 Mon Sep 17 00:00:00 2001 From: sg Date: Mon, 7 Oct 2024 18:54:27 +0100 Subject: [PATCH] add a github wrapper package --- pkg/github/mock/mock.go | 28 +++++++++++++++++++++++ pkg/github/wrapper.go | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 pkg/github/mock/mock.go create mode 100644 pkg/github/wrapper.go diff --git a/pkg/github/mock/mock.go b/pkg/github/mock/mock.go new file mode 100644 index 000000000..47e4ac53c --- /dev/null +++ b/pkg/github/mock/mock.go @@ -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) +} + +// MockClient 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) +} + +// CreateComment 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) +} diff --git a/pkg/github/wrapper.go b/pkg/github/wrapper.go new file mode 100644 index 000000000..0a170f158 --- /dev/null +++ b/pkg/github/wrapper.go @@ -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), + } +} + +// UploadSarif is a thin wrapper around github's upload sarif +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) +} + +// CreateComment is a thin wrapper around github's CreateComment +func (gc Client) ListRepoDependabotAlerts(ctx context.Context, owner string, repo string, opt *github.ListAlertsOptions) ([]*github.DependabotAlert, *github.Response, error) { + return gc.Client.Dependabot.ListRepoAlerts(context.Background(), owner, repo, opt) +}