Skip to content

Commit

Permalink
Move webhook api calls to client functions
Browse files Browse the repository at this point in the history
  • Loading branch information
blaketigges committed Jun 14, 2023
1 parent d242b9d commit 23852b5
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 23 deletions.
31 changes: 8 additions & 23 deletions pkg/controller/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
}
}
}
Expand Down
86 changes: 86 additions & 0 deletions pkg/gitlab/webhooks.go
Original file line number Diff line number Diff line change
@@ -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
}
64 changes: 64 additions & 0 deletions pkg/gitlab/webhooks_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 23852b5

Please sign in to comment.