Skip to content

Commit 41eb25d

Browse files
committed
fix: ensure latest verification is loaded on pact object before executing webhook
This fixes the bug where the githubVerificationStatus was being calculated as 'pending' when a pact with the same content as a previously verified one was republished.
1 parent d6987a4 commit 41eb25d

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

lib/pact_broker/domain/pact.rb

+23-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
=end
77

88
module PactBroker
9+
class UnsetAttributeError < StandardError; end
10+
class UnsetAttribute; end
11+
912
module Domain
1013
class Pact
14+
1115
# The ID is the pact_publication ID
1216
attr_accessor :id,
1317
:provider,
@@ -21,7 +25,8 @@ class Pact
2125
:latest_verification,
2226
:head_tag_names
2327

24-
def initialize attributes
28+
def initialize attributes = {}
29+
@latest_verification = UnsetAttribute.new
2530
attributes.each_pair do | key, value |
2631
self.send(key.to_s + "=", value)
2732
end
@@ -47,6 +52,10 @@ def latest_consumer_version_tag_names= latest_consumer_version_tag_names
4752
@latest_consumer_version_tag_names = latest_consumer_version_tag_names
4853
end
4954

55+
def latest_verification
56+
get_attribute_if_set :latest_verification
57+
end
58+
5059
def to_s
5160
"Pact: consumer=#{consumer.name} provider=#{provider.name}"
5261
end
@@ -88,6 +97,19 @@ def pending?
8897
def pact_version
8998
db_model.pact_version
9099
end
100+
101+
# This class has various incarnations with different properties loaded.
102+
# They should probably be different classes, but for now, raise an error if
103+
# an attribute is called when it hasn't been set in the constuctor, because
104+
# returning nil when there should be an object causes bugs.
105+
def get_attribute_if_set attribute_name
106+
val = instance_variable_get("@#{attribute_name}".to_sym)
107+
if val.is_a?(UnsetAttribute)
108+
raise UnsetAttributeError.new("Attribute #{attribute_name} not set")
109+
else
110+
val
111+
end
112+
end
91113
end
92114
end
93115
end

lib/pact_broker/pacts/all_pact_publications.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ def to_domain_without_tags
9393
number: consumer_version_number,
9494
order: consumer_version_order,
9595
pacticipant: consumer,
96-
tags: nil)
97-
Domain::Pact.new(id: id,
96+
tags: nil
97+
)
98+
Domain::Pact.new(
99+
id: id,
98100
consumer: consumer,
99101
consumer_version: consumer_version,
100102
provider: provider,
@@ -103,7 +105,9 @@ def to_domain_without_tags
103105
pact_version_sha: pact_version_sha,
104106
created_at: created_at,
105107
head_tag_names: head_tag_names,
106-
db_model: self)
108+
latest_verification: pact_version.latest_verification,
109+
db_model: self
110+
)
107111
end
108112

109113
def head_tag_names

lib/pact_broker/pacts/pact_publication.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def to_domain
113113
revision_number: revision_number,
114114
json_content: pact_version.content,
115115
pact_version_sha: pact_version.sha,
116-
latest_verification: nil,
116+
latest_verification: pact_version.latest_verification,
117117
created_at: created_at,
118118
head_tag_names: [],
119119
db_model: self
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'pact_broker/domain/pact'
2+
3+
module PactBroker
4+
module Domain
5+
describe Pact do
6+
describe "latest_verification" do
7+
context "when it has been set to an object" do
8+
subject { Pact.new(latest_verification: 'verification') }
9+
10+
its(:latest_verification) { is_expected.to eq 'verification' }
11+
end
12+
13+
context "when it has been set to nil" do
14+
subject { Pact.new(latest_verification: nil) }
15+
16+
its(:latest_verification) { is_expected.to eq nil }
17+
end
18+
19+
context "when it has not been set" do
20+
it "raises an error" do
21+
expect { Pact.new.latest_verification.foo }.to raise_error UnsetAttributeError
22+
end
23+
end
24+
end
25+
end
26+
end
27+
end

spec/lib/pact_broker/pacts/pact_publication_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33
module PactBroker
44
module Pacts
55
describe PactPublication do
6+
describe "to_domain" do
7+
before do
8+
td.create_pact_with_verification("Foo", "1", "Bar", "2")
9+
end
10+
11+
subject { PactPublication.first.to_domain }
12+
13+
its(:latest_verification) { is_expected.to_not be nil }
14+
end
15+
16+
describe "to_domain_lightweight" do
17+
before do
18+
td.create_pact_with_verification("Foo", "1", "Bar", "2")
19+
end
20+
21+
subject { PactPublication.first.to_domain_lightweight }
22+
23+
it "raises an error if you try to access the latest_verification" do
24+
expect { subject.latest_verification }.to raise_error PactBroker::UnsetAttributeError
25+
end
26+
end
27+
628
describe "save and upsert" do
729
before do
830
td.create_consumer

0 commit comments

Comments
 (0)