Skip to content

Commit e19c9c9

Browse files
committed
feat: add ${pactbroker.verificationResultUrl} to webhook templates
1 parent a4b69db commit e19c9c9

File tree

6 files changed

+48
-15
lines changed

6 files changed

+48
-15
lines changed

lib/pact_broker/doc/views/webhooks.markdown

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ Pact Broker Github repository.
8686

8787
The following variables may be used in the request parameters or body, and will be replaced with their appropriate values at runtime.
8888

89-
`${pactbroker.pactUrl}`: the "permalink" URL to the newly published pact (the URL specifying the consumer version URL, rather than the "/latest" format.)
9089
`${pactbroker.consumerName}`: the consumer name
9190
`${pactbroker.providerName}`: the provider name
9291
`${pactbroker.consumerVersionNumber}`: the version number of the most recent consumer version associated with the pact content.
9392
`${pactbroker.providerVersionNumber}`: the provider version number for the verification result
9493
`${pactbroker.githubVerificationStatus}`: the verification status using the correct keywords for posting to the the [Github commit status API](https://developer.github.com/v3/repos/statuses).
94+
`${pactbroker.pactUrl}`: the "permalink" URL to the newly published pact (the URL specifying the consumer version URL, rather than the "/latest" format.)
95+
`${pactbroker.verificationResultUrl}`: the URL to the relevant verification result.
9596

9697
Example usage:
9798

lib/pact_broker/domain/pact.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module PactBroker
66
module Domain
77
class Pact
88

9-
attr_accessor :id, :provider, :consumer_version, :consumer, :created_at, :json_content, :consumer_version_number, :revision_number, :pact_version_sha
9+
attr_accessor :id, :provider, :consumer_version, :consumer, :created_at, :json_content, :consumer_version_number, :revision_number, :pact_version_sha, :latest_verification
1010

1111
def initialize attributes
1212
attributes.each_pair do | key, value |

lib/pact_broker/pacts/pact_publication.rb

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def latest_tag_names
2626
LatestTaggedPactPublications.where(id: id).select(:tag_name).collect{|t| t[:tag_name]}
2727
end
2828

29+
def latest_verification
30+
pact_version.latest_verification
31+
end
32+
2933
def to_domain
3034
PactBroker::Domain::Pact.new(
3135
id: id,
@@ -36,6 +40,7 @@ def to_domain
3640
revision_number: revision_number,
3741
json_content: pact_version.content,
3842
pact_version_sha: pact_version.sha,
43+
latest_verification: latest_verification,
3944
created_at: created_at
4045
)
4146
end

lib/pact_broker/pacts/pact_version.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
module PactBroker
44
module Pacts
55
class PactVersion < Sequel::Model(:pact_versions)
6-
one_to_many :pact_publications, :reciprocal => :pact_version
6+
one_to_many :pact_publications, reciprocal: :pact_version
7+
one_to_many :verifications, reciprocal: :verification, order: :id, :class => "PactBroker::Domain::Verification"
78

89
def name
910
"Pact between #{consumer_name} and #{provider_name}"
@@ -31,6 +32,10 @@ def latest_pact_publication
3132
.last
3233
end
3334

35+
def latest_verification
36+
verifications.last
37+
end
38+
3439
def consumer_versions
3540
PactBroker::Domain::Version.where(id: PactBroker::Pacts::PactPublication.select(:consumer_version_id).where(pact_version_id: id)).order(:order)
3641
end

lib/pact_broker/webhooks/render.rb

+19-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ class Render
77
def self.call(template, pact, verification = nil, &escaper)
88
base_url = PactBroker.configuration.base_url
99
params = {
10-
'${pactbroker.pactUrl}' => PactBroker::Api::PactBrokerUrls.pact_url(base_url, pact),
11-
'${pactbroker.consumerVersionNumber}' => pact.consumer_version_number,
10+
'${pactbroker.pactUrl}' => pact ? PactBroker::Api::PactBrokerUrls.pact_url(base_url, pact) : "",
11+
'${pactbroker.verificationResultUrl}' => verification_url(pact, verification),
12+
'${pactbroker.consumerVersionNumber}' => pact ? pact.consumer_version_number : "",
1213
'${pactbroker.providerVersionNumber}' => verification ? verification.provider_version_number : "",
13-
'${pactbroker.consumerName}' => pact.consumer_name,
14-
'${pactbroker.providerName}' => pact.provider_name,
15-
'${pactbroker.githubVerificationStatus}' => github_verification_status(verification)
14+
'${pactbroker.consumerName}' => pact ? pact.consumer_name : "",
15+
'${pactbroker.providerName}' => pact ? pact.provider_name : "",
16+
'${pactbroker.githubVerificationStatus}' => github_verification_status(pact, verification)
1617
}
1718

1819
if escaper
@@ -26,9 +27,21 @@ def self.call(template, pact, verification = nil, &escaper)
2627
end
2728
end
2829

29-
def self.github_verification_status verification
30+
def self.github_verification_status pact, verification
3031
if verification
3132
verification.success ? "success" : "failure"
33+
elsif pact && pact.latest_verification
34+
pact.latest_verification.success ? "success" : "failure"
35+
elsif pact
36+
"pending"
37+
else
38+
""
39+
end
40+
end
41+
42+
def self.verification_url pact, verification
43+
if verification || (pact && pact.latest_verification)
44+
PactBroker::Api::PactBrokerUrls.verification_url(verification || pact.latest_verification, PactBroker.configuration.base_url)
3245
else
3346
""
3447
end

spec/lib/pact_broker/webhooks/render_spec.rb

+15-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ module Webhooks
77
describe "#call" do
88
before do
99
allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).and_return("http://foo")
10+
allow(PactBroker::Api::PactBrokerUrls).to receive(:verification_url) do | verification, base_url |
11+
expect(verification).to_not be nil
12+
"http://verification"
13+
end
1014
end
1115

1216
let(:pact) do
@@ -56,15 +60,20 @@ module Webhooks
5660
["${pactbroker.githubVerificationStatus}", "", :nil_pact, :nil_verification],
5761
["${pactbroker.githubVerificationStatus}", "pending", :pact_with_no_verification, :nil_verification],
5862
["${pactbroker.githubVerificationStatus}", "success", :pact_with_successful_verification, :nil_verification],
59-
["${pactbroker.githubVerificationStatus}", "failure", :pact_with_failed_verification, :nil_verification]
63+
["${pactbroker.githubVerificationStatus}", "failure", :pact_with_failed_verification, :nil_verification],
64+
["${pactbroker.verificationResultUrl}", "", :pact_with_no_verification, :nil_verification],
65+
["${pactbroker.verificationResultUrl}", "http://verification", :pact_with_successful_verification, :nil_verification],
66+
["${pactbroker.verificationResultUrl}", "http://verification", :pact_with_successful_verification, :verification],
6067
]
6168

6269
TEST_CASES.each do | (template, expected_output, pact_var_name, verification_var_name) |
63-
it "replaces #{template} with #{expected_output.inspect}" do
64-
the_pact = send(pact_var_name)
65-
the_verification = send(verification_var_name)
66-
output = Render.call(template, the_pact, the_verification)
67-
expect(output).to eq expected_output
70+
context "with #{pact_var_name} and #{verification_var_name}" do
71+
it "replaces #{template} with #{expected_output.inspect}" do
72+
the_pact = send(pact_var_name)
73+
the_verification = send(verification_var_name)
74+
output = Render.call(template, the_pact, the_verification)
75+
expect(output).to eq expected_output
76+
end
6877
end
6978
end
7079

0 commit comments

Comments
 (0)