include system in webhook payload

pull/2674/head
Brad Rydzewski 2019-04-16 08:21:05 -07:00
parent d25d661c75
commit 8042aa4519
7 changed files with 61 additions and 17 deletions

View File

@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- sqlite username column changed to case-insensitive, by [@bradrydzewski](https://github.com/bradrydzewski).
- endpoint to purge repository from database, by [@bradrydzewski](https://github.com/bradrydzewski).
- support for per-organization secrets, by [@bradrydzewski](https://github.com/bradrydzewski).
- include system metadata in global webhooks, by [@bradrydzewski](https://github.com/bradrydzewski).
- update drone-yaml from version 1.0.6 to 1.0.8.
- update drone-runtime from version 1.0.4 to 1.0.6.

View File

@ -95,9 +95,10 @@ func provideSecretPlugin(config spec.Config) core.SecretService {
// provideWebhookPlugin is a Wire provider function that returns
// a webhook plugin based on the environment configuration.
func provideWebhookPlugin(config spec.Config) core.WebhookSender {
return webhook.New(
config.Webhook.Endpoint,
config.Webhook.Secret,
)
func provideWebhookPlugin(config spec.Config, system *core.System) core.WebhookSender {
return webhook.New(webhook.Config{
Endpoint: config.Webhook.Endpoint,
Secret: config.Webhook.Secret,
System: system,
})
}

View File

@ -56,10 +56,10 @@ func InitializeApplication(config2 config.Config) (application, error) {
buildStore := provideBuildStore(db)
stageStore := provideStageStore(db)
scheduler := provideScheduler(stageStore, config2)
webhookSender := provideWebhookPlugin(config2)
system := provideSystem(config2)
webhookSender := provideWebhookPlugin(config2, system)
triggerer := trigger.New(configService, commitService, statusService, buildStore, scheduler, repositoryStore, userStore, webhookSender)
cronScheduler := cron2.New(commitService, cronStore, repositoryStore, userStore, triggerer)
system := provideSystem(config2)
coreLicense := provideLicense(client, config2)
datadog := provideDatadog(userStore, repositoryStore, buildStore, system, coreLicense, config2)
corePubsub := pubsub.New()

24
plugin/webhook/config.go Normal file
View File

@ -0,0 +1,24 @@
// Copyright 2019 Drone IO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package webhook
import "github.com/drone/drone/core"
// Config provides the webhook configuration.
type Config struct {
Endpoint []string
Secret string
System *core.System
}

View File

@ -32,29 +32,39 @@ var signer = httpsignatures.NewSigner(
)
// New returns a new Webhook sender.
func New(endpoints []string, secret string) core.WebhookSender {
func New(config Config) core.WebhookSender {
return &sender{
Endpoints: endpoints,
Secret: secret,
Endpoints: config.Endpoint,
Secret: config.Secret,
System: config.System,
}
}
type payload struct {
*core.WebhookData
System *core.System `json:"system,omitempty"`
}
type sender struct {
Client *http.Client
Endpoints []string
Secret string
System *core.System
}
// Send sends the JSON encoded webhook to the global
// HTTP endpoints.
func (s *sender) Send(ctx context.Context, payload *core.WebhookData) error {
func (s *sender) Send(ctx context.Context, in *core.WebhookData) error {
if len(s.Endpoints) == 0 {
return nil
}
data, _ := json.Marshal(payload)
wrapper := payload{
WebhookData: in,
System: s.System,
}
data, _ := json.Marshal(wrapper)
for _, endpoint := range s.Endpoints {
err := s.send(endpoint, s.Secret, payload.Event, data)
err := s.send(endpoint, s.Secret, in.Event, data)
if err != nil {
return err
}

View File

@ -23,7 +23,7 @@ import (
)
// New returns a no-op Webhook sender.
func New([]string, string) core.WebhookSender {
func New(Config) core.WebhookSender {
return new(noop)
}

View File

@ -46,7 +46,11 @@ func TestWebhook(t *testing.T) {
Reply(200).
Type("application/json")
sender := New([]string{"https://company.com/hooks"}, "GMEuUHQfmrMRsseWxi9YlIeBtn9lm6im")
config := Config{
Endpoint: []string{"https://company.com/hooks"},
Secret: "GMEuUHQfmrMRsseWxi9YlIeBtn9lm6im",
}
sender := New(config)
err := sender.Send(noContext, webhook)
if err != nil {
t.Error(err)
@ -77,7 +81,11 @@ func TestWebhook_NoEndpoints(t *testing.T) {
User: &core.User{Login: "octocat"},
}
sender := New([]string{}, "correct-horse-battery-staple")
config := Config{
Endpoint: []string{},
Secret: "correct-horse-battery-staple",
}
sender := New(config)
err := sender.Send(noContext, webhook)
if err != nil {
t.Error(err)