-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move webhook api calls to client functions
- Loading branch information
1 parent
d242b9d
commit 23852b5
Showing
3 changed files
with
158 additions
and
23 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
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,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 | ||
} |
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,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) | ||
} |