Skip to content

Commit 35f8bfc

Browse files
committed
fix: show consumer/provider/global webhooks in webhook column on index page
Fixes: pact-foundation#234
1 parent d37f0eb commit 35f8bfc

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

lib/pact_broker/matrix/head_row.rb

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'pact_broker/matrix/row'
2+
require 'pact_broker/webhooks/webhook'
23

34
module PactBroker
45
module Matrix
@@ -29,6 +30,25 @@ class HeadRow < Row
2930
end
3031
end
3132
end)
33+
34+
# When viewing the index, every webhook in the database will match at least one of the rows, so
35+
# it makes sense to load the entire table and match each webhook to the appropriate row.
36+
# This will only work when using eager loading. The keys are just blanked out to avoid errors.
37+
# I don't understand how they work at all.
38+
# It would be nice to do this declaratively.
39+
many_to_many :webhooks, :left_key => [], left_primary_key: [], :eager_loader=>(proc do |eo_opts|
40+
eo_opts[:rows].each do |row|
41+
row.associations[:webhooks] = []
42+
end
43+
44+
PactBroker::Webhooks::Webhook.each do | webhook |
45+
eo_opts[:rows].each do | row |
46+
if webhook.is_for?(row)
47+
row.associations[:webhooks] << webhook
48+
end
49+
end
50+
end
51+
end)
3252
end
3353
end
3454
end

lib/pact_broker/matrix/row.rb

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class Row < Sequel::Model(:matrix)
1414
TP_COLS = [ :consumer_version_number, :pact_revision_number, :provider_version_number, :verification_number]
1515

1616
associate(:one_to_many, :latest_triggered_webhooks, :class => "PactBroker::Webhooks::LatestTriggeredWebhook", primary_key: :pact_publication_id, key: :pact_publication_id)
17-
associate(:one_to_many, :webhooks, :class => "PactBroker::Webhooks::Webhook", primary_key: [:consumer_id, :provider_id], key: [:consumer_id, :provider_id])
1817
associate(:one_to_many, :consumer_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :consumer_version_id, key: :version_id)
1918
associate(:one_to_many, :provider_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :provider_version_id, key: :version_id)
2019

lib/pact_broker/webhooks/webhook.rb

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def parsed_body
7171
end
7272
end
7373

74+
def is_for? relationship
75+
(consumer_id == relationship.consumer_id || !consumer_id) && (provider_id == relationship.provider_id || !provider_id)
76+
end
77+
7478
private
7579

7680
def self.properties_hash_from_domain webhook

spec/lib/pact_broker/matrix/head_row_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ module Matrix
55
describe HeadRow do
66
let(:td) { TestDataBuilder.new }
77

8+
describe "webhooks" do
9+
let(:td) { TestDataBuilder.new }
10+
11+
before do
12+
td.create_consumer("Foo")
13+
.create_provider("Bar")
14+
.create_consumer_version
15+
.create_pact
16+
.create_global_webhook
17+
.create_consumer_webhook
18+
.create_provider_webhook
19+
.create_provider("Wiffle")
20+
.create_provider_webhook
21+
end
22+
23+
let(:row) { HeadRow.where(consumer_name: "Foo", provider_name: "Bar").single_record }
24+
25+
it "returns all the webhooks" do
26+
rows = HeadRow.eager(:webhooks).all
27+
expect(rows.first.webhooks.count).to eq 3
28+
end
29+
end
30+
831
describe "latest_verification_for_consumer_version_tag" do
932
context "when the pact with a given tag has been verified" do
1033
before do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'pact_broker/webhooks/webhook'
2+
3+
module PactBroker
4+
module Webhooks
5+
describe Webhook do
6+
7+
let(:td) { TestDataBuilder.new }
8+
9+
before do
10+
td.create_consumer("Foo")
11+
.create_provider("Bar")
12+
.create_consumer_version
13+
.create_pact
14+
.create_global_webhook
15+
.create_consumer_webhook
16+
.create_provider_webhook
17+
.create_provider("Wiffle")
18+
.create_provider_webhook
19+
end
20+
21+
let(:consumer) { PactBroker::Domain::Pacticipant.find(name: "Foo") }
22+
let(:provider) { PactBroker::Domain::Pacticipant.find(name: "Bar") }
23+
let(:pact) { double(consumer_id: consumer.id, provider_id: provider.id).as_null_object }
24+
25+
describe "#is_for?" do
26+
let(:matching_webhook_uuids) { Webhooks::Repository.new.find_by_consumer_and_or_provider(consumer, provider).collect(&:uuid) }
27+
let(:matching_webhooks) { Webhooks::Webhook.where(uuid: matching_webhook_uuids) }
28+
let(:non_matching_webhooks) { Webhooks::Webhook.exclude(uuid: matching_webhook_uuids) }
29+
30+
it "matches the implementation of Webhook::Repository#find_by_consumer_and_or_provider" do
31+
expect(matching_webhooks.count).to be > 0
32+
expect(non_matching_webhooks.count).to be > 0
33+
expect(matching_webhooks.all?{|w| w.is_for?(pact)}).to be true
34+
expect(non_matching_webhooks.all?{|w| !w.is_for?(pact)}).to be true
35+
end
36+
end
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)