-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webhook management #683
Open
blaketigges
wants to merge
3
commits into
mvisonneau:main
Choose a base branch
from
blaketigges:webhook-manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Webhook management #683
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -50,6 +50,23 @@ server: | |||||||||
# environment variable) | ||||||||||
secret_token: 063f51ec-09a4-11eb-adc1-0242ac120002 | ||||||||||
|
||||||||||
add_webhooks: | ||||||||||
# Whether to add webhooks to projects from wildcards when | ||||||||||
# exporter starts (optional, default: false) | ||||||||||
on_init: false | ||||||||||
# Whether to add webhooks to projects from wildcards | ||||||||||
# on a regular basis (optional, default: false) | ||||||||||
scheduled: false | ||||||||||
# Interval in seconds to add webhooks to projects | ||||||||||
# from wildcards (optional, default: 43200) | ||||||||||
interval_seconds: 43200 | ||||||||||
|
||||||||||
# GCPE Webhook endpoint URL | ||||||||||
webhook_url: https://gcpe.example.net/webhook | ||||||||||
Comment on lines
+64
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
# Whether to remove webhooks when GCPE shutsdown | ||||||||||
remove_webhooks: false | ||||||||||
Comment on lines
+67
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
# Redis configuration, optional and solely useful for an HA setup. | ||||||||||
# By default the data is held in memory of the exporter | ||||||||||
redis: | ||||||||||
|
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
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
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |||||
"strconv" | ||||||
"strings" | ||||||
|
||||||
"github.com/openlyinc/pointy" | ||||||
log "github.com/sirupsen/logrus" | ||||||
goGitlab "github.com/xanzy/go-gitlab" | ||||||
|
||||||
|
@@ -327,3 +328,95 @@ func isEnvMatchingWilcard(w config.Wildcard, env schemas.Environment) (matches b | |||||
// Then we check if the ref matches the project pull parameters | ||||||
return isEnvMatchingProjectPullEnvironments(w.Pull.Environments, env) | ||||||
} | ||||||
|
||||||
// Add a webhook to every project matching the wildcards. | ||||||
func (c *Controller) addWebhooks(ctx context.Context) error { | ||||||
for _, w := range c.Config.Wildcards { | ||||||
projects, err := c.Gitlab.ListProjects(ctx, w) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
if len(projects) == 0 { // if no wildcards read config.projects | ||||||
for _, p := range c.Config.Projects { | ||||||
sp := schemas.Project{Project: p} | ||||||
projects = append(projects, sp) | ||||||
} | ||||||
} | ||||||
|
||||||
for _, p := range projects { | ||||||
hooks, err := c.Gitlab.GetProjectHooks(ctx, p.Name) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
WURL := c.Config.Server.Webhook.URL | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
opts := goGitlab.AddProjectHookOptions{ // options for hook | ||||||
PushEvents: pointy.Bool(false), | ||||||
PipelineEvents: pointy.Bool(true), | ||||||
DeploymentEvents: pointy.Bool(true), | ||||||
EnableSSLVerification: pointy.Bool(false), | ||||||
URL: &WURL, | ||||||
Token: &c.Config.Server.Webhook.SecretToken, | ||||||
} | ||||||
|
||||||
if len(hooks) == 0 { // if no hooks | ||||||
_, err := c.Gitlab.AddProjectHook(ctx, p.Name, &opts) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
} else { | ||||||
exists := false | ||||||
for _, h := range hooks { | ||||||
if h.URL == WURL { | ||||||
exists = true | ||||||
} | ||||||
} | ||||||
if exists == false { | ||||||
_, err := c.Gitlab.AddProjectHook(ctx, p.Name, &opts) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
func (c *Controller) RemoveWebhooks(ctx context.Context) error { | ||||||
for _, w := range c.Config.Wildcards { | ||||||
projects, err := c.Gitlab.ListProjects(ctx, w) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
if len(projects) == 0 { // if no wildcards read config.projects | ||||||
for _, p := range c.Config.Projects { | ||||||
sp := schemas.Project{Project: p} | ||||||
projects = append(projects, sp) | ||||||
} | ||||||
} | ||||||
|
||||||
for _, p := range projects { | ||||||
hooks, err := c.Gitlab.GetProjectHooks(ctx, p.Name) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
WURL := c.Config.Server.Webhook.URL | ||||||
|
||||||
for _, h := range hooks { | ||||||
if h.URL == WURL { | ||||||
err := c.Gitlab.RemoveProjectHook(ctx, p.Name, h.ID) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
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,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) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.