diff --git a/lib/pact_broker/doc/views/webhooks.markdown b/lib/pact_broker/doc/views/webhooks.markdown index 67901fcbc..b9a3873f4 100644 --- a/lib/pact_broker/doc/views/webhooks.markdown +++ b/lib/pact_broker/doc/views/webhooks.markdown @@ -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: diff --git a/lib/pact_broker/webhooks/render.rb b/lib/pact_broker/webhooks/render.rb index 184ad90b5..52e74cf2c 100644 --- a/lib/pact_broker/webhooks/render.rb +++ b/lib/pact_broker/webhooks/render.rb @@ -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 @@ -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 diff --git a/spec/lib/pact_broker/webhooks/render_spec.rb b/spec/lib/pact_broker/webhooks/render_spec.rb index c70d879c0..a11d06690 100644 --- a/spec/lib/pact_broker/webhooks/render_spec.rb +++ b/spec/lib/pact_broker/webhooks/render_spec.rb @@ -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