From 302d70f6e5faf4dc0ffcebb1c85cf0a2e4d0b5c4 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 23 May 2019 11:39:20 +1000 Subject: [PATCH] feat(webhooks): allow description to be edited --- .../20190525_add_description_column_to_webhooks.rb | 5 +++++ .../api/decorators/webhook_decorator.rb | 12 +++++++++++- lib/pact_broker/domain/webhook.rb | 5 +++-- lib/pact_broker/webhooks/webhook.rb | 4 +++- spec/features/create_webhook_spec.rb | 1 + .../api/decorators/webhook_decorator_spec.rb | 14 ++++++++++++++ spec/lib/pact_broker/domain/webhook_spec.rb | 4 ++-- spec/lib/pact_broker/webhooks/service_spec.rb | 2 +- 8 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 db/migrations/20190525_add_description_column_to_webhooks.rb diff --git a/db/migrations/20190525_add_description_column_to_webhooks.rb b/db/migrations/20190525_add_description_column_to_webhooks.rb new file mode 100644 index 000000000..3e5f16852 --- /dev/null +++ b/db/migrations/20190525_add_description_column_to_webhooks.rb @@ -0,0 +1,5 @@ +Sequel.migration do + change do + add_column(:webhooks, :description, String) + end +end diff --git a/lib/pact_broker/api/decorators/webhook_decorator.rb b/lib/pact_broker/api/decorators/webhook_decorator.rb index 96969e6aa..d4cb67812 100644 --- a/lib/pact_broker/api/decorators/webhook_decorator.rb +++ b/lib/pact_broker/api/decorators/webhook_decorator.rb @@ -16,6 +16,8 @@ class WebhookEventDecorator < BaseDecorator property :name end + property :description, getter: lambda { |context| context[:decorator].display_description } + property :consumer, :class => PactBroker::Domain::Pacticipant do property :name end @@ -33,7 +35,7 @@ class WebhookEventDecorator < BaseDecorator link :self do | options | { - title: represented.description, + title: display_description, href: webhook_url(represented.uuid, options[:base_url]) } @@ -90,6 +92,14 @@ def from_json represented end end end + + def display_description + if represented.description && represented.description.strip.size > 0 + represented.description + else + represented.scope_description + end + end end end end diff --git a/lib/pact_broker/domain/webhook.rb b/lib/pact_broker/domain/webhook.rb index 8904b2dc4..1653310fc 100644 --- a/lib/pact_broker/domain/webhook.rb +++ b/lib/pact_broker/domain/webhook.rb @@ -10,12 +10,13 @@ class Webhook include Messages include Logging - attr_accessor :uuid, :consumer, :provider, :request, :created_at, :updated_at, :events, :enabled + attr_accessor :uuid, :consumer, :provider, :request, :created_at, :updated_at, :events, :enabled, :description attr_reader :attributes def initialize attributes = {} @attributes = attributes @uuid = attributes[:uuid] + @description = attributes[:description] @request = attributes[:request] @consumer = attributes[:consumer] @provider = attributes[:provider] @@ -25,7 +26,7 @@ def initialize attributes = {} @updated_at = attributes[:updated_at] end - def description + def scope_description if consumer && provider "A webhook for the pact between #{consumer.name} and #{provider.name}" elsif provider diff --git a/lib/pact_broker/webhooks/webhook.rb b/lib/pact_broker/webhooks/webhook.rb index 3690add01..dfe0f53ec 100644 --- a/lib/pact_broker/webhooks/webhook.rb +++ b/lib/pact_broker/webhooks/webhook.rb @@ -46,6 +46,7 @@ def self.not_plain_text_password password def to_domain Domain::Webhook.new( uuid: uuid, + description: description, consumer: consumer, provider: provider, events: events, @@ -86,6 +87,7 @@ def is_for? relationship def self.properties_hash_from_domain webhook is_json_request_body = !(String === webhook.request.body || webhook.request.body.nil?) # Can't rely on people to set content type { + description: webhook.description, method: webhook.request.method, url: webhook.request.url, username: webhook.request.username, @@ -97,7 +99,7 @@ def self.properties_hash_from_domain webhook end end - Webhook.plugin :timestamps, :update_on_create=>true + Webhook.plugin :timestamps, update_on_create: true class WebhookHeader < Sequel::Model associate(:many_to_one, :webhook, :class => "PactBroker::Repositories::Webhook", :key => :webhook_id, :primary_key => :id) diff --git a/spec/features/create_webhook_spec.rb b/spec/features/create_webhook_spec.rb index 456710f81..faa4fa819 100644 --- a/spec/features/create_webhook_spec.rb +++ b/spec/features/create_webhook_spec.rb @@ -11,6 +11,7 @@ let(:webhook_json) { webhook_hash.to_json } let(:webhook_hash) do { + description: "trigger build", enabled: false, events: [{ name: 'something_happened' diff --git a/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb index fd755eec6..8b2c533a0 100644 --- a/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb @@ -5,6 +5,7 @@ module PactBroker module Api module Decorators describe WebhookDecorator do + let(:description) { "Trigger build" } let(:headers) { { :'Content-Type' => 'application/json' } } let(:request) do { @@ -27,6 +28,7 @@ module Decorators let(:webhook) do Domain::Webhook.new( + description: description, request: webhook_request, uuid: 'some-uuid', consumer: consumer, @@ -43,6 +45,10 @@ module Decorators describe 'to_json' do let(:parsed_json) { JSON.parse(subject.to_json(user_options: { base_url: 'http://example.org' }), symbolize_names: true) } + it 'includes the description' do + expect(parsed_json[:description]).to eq "Trigger build" + end + it 'includes the request' do expect(parsed_json[:request]).to eq request end @@ -136,6 +142,14 @@ module Decorators expect(parsed_json[:request][:headers][:'Authorization']).to eq "**********" end end + + context "when the description is empty" do + let(:description) { " " } + + it 'uses the scope description' do + expect(parsed_json[:description]).to match /A webhook/ + end + end end describe 'from_json' do diff --git a/spec/lib/pact_broker/domain/webhook_spec.rb b/spec/lib/pact_broker/domain/webhook_spec.rb index eecaaaca3..7aa063d6a 100644 --- a/spec/lib/pact_broker/domain/webhook_spec.rb +++ b/spec/lib/pact_broker/domain/webhook_spec.rb @@ -20,8 +20,8 @@ module Domain subject(:webhook) { Webhook.new(request: request_template, consumer: consumer, provider: provider) } - describe "description" do - subject { webhook.description } + describe "scope_description" do + subject { webhook.scope_description } context "with a consumer and provider" do it { is_expected.to eq "A webhook for the pact between Consumer and Provider" } diff --git a/spec/lib/pact_broker/webhooks/service_spec.rb b/spec/lib/pact_broker/webhooks/service_spec.rb index 44344fe15..1426bf266 100644 --- a/spec/lib/pact_broker/webhooks/service_spec.rb +++ b/spec/lib/pact_broker/webhooks/service_spec.rb @@ -71,7 +71,7 @@ module Webhooks end it "logs that no webhook was found" do - expect(logger).to receive(:debug).with(/No webhook found/) + expect(logger).to receive(:debug).with(/No enabled webhooks found/) subject end end