diff --git a/db/migrations/20190523_add_enabled_column_to_webhooks.rb b/db/migrations/20190523_add_enabled_column_to_webhooks.rb new file mode 100644 index 000000000..6f616857d --- /dev/null +++ b/db/migrations/20190523_add_enabled_column_to_webhooks.rb @@ -0,0 +1,5 @@ +Sequel.migration do + change do + add_column(:webhooks, :enabled, TrueClass, default: true) + end +end diff --git a/lib/pact_broker/api/decorators/webhook_decorator.rb b/lib/pact_broker/api/decorators/webhook_decorator.rb index 10e31b38b..96969e6aa 100644 --- a/lib/pact_broker/api/decorators/webhook_decorator.rb +++ b/lib/pact_broker/api/decorators/webhook_decorator.rb @@ -24,6 +24,8 @@ class WebhookEventDecorator < BaseDecorator property :name end + property :enabled, default: true + property :request, :class => PactBroker::Webhooks::WebhookRequestTemplate, extend: WebhookRequestTemplateDecorator collection :events, :class => PactBroker::Webhooks::WebhookEvent, extend: WebhookEventDecorator diff --git a/lib/pact_broker/domain/webhook.rb b/lib/pact_broker/domain/webhook.rb index e8676c764..8904b2dc4 100644 --- a/lib/pact_broker/domain/webhook.rb +++ b/lib/pact_broker/domain/webhook.rb @@ -10,7 +10,7 @@ class Webhook include Messages include Logging - attr_accessor :uuid, :consumer, :provider, :request, :created_at, :updated_at, :events + attr_accessor :uuid, :consumer, :provider, :request, :created_at, :updated_at, :events, :enabled attr_reader :attributes def initialize attributes = {} @@ -20,6 +20,7 @@ def initialize attributes = {} @consumer = attributes[:consumer] @provider = attributes[:provider] @events = attributes[:events] + @enabled = attributes[:enabled] @created_at = attributes[:created_at] @updated_at = attributes[:updated_at] end diff --git a/lib/pact_broker/webhooks/webhook.rb b/lib/pact_broker/webhooks/webhook.rb index 9a8435bd2..4c5e13323 100644 --- a/lib/pact_broker/webhooks/webhook.rb +++ b/lib/pact_broker/webhooks/webhook.rb @@ -46,6 +46,7 @@ def to_domain provider: provider, events: events, request: Webhooks::WebhookRequestTemplate.new(request_attributes), + enabled: enabled, created_at: created_at, updated_at: updated_at) end @@ -85,6 +86,7 @@ def self.properties_hash_from_domain webhook url: webhook.request.url, username: webhook.request.username, password: not_plain_text_password(webhook.request.password), + enabled: webhook.enabled, body: (is_json_request_body ? webhook.request.body.to_json : webhook.request.body), is_json_request_body: is_json_request_body } diff --git a/spec/features/create_webhook_spec.rb b/spec/features/create_webhook_spec.rb index 74c146451..456710f81 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 { + enabled: false, events: [{ name: 'something_happened' }], diff --git a/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb b/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb index 7ee5c94b1..6be4dc993 100644 --- a/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb +++ b/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb @@ -298,6 +298,18 @@ def valid_webhook_with expect(subject.errors[:"request.url"]).to eq ["cannot have a template parameter in the host"] end end + + context "when enabled is not a boolean", pending: "I can't work out why this doesn't work" do + let(:json) do + valid_webhook_with do |hash| + hash['enabled'] = 'foo' + end + end + + it "contains an error" do + expect(subject.errors[:enabled]).to eq ["cannot have a template parameter in the host"] + end + end end end end 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 3abdd3f43..fd755eec6 100644 --- a/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb @@ -33,7 +33,8 @@ module Decorators provider: provider, events: [event], created_at: created_at, - updated_at: updated_at + updated_at: updated_at, + enabled: false ) end @@ -90,6 +91,10 @@ module Decorators expect(parsed_json[:updatedAt]).to eq FormatDateTime.call(updated_at) end + it 'includes the enabled flag' do + expect(parsed_json[:enabled]).to eq false + end + context 'when the headers are empty' do let(:headers) { nil } it 'does not include the headers' do