Skip to content

Commit 302d70f

Browse files
committed
feat(webhooks): allow description to be edited
1 parent bbf5ad2 commit 302d70f

File tree

8 files changed

+40
-7
lines changed

8 files changed

+40
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Sequel.migration do
2+
change do
3+
add_column(:webhooks, :description, String)
4+
end
5+
end

lib/pact_broker/api/decorators/webhook_decorator.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class WebhookEventDecorator < BaseDecorator
1616
property :name
1717
end
1818

19+
property :description, getter: lambda { |context| context[:decorator].display_description }
20+
1921
property :consumer, :class => PactBroker::Domain::Pacticipant do
2022
property :name
2123
end
@@ -33,7 +35,7 @@ class WebhookEventDecorator < BaseDecorator
3335

3436
link :self do | options |
3537
{
36-
title: represented.description,
38+
title: display_description,
3739
href: webhook_url(represented.uuid, options[:base_url])
3840
}
3941

@@ -90,6 +92,14 @@ def from_json represented
9092
end
9193
end
9294
end
95+
96+
def display_description
97+
if represented.description && represented.description.strip.size > 0
98+
represented.description
99+
else
100+
represented.scope_description
101+
end
102+
end
93103
end
94104
end
95105
end

lib/pact_broker/domain/webhook.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ class Webhook
1010
include Messages
1111
include Logging
1212

13-
attr_accessor :uuid, :consumer, :provider, :request, :created_at, :updated_at, :events, :enabled
13+
attr_accessor :uuid, :consumer, :provider, :request, :created_at, :updated_at, :events, :enabled, :description
1414
attr_reader :attributes
1515

1616
def initialize attributes = {}
1717
@attributes = attributes
1818
@uuid = attributes[:uuid]
19+
@description = attributes[:description]
1920
@request = attributes[:request]
2021
@consumer = attributes[:consumer]
2122
@provider = attributes[:provider]
@@ -25,7 +26,7 @@ def initialize attributes = {}
2526
@updated_at = attributes[:updated_at]
2627
end
2728

28-
def description
29+
def scope_description
2930
if consumer && provider
3031
"A webhook for the pact between #{consumer.name} and #{provider.name}"
3132
elsif provider

lib/pact_broker/webhooks/webhook.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def self.not_plain_text_password password
4646
def to_domain
4747
Domain::Webhook.new(
4848
uuid: uuid,
49+
description: description,
4950
consumer: consumer,
5051
provider: provider,
5152
events: events,
@@ -86,6 +87,7 @@ def is_for? relationship
8687
def self.properties_hash_from_domain webhook
8788
is_json_request_body = !(String === webhook.request.body || webhook.request.body.nil?) # Can't rely on people to set content type
8889
{
90+
description: webhook.description,
8991
method: webhook.request.method,
9092
url: webhook.request.url,
9193
username: webhook.request.username,
@@ -97,7 +99,7 @@ def self.properties_hash_from_domain webhook
9799
end
98100
end
99101

100-
Webhook.plugin :timestamps, :update_on_create=>true
102+
Webhook.plugin :timestamps, update_on_create: true
101103

102104
class WebhookHeader < Sequel::Model
103105
associate(:many_to_one, :webhook, :class => "PactBroker::Repositories::Webhook", :key => :webhook_id, :primary_key => :id)

spec/features/create_webhook_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
let(:webhook_json) { webhook_hash.to_json }
1212
let(:webhook_hash) do
1313
{
14+
description: "trigger build",
1415
enabled: false,
1516
events: [{
1617
name: 'something_happened'

spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module PactBroker
55
module Api
66
module Decorators
77
describe WebhookDecorator do
8+
let(:description) { "Trigger build" }
89
let(:headers) { { :'Content-Type' => 'application/json' } }
910
let(:request) do
1011
{
@@ -27,6 +28,7 @@ module Decorators
2728

2829
let(:webhook) do
2930
Domain::Webhook.new(
31+
description: description,
3032
request: webhook_request,
3133
uuid: 'some-uuid',
3234
consumer: consumer,
@@ -43,6 +45,10 @@ module Decorators
4345
describe 'to_json' do
4446
let(:parsed_json) { JSON.parse(subject.to_json(user_options: { base_url: 'http://example.org' }), symbolize_names: true) }
4547

48+
it 'includes the description' do
49+
expect(parsed_json[:description]).to eq "Trigger build"
50+
end
51+
4652
it 'includes the request' do
4753
expect(parsed_json[:request]).to eq request
4854
end
@@ -136,6 +142,14 @@ module Decorators
136142
expect(parsed_json[:request][:headers][:'Authorization']).to eq "**********"
137143
end
138144
end
145+
146+
context "when the description is empty" do
147+
let(:description) { " " }
148+
149+
it 'uses the scope description' do
150+
expect(parsed_json[:description]).to match /A webhook/
151+
end
152+
end
139153
end
140154

141155
describe 'from_json' do

spec/lib/pact_broker/domain/webhook_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ module Domain
2020

2121
subject(:webhook) { Webhook.new(request: request_template, consumer: consumer, provider: provider) }
2222

23-
describe "description" do
24-
subject { webhook.description }
23+
describe "scope_description" do
24+
subject { webhook.scope_description }
2525

2626
context "with a consumer and provider" do
2727
it { is_expected.to eq "A webhook for the pact between Consumer and Provider" }

spec/lib/pact_broker/webhooks/service_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module Webhooks
7171
end
7272

7373
it "logs that no webhook was found" do
74-
expect(logger).to receive(:debug).with(/No webhook found/)
74+
expect(logger).to receive(:debug).with(/No enabled webhooks found/)
7575
subject
7676
end
7777
end

0 commit comments

Comments
 (0)