Skip to content

Commit abccf7a

Browse files
committed
feat: add ${pactbroker.githubVerificationStatus} to webhook templates
1 parent db2f9d1 commit abccf7a

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

lib/pact_broker/doc/views/webhooks.markdown

+3
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ Pact Broker Github repository.
8787
The following variables may be used in the request parameters or body, and will be replaced with their appropriate values at runtime.
8888

8989
`${pactbroker.pactUrl}`: the "permalink" URL to the newly published pact (the URL specifying the consumer version URL, rather than the "/latest" format.)
90+
`${pactbroker.consumerName}`: the consumer name
91+
`${pactbroker.providerName}`: the provider name
9092
`${pactbroker.consumerVersionNumber}`: the version number of the most recent consumer version associated with the pact content.
9193
`${pactbroker.providerVersionNumber}`: the provider version number for the verification result
94+
`${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).
9295

9396
Example usage:
9497

lib/pact_broker/webhooks/render.rb

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
module PactBroker
22
module Webhooks
33
class Render
4-
def self.call(body, pact, verification = nil, &escaper)
4+
def self.call(template, pact, verification = nil, &escaper)
55
base_url = PactBroker.configuration.base_url
66
params = {
77
'${pactbroker.pactUrl}' => PactBroker::Api::PactBrokerUrls.pact_url(base_url, pact),
88
'${pactbroker.consumerVersionNumber}' => pact.consumer_version_number,
9-
'${pactbroker.providerVersionNumber}' => verification ? verification.provider_version_number : ""
9+
'${pactbroker.providerVersionNumber}' => verification ? verification.provider_version_number : "",
10+
'${pactbroker.consumerName}' => pact.consumer_name,
11+
'${pactbroker.providerName}' => pact.provider_name,
12+
'${pactbroker.githubVerificationStatus}' => github_verification_status(verification)
1013
}
1114

1215
if escaper
@@ -15,8 +18,16 @@ def self.call(body, pact, verification = nil, &escaper)
1518
end
1619
end
1720

18-
params.inject(body) do | body, (key, value) |
19-
body.gsub(key, value)
21+
params.inject(template) do | template, (key, value) |
22+
template.gsub(key, value)
23+
end
24+
end
25+
26+
def self.github_verification_status verification
27+
if verification
28+
verification.success ? "success" : "failure"
29+
else
30+
""
2031
end
2132
end
2233
end

spec/lib/pact_broker/webhooks/render_spec.rb

+29-18
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,54 @@ module Webhooks
99
allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).and_return("http://foo")
1010
end
1111

12-
let(:body) do
13-
"Foo ${pactbroker.pactUrl} ${pactbroker.consumerVersionNumber} ${pactbroker.providerVersionNumber}"
14-
end
15-
1612
let(:pact) do
17-
instance_double("pact", consumer_version_number: "1.2.3+foo")
13+
instance_double("pact", consumer_version_number: "1.2.3+foo", consumer_name: "Foo", provider_name: "Bar")
1814
end
1915

2016
let(:verification) do
21-
instance_double("verification", provider_version_number: "3")
17+
instance_double("verification", provider_version_number: "3", success: true)
2218
end
2319

24-
subject { Render.call(body, pact, verification) }
25-
26-
it { is_expected.to eq "Foo http://foo 1.2.3+foo 3" }
20+
let(:failed_verification) do
21+
instance_double("verification", provider_version_number: "3", success: false)
22+
end
2723

24+
let(:nil_verification) { nil }
2825

29-
context "when the verification is nil" do
30-
let(:verification) { nil }
26+
subject { Render.call(template, pact, verification) }
3127

32-
let(:body) do
33-
"${pactbroker.providerVersionNumber}"
34-
end
28+
TEST_CASES = [
29+
["${pactbroker.pactUrl}", "http://foo", :pact, :verification],
30+
["${pactbroker.consumerVersionNumber}", "1.2.3+foo", :pact, :verification],
31+
["${pactbroker.providerVersionNumber}", "3", :pact, :verification],
32+
["${pactbroker.providerVersionNumber}", "", :pact, :nil_verification],
33+
["${pactbroker.consumerName}", "Foo", :pact, :verification],
34+
["${pactbroker.providerName}", "Bar", :pact, :verification],
35+
["${pactbroker.githubVerificationStatus}", "success", :pact, :verification],
36+
["${pactbroker.githubVerificationStatus}", "failure", :pact, :failed_verification],
37+
["${pactbroker.githubVerificationStatus}", "", :pact, :nil_verification]
38+
]
3539

36-
it "inserts an empty string" do
37-
expect(subject).to eq ""
40+
TEST_CASES.each do | (template, expected_output, pact_var_name, verification_var_name) |
41+
it "replaces #{template} with #{expected_output.inspect}" do
42+
the_pact = send(pact_var_name)
43+
the_verification = send(verification_var_name)
44+
output = Render.call(template, the_pact, the_verification)
45+
expect(output).to eq expected_output
3846
end
3947
end
4048

4149
context "with an escaper" do
4250
subject do
43-
Render.call(body, pact, verification) do | value |
51+
Render.call(template, pact, verification) do | value |
4452
CGI.escape(value)
4553
end
4654
end
55+
let(:template) do
56+
"${pactbroker.pactUrl}"
57+
end
4758

48-
it { is_expected.to eq "Foo http%3A%2F%2Ffoo 1.2.3%2Bfoo 3" }
59+
it { is_expected.to eq "http%3A%2F%2Ffoo" }
4960
end
5061
end
5162
end

0 commit comments

Comments
 (0)