Skip to content

Commit

Permalink
feat(webhooks): allow description to be edited
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 23, 2019
1 parent bbf5ad2 commit 302d70f
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 7 deletions.
5 changes: 5 additions & 0 deletions db/migrations/20190525_add_description_column_to_webhooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Sequel.migration do
change do
add_column(:webhooks, :description, String)
end
end
12 changes: 11 additions & 1 deletion lib/pact_broker/api/decorators/webhook_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/pact_broker/domain/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/pact_broker/webhooks/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions spec/features/create_webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
let(:webhook_json) { webhook_hash.to_json }
let(:webhook_hash) do
{
description: "trigger build",
enabled: false,
events: [{
name: 'something_happened'
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -27,6 +28,7 @@ module Decorators

let(:webhook) do
Domain::Webhook.new(
description: description,
request: webhook_request,
uuid: 'some-uuid',
consumer: consumer,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact_broker/domain/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/webhooks/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 302d70f

Please sign in to comment.