From 23852b5121a4b8eaa4e30a645a527b926424efdf Mon Sep 17 00:00:00 2001 From: Blake Tigges <30013719+blaketigges@users.noreply.github.com> Date: Wed, 14 Jun 2023 11:41:45 -0400 Subject: [PATCH] Move webhook api calls to client functions --- pkg/controller/webhooks.go | 31 ++++--------- pkg/gitlab/webhooks.go | 86 +++++++++++++++++++++++++++++++++++++ pkg/gitlab/webhooks_test.go | 64 +++++++++++++++++++++++++++ 3 files changed, 158 insertions(+), 23 deletions(-) create mode 100644 pkg/gitlab/webhooks.go create mode 100644 pkg/gitlab/webhooks_test.go diff --git a/pkg/controller/webhooks.go b/pkg/controller/webhooks.go index 0e00413d..7eb636e9 100644 --- a/pkg/controller/webhooks.go +++ b/pkg/controller/webhooks.go @@ -345,11 +345,7 @@ func (c *Controller) addWebhooks(ctx context.Context) error { } for _, p := range projects { - hooks, _, err := c.Gitlab.Projects.ListProjectHooks( - p.Name, - &goGitlab.ListProjectHooksOptions{}, - goGitlab.WithContext(ctx), - ) + hooks, err := c.Gitlab.GetProjectHooks(ctx, p.Name) if err != nil { return err } @@ -365,10 +361,7 @@ func (c *Controller) addWebhooks(ctx context.Context) error { } if len(hooks) == 0 { // if no hooks - _, _, err := c.Gitlab.Projects.AddProjectHook( // add hook - p.Name, - &opts, - goGitlab.WithContext(ctx)) + _, err := c.Gitlab.AddProjectHook(ctx, p.Name, &opts) if err != nil { return err } @@ -380,10 +373,7 @@ func (c *Controller) addWebhooks(ctx context.Context) error { } } if exists == false { - _, _, err := c.Gitlab.Projects.AddProjectHook( // else add hook - p.Name, - &opts, - goGitlab.WithContext(ctx)) + _, err := c.Gitlab.AddProjectHook(ctx, p.Name, &opts) if err != nil { return err } @@ -410,11 +400,7 @@ func (c *Controller) RemoveWebhooks(ctx context.Context) error { } for _, p := range projects { - hooks, _, err := c.Gitlab.Projects.ListProjectHooks( - p.Name, - &goGitlab.ListProjectHooksOptions{}, - goGitlab.WithContext(ctx), - ) + hooks, err := c.Gitlab.GetProjectHooks(ctx, p.Name) if err != nil { return err } @@ -423,11 +409,10 @@ func (c *Controller) RemoveWebhooks(ctx context.Context) error { for _, h := range hooks { if h.URL == WURL { - c.Gitlab.Projects.DeleteProjectHook( - p.Name, - h.ID, - goGitlab.WithContext(ctx), - ) + err := c.Gitlab.RemoveProjectHook(ctx, p.Name, h.ID) + if err != nil { + return err + } } } } diff --git a/pkg/gitlab/webhooks.go b/pkg/gitlab/webhooks.go new file mode 100644 index 00000000..fc63bfc7 --- /dev/null +++ b/pkg/gitlab/webhooks.go @@ -0,0 +1,86 @@ +package gitlab + +import ( + "context" + + log "github.com/sirupsen/logrus" + goGitlab "github.com/xanzy/go-gitlab" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" +) + +// GetProjectHooks .. +func (c *Client) GetProjectHooks(ctx context.Context, projectName string) (hooks []*goGitlab.ProjectHook, err error) { + ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:GetProjectHooks") + defer span.End() + span.SetAttributes(attribute.String("project_name", projectName)) + + log.WithField("project_name", projectName).Trace("listing project hooks") + + c.rateLimit(ctx) + + hooks, resp, err := c.Projects.ListProjectHooks( + projectName, + &goGitlab.ListProjectHooksOptions{}, + goGitlab.WithContext(ctx), + ) + if err != nil { + return + } + + c.requestsRemaining(resp) + + return hooks, nil +} + +// AddProjectHook .. +func (c *Client) AddProjectHook(ctx context.Context, projectName string, options *goGitlab.AddProjectHookOptions) (hook *goGitlab.ProjectHook, err error) { + ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:AddProjectHook") + defer span.End() + span.SetAttributes(attribute.String("project_name", projectName)) + + log.WithField("project_name", projectName).Trace("adding project hook") + + c.rateLimit(ctx) + + hook, resp, err := c.Projects.AddProjectHook( + projectName, + options, + goGitlab.WithContext(ctx), + ) + if err != nil { + return + } + + c.requestsRemaining(resp) + + return hook, nil +} + +// RemoveProjectHook .. +func (c *Client) RemoveProjectHook(ctx context.Context, projectName string, hookID int) (err error) { + ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:RemoveProjectHook") + defer span.End() + span.SetAttributes(attribute.String("project_name", projectName)) + span.SetAttributes(attribute.Int("hook_id", hookID)) + + log.WithFields(log.Fields{ + "project_name": projectName, + "hook_id": hookID, + }).Trace("removing project hook") + + c.rateLimit(ctx) + + resp, err := c.Projects.DeleteProjectHook( + projectName, + hookID, + goGitlab.WithContext(ctx), + ) + if err != nil { + return + } + + c.requestsRemaining(resp) + + return nil +} diff --git a/pkg/gitlab/webhooks_test.go b/pkg/gitlab/webhooks_test.go new file mode 100644 index 00000000..765ebfde --- /dev/null +++ b/pkg/gitlab/webhooks_test.go @@ -0,0 +1,64 @@ +package gitlab + +import ( + "fmt" + "net/http" + "net/url" + "testing" + + "github.com/openlyinc/pointy" + "github.com/stretchr/testify/assert" + goGitlab "github.com/xanzy/go-gitlab" +) + +func TestGetProjectHooks(t *testing.T) { + ctx, mux, server, c := getMockedClient() + defer server.Close() + + mux.HandleFunc(fmt.Sprintf("/api/v4/projects/foo/hooks"), + func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "GET", r.Method) + expectedQueryParams := url.Values{} + assert.Equal(t, expectedQueryParams, r.URL.Query()) + fmt.Fprint(w, `[{"id":1}]`) + }) + + hooks, err := c.GetProjectHooks(ctx, "foo") + fmt.Println(hooks) + assert.NoError(t, err) + assert.Len(t, hooks, 1) +} + +func TestAddProjectHook(t *testing.T) { + ctx, mux, server, c := getMockedClient() + defer server.Close() + + mux.HandleFunc(fmt.Sprintf("/api/v4/projects/foo/hooks"), + func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "POST", r.Method) + expectedQueryParams := url.Values{} + assert.Equal(t, expectedQueryParams, r.URL.Query()) + fmt.Fprint(w, `{"id":1, "url":"www.example.com/webhook", "push_events":false, "pipeline_events": true, "deployment_events": true, "enable_ssl_verification": false}`) + }) + + hook, err := c.AddProjectHook(ctx, "foo", &goGitlab.AddProjectHookOptions{ + PushEvents: pointy.Bool(false), + PipelineEvents: pointy.Bool(true), + DeploymentEvents: pointy.Bool(true), + EnableSSLVerification: pointy.Bool(false), // add config for this later + URL: pointy.String("www.example.com/webhook"), + Token: pointy.String("token"), + }) + + h := goGitlab.ProjectHook{ + URL: "www.example.com/webhook", + ID: 1, + PushEvents: false, + PipelineEvents: true, + DeploymentEvents: true, + EnableSSLVerification: false, + } + + assert.NoError(t, err) + assert.Equal(t, &h, hook) +}