Skip to content
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

[MM-320] Added config setting to enable/disable child pipeline notification #409

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@
"placeholder": "",
"default": null
},
{
"key": "EnableChildPipelineNotifications",
"display_name": "Enable Child Pipelines Notification:",
"type": "bool",
"help_text": "Allow the plugin to post notfication for child pipelines when the pipeline subscription is created in a channel.",
"placeholder": "",
"default": true
},
{
"key": "EnableCodePreview",
"display_name": "Enable Code Previews:",
Expand Down
19 changes: 10 additions & 9 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ import (
// If you add non-reference types to your configuration struct, be sure to rewrite Clone as a deep
// copy appropriate for your types.
type configuration struct {
GitlabURL string `json:"gitlaburl"`
GitlabOAuthClientID string `json:"gitlaboauthclientid"`
GitlabOAuthClientSecret string `json:"gitlaboauthclientsecret"`
WebhookSecret string `json:"webhooksecret"`
EncryptionKey string `json:"encryptionkey"`
GitlabGroup string `json:"gitlabgroup"`
EnablePrivateRepo bool `json:"enableprivaterepo"`
EnableCodePreview string `json:"enablecodepreview"`
UsePreregisteredApplication bool `json:"usepreregisteredapplication"`
GitlabURL string `json:"gitlaburl"`
GitlabOAuthClientID string `json:"gitlaboauthclientid"`
GitlabOAuthClientSecret string `json:"gitlaboauthclientsecret"`
WebhookSecret string `json:"webhooksecret"`
EncryptionKey string `json:"encryptionkey"`
GitlabGroup string `json:"gitlabgroup"`
EnablePrivateRepo bool `json:"enableprivaterepo"`
EnableCodePreview string `json:"enablecodepreview"`
UsePreregisteredApplication bool `json:"usepreregisteredapplication"`
EnableChildPipelineNotifications bool `json:"enablechildpipelinenotifications"`
}

// Clone shallow copies the configuration. Your implementation may require a deep copy if
Expand Down
7 changes: 6 additions & 1 deletion server/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

const (
webhookTimeout = 10 * time.Second
webhookTimeout = 10 * time.Second
eventSourceParentPipeline = "parent_pipeline"
)

type gitlabRetreiver struct {
Expand Down Expand Up @@ -118,6 +119,10 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
repoPrivate = event.Project.Visibility == gitlabLib.PrivateVisibility
pathWithNamespace = event.Project.PathWithNamespace
fromUser = event.User.Username
if !p.configuration.EnableChildPipelineNotifications && event.ObjectAttributes.Source == eventSourceParentPipeline {
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
return
}

handlers, errHandler = p.WebhookHandler.HandlePipeline(ctx, event)
case *gitlabLib.JobEvent:
repoPrivate = event.Repository.Visibility == gitlabLib.PrivateVisibility
Expand Down
42 changes: 42 additions & 0 deletions server/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,45 @@ func TestHandleWebhookToChannel(t *testing.T) {
mock.AssertNumberOfCalls(t, "PublishWebSocketEvent", 1)
mock.AssertNumberOfCalls(t, "CreatePost", 1)
}

func TestHandleWebhookForChildPipelineNotficationDisabled(t *testing.T) {
p := &Plugin{configuration: &configuration{WebhookSecret: "secret", EnableChildPipelineNotifications: false}, WebhookHandler: fakeWebhookHandler{}}

mock := &plugintest.API{}
p.SetAPI(mock)
p.client = pluginapi.NewClient(mock, p.Driver)

req := httptest.NewRequest("POST", "/", bytes.NewBufferString(`{"user": {"username":"test"}, "object_attributes": {"source":"parent_pipeline"}}`))
req.Header.Add("X-Gitlab-Token", "secret")
req.Header.Add("X-Gitlab-Event", string(gitlabLib.EventTypePipeline))
w := httptest.NewRecorder()

p.handleWebhook(w, req)
resp := w.Result()

assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestHandleWebhookForChildPipelineNotficationEnabled(t *testing.T) {
p := &Plugin{configuration: &configuration{WebhookSecret: "secret", EnableChildPipelineNotifications: true}, WebhookHandler: fakeWebhookHandler{}}

mock := &plugintest.API{}
mock.On("KVGet", "test_gitlabusername").Return([]byte("1"), nil).Once()
mock.On("PublishWebSocketEvent", WsEventRefresh, map[string]interface{}(nil), &model.WebsocketBroadcast{UserId: "1"}).Return(nil).Once()
p.SetAPI(mock)
p.client = pluginapi.NewClient(mock, p.Driver)

req := httptest.NewRequest("POST", "/", bytes.NewBufferString(`{"user": {"username":"test"}, "object_attributes": {"source":"parent_pipeline"}}`))
req.Header.Add("X-Gitlab-Token", "secret")
req.Header.Add("X-Gitlab-Event", string(gitlabLib.EventTypePipeline))
w := httptest.NewRecorder()

p.handleWebhook(w, req)
resp := w.Result()

assert.Equal(t, http.StatusOK, resp.StatusCode)
mock.AssertCalled(t, "KVGet", "test_gitlabusername")
mock.AssertNumberOfCalls(t, "KVGet", 1)
mock.AssertCalled(t, "PublishWebSocketEvent", WsEventRefresh, map[string]interface{}(nil), &model.WebsocketBroadcast{UserId: "1"})
mock.AssertNumberOfCalls(t, "PublishWebSocketEvent", 1)
}
Loading