Skip to content

Commit

Permalink
feat: add ${pactbroker.githubVerificationStatus} to webhook templates
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 9, 2018
1 parent db2f9d1 commit abccf7a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
3 changes: 3 additions & 0 deletions lib/pact_broker/doc/views/webhooks.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ Pact Broker Github repository.
The following variables may be used in the request parameters or body, and will be replaced with their appropriate values at runtime.

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

Example usage:

Expand Down
19 changes: 15 additions & 4 deletions lib/pact_broker/webhooks/render.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module PactBroker
module Webhooks
class Render
def self.call(body, pact, verification = nil, &escaper)
def self.call(template, pact, verification = nil, &escaper)
base_url = PactBroker.configuration.base_url
params = {
'${pactbroker.pactUrl}' => PactBroker::Api::PactBrokerUrls.pact_url(base_url, pact),
'${pactbroker.consumerVersionNumber}' => pact.consumer_version_number,
'${pactbroker.providerVersionNumber}' => verification ? verification.provider_version_number : ""
'${pactbroker.providerVersionNumber}' => verification ? verification.provider_version_number : "",
'${pactbroker.consumerName}' => pact.consumer_name,
'${pactbroker.providerName}' => pact.provider_name,
'${pactbroker.githubVerificationStatus}' => github_verification_status(verification)
}

if escaper
Expand All @@ -15,8 +18,16 @@ def self.call(body, pact, verification = nil, &escaper)
end
end

params.inject(body) do | body, (key, value) |
body.gsub(key, value)
params.inject(template) do | template, (key, value) |
template.gsub(key, value)
end
end

def self.github_verification_status verification
if verification
verification.success ? "success" : "failure"
else
""
end
end
end
Expand Down
47 changes: 29 additions & 18 deletions spec/lib/pact_broker/webhooks/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,54 @@ module Webhooks
allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).and_return("http://foo")
end

let(:body) do
"Foo ${pactbroker.pactUrl} ${pactbroker.consumerVersionNumber} ${pactbroker.providerVersionNumber}"
end

let(:pact) do
instance_double("pact", consumer_version_number: "1.2.3+foo")
instance_double("pact", consumer_version_number: "1.2.3+foo", consumer_name: "Foo", provider_name: "Bar")
end

let(:verification) do
instance_double("verification", provider_version_number: "3")
instance_double("verification", provider_version_number: "3", success: true)
end

subject { Render.call(body, pact, verification) }

it { is_expected.to eq "Foo http://foo 1.2.3+foo 3" }
let(:failed_verification) do
instance_double("verification", provider_version_number: "3", success: false)
end

let(:nil_verification) { nil }

context "when the verification is nil" do
let(:verification) { nil }
subject { Render.call(template, pact, verification) }

let(:body) do
"${pactbroker.providerVersionNumber}"
end
TEST_CASES = [
["${pactbroker.pactUrl}", "http://foo", :pact, :verification],
["${pactbroker.consumerVersionNumber}", "1.2.3+foo", :pact, :verification],
["${pactbroker.providerVersionNumber}", "3", :pact, :verification],
["${pactbroker.providerVersionNumber}", "", :pact, :nil_verification],
["${pactbroker.consumerName}", "Foo", :pact, :verification],
["${pactbroker.providerName}", "Bar", :pact, :verification],
["${pactbroker.githubVerificationStatus}", "success", :pact, :verification],
["${pactbroker.githubVerificationStatus}", "failure", :pact, :failed_verification],
["${pactbroker.githubVerificationStatus}", "", :pact, :nil_verification]
]

it "inserts an empty string" do
expect(subject).to eq ""
TEST_CASES.each do | (template, expected_output, pact_var_name, verification_var_name) |
it "replaces #{template} with #{expected_output.inspect}" do
the_pact = send(pact_var_name)
the_verification = send(verification_var_name)
output = Render.call(template, the_pact, the_verification)
expect(output).to eq expected_output
end
end

context "with an escaper" do
subject do
Render.call(body, pact, verification) do | value |
Render.call(template, pact, verification) do | value |
CGI.escape(value)
end
end
let(:template) do
"${pactbroker.pactUrl}"
end

it { is_expected.to eq "Foo http%3A%2F%2Ffoo 1.2.3%2Bfoo 3" }
it { is_expected.to eq "http%3A%2F%2Ffoo" }
end
end
end
Expand Down

0 comments on commit abccf7a

Please sign in to comment.