Skip to content

Commit 9321c73

Browse files
committed
feat(consumer or provider webhooks): refactor webhooks resource classes and add consumer and provider webhook links to pact resource
1 parent 1b8c0e2 commit 9321c73

File tree

8 files changed

+121
-21
lines changed

8 files changed

+121
-21
lines changed

lib/pact_broker/api.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ module PactBroker
5555
add ['pacticipants', :pacticipant_name, 'labels', :label_name], Api::Resources::Label, {resource_name: "pacticipant_label"}
5656

5757
# Webhooks
58-
add ['webhooks', 'provider', :provider_name, 'consumer', :consumer_name ], Api::Resources::PactWebhooks, {resource_name: "consumer_and_provider_webhooks"}
59-
add ['webhooks', 'provider', :provider_name], Api::Resources::PactWebhooks, {resource_name: "provider_webhooks"}
60-
add ['webhooks', 'consumer', :consumer_name], Api::Resources::PactWebhooks, {resource_name: "consumer_webhooks"}
58+
add ['webhooks', 'provider', :provider_name, 'consumer', :consumer_name ], Api::Resources::Webhooks, {resource_name: "consumer_and_provider_webhooks"}
59+
add ['webhooks', 'provider', :provider_name], Api::Resources::Webhooks, {resource_name: "provider_webhooks"}
60+
add ['webhooks', 'consumer', :consumer_name], Api::Resources::Webhooks, {resource_name: "consumer_webhooks"}
6161
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'webhooks'], Api::Resources::PactWebhooks, {resource_name: "pact_webhooks"}
6262
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'webhooks', 'status'], Api::Resources::PactWebhooksStatus, {resource_name: "pact_webhooks_status"}
6363

lib/pact_broker/api/decorators/pact_decorator.rb

+14
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ def to_hash(options = {})
106106
}
107107
end
108108

109+
link :'pb:consumer-webhooks' do | options |
110+
{
111+
title: "Webhooks for all pacts with consumer #{represented.consumer.name}",
112+
href: consumer_webhooks_url(represented.consumer, options.fetch(:base_url))
113+
}
114+
end
115+
116+
link :'pb:consumer-webhooks' do | options |
117+
{
118+
title: "Webhooks for all pacts with provider #{represented.provider.name}",
119+
href: consumer_webhooks_url(represented.provider, options.fetch(:base_url))
120+
}
121+
end
122+
109123
link :'pb:tag-prod-version' do | options |
110124
{
111125
title: "PUT to this resource to tag this consumer version as 'production'",

lib/pact_broker/api/decorators/pact_webhooks_status_decorator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class PactWebhooksStatusDecorator < BaseDecorator
6363
link :'pb:pact-webhooks' do | context |
6464
{
6565
title: "Webhooks for the pact between #{context[:consumer_name]} and #{context[:provider_name]}",
66-
href: webhooks_for_consumer_and_provider_url(fake_consumer(context), fake_provider(context), context.fetch(:base_url))
66+
href: webhooks_for_pact_url(fake_consumer(context), fake_provider(context), context.fetch(:base_url))
6767
}
6868
end
6969

lib/pact_broker/api/pact_broker_urls.rb

+8
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ def webhooks_for_consumer_and_provider_url consumer, provider, base_url = ''
156156
"#{base_url}/webhooks/provider/#{url_encode(provider.name)}/consumer/#{url_encode(consumer.name)}"
157157
end
158158

159+
def consumer_webhooks_url consumer, base_url = ''
160+
"#{base_url}/webhooks/consumer/#{url_encode(consumer.name)}"
161+
end
162+
163+
def provider_webhooks_url provider, base_url = ''
164+
"#{base_url}/webhooks/provider/#{url_encode(provider.name)}"
165+
end
166+
159167
def webhooks_for_pact_url consumer, provider, base_url = ''
160168
"#{base_url}/pacts/provider/#{url_encode(provider.name)}/consumer/#{url_encode(consumer.name)}/webhooks"
161169
end

lib/pact_broker/api/resources/all_webhooks.rb

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
module PactBroker
66
module Api
77
module Resources
8-
98
class AllWebhooks < BaseResource
109

1110
def content_types_provided
@@ -23,7 +22,6 @@ def to_json
2322
def webhooks
2423
webhook_service.find_all
2524
end
26-
2725
end
2826
end
2927
end

lib/pact_broker/api/resources/pact_webhooks.rb

-13
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,6 @@ def next_uuid
7676
@next_uuid ||= webhook_service.next_uuid
7777
end
7878

79-
def consumer
80-
@consumer ||= identifier_from_path[:consumer_name] && find_pacticipant(identifier_from_path[:consumer_name], "consumer")
81-
end
82-
83-
def provider
84-
@provider ||= identifier_from_path[:provider_name] && find_pacticipant(identifier_from_path[:provider_name], "provider")
85-
end
86-
87-
def find_pacticipant name, role
88-
pacticipant_service.find_pacticipant_by_name(name).tap do | pacticipant |
89-
set_json_error_message("No #{role} with name '#{name}' found") if pacticipant.nil?
90-
end
91-
end
9279

9380
end
9481
end
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
require 'pact_broker/api/resources/base_resource'
2+
require 'pact_broker/api/decorators/webhook_decorator'
3+
require 'pact_broker/api/decorators/webhooks_decorator'
4+
require 'pact_broker/api/contracts/webhook_contract'
5+
6+
module PactBroker
7+
module Api
8+
module Resources
9+
class Webhooks < BaseResource
10+
11+
def allowed_methods
12+
["POST", "GET"]
13+
end
14+
15+
def content_types_provided
16+
[["application/hal+json", :to_json]]
17+
end
18+
19+
def content_types_accepted
20+
[["application/json", :from_json]]
21+
end
22+
23+
def resource_exists?
24+
(identifier_from_path[:consumer_name].nil? || consumer) &&
25+
(identifier_from_path[:provider_name].nil? || provider)
26+
end
27+
28+
def malformed_request?
29+
if request.post?
30+
return invalid_json? || validation_errors?(webhook)
31+
end
32+
false
33+
end
34+
35+
def validation_errors? webhook
36+
errors = webhook_service.errors(webhook)
37+
38+
unless errors.empty?
39+
response.headers['Content-Type'] = 'application/json;charset=utf-8'
40+
response.body = { errors: errors.messages }.to_json
41+
end
42+
43+
!errors.empty?
44+
end
45+
46+
def create_path
47+
webhook_url next_uuid, base_url
48+
end
49+
50+
def post_is_create?
51+
true
52+
end
53+
54+
def from_json
55+
saved_webhook = webhook_service.create next_uuid, webhook, consumer, provider
56+
response.body = Decorators::WebhookDecorator.new(saved_webhook).to_json(user_options: { base_url: base_url })
57+
end
58+
59+
def to_json
60+
Decorators::WebhooksDecorator.new(webhooks).to_json(user_options: decorator_context(resource_title: 'Pact webhooks'))
61+
end
62+
63+
private
64+
65+
def webhooks
66+
webhook_service.find_by_consumer_and_provider consumer, provider
67+
end
68+
69+
def webhook
70+
@webhook ||= Decorators::WebhookDecorator.new(PactBroker::Domain::Webhook.new).from_json(request_body)
71+
end
72+
73+
def next_uuid
74+
@next_uuid ||= webhook_service.next_uuid
75+
end
76+
77+
def consumer
78+
@consumer ||= identifier_from_path[:consumer_name] && find_pacticipant(identifier_from_path[:consumer_name], "consumer")
79+
end
80+
81+
def provider
82+
@provider ||= identifier_from_path[:provider_name] && find_pacticipant(identifier_from_path[:provider_name], "provider")
83+
end
84+
85+
def find_pacticipant name, role
86+
pacticipant_service.find_pacticipant_by_name(name).tap do | pacticipant |
87+
set_json_error_message("No #{role} with name '#{name}' found") if pacticipant.nil?
88+
end
89+
end
90+
end
91+
end
92+
end
93+
end

spec/lib/pact_broker/api/resources/pact_webhooks_spec.rb spec/lib/pact_broker/api/resources/webhooks_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
require 'pact_broker/api/resources/pact_webhooks'
1+
require 'pact_broker/api/resources/webhooks'
22

33
module PactBroker::Api
44
module Resources
5-
describe PactWebhooks do
5+
describe Webhooks do
66

77
let(:webhook_service) { PactBroker::Webhooks::Service }
88
let(:uuid) { '1483234k24DKFGJ45K' }

0 commit comments

Comments
 (0)