From e19c9c90aeadb49f18c06367f46e2b11f84da3d8 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sun, 10 Jun 2018 09:43:31 +1000 Subject: [PATCH] feat: add ${pactbroker.verificationResultUrl} to webhook templates --- lib/pact_broker/doc/views/webhooks.markdown | 3 ++- lib/pact_broker/domain/pact.rb | 2 +- lib/pact_broker/pacts/pact_publication.rb | 5 ++++ lib/pact_broker/pacts/pact_version.rb | 7 +++++- lib/pact_broker/webhooks/render.rb | 25 +++++++++++++++----- spec/lib/pact_broker/webhooks/render_spec.rb | 21 +++++++++++----- 6 files changed, 48 insertions(+), 15 deletions(-) diff --git a/lib/pact_broker/doc/views/webhooks.markdown b/lib/pact_broker/doc/views/webhooks.markdown index b9a3873f4..c67405b03 100644 --- a/lib/pact_broker/doc/views/webhooks.markdown +++ b/lib/pact_broker/doc/views/webhooks.markdown @@ -86,12 +86,13 @@ 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). +`${pactbroker.pactUrl}`: the "permalink" URL to the newly published pact (the URL specifying the consumer version URL, rather than the "/latest" format.) +`${pactbroker.verificationResultUrl}`: the URL to the relevant verification result. Example usage: diff --git a/lib/pact_broker/domain/pact.rb b/lib/pact_broker/domain/pact.rb index cec84da0b..fbfff13fd 100644 --- a/lib/pact_broker/domain/pact.rb +++ b/lib/pact_broker/domain/pact.rb @@ -6,7 +6,7 @@ module PactBroker module Domain class Pact - attr_accessor :id, :provider, :consumer_version, :consumer, :created_at, :json_content, :consumer_version_number, :revision_number, :pact_version_sha + attr_accessor :id, :provider, :consumer_version, :consumer, :created_at, :json_content, :consumer_version_number, :revision_number, :pact_version_sha, :latest_verification def initialize attributes attributes.each_pair do | key, value | diff --git a/lib/pact_broker/pacts/pact_publication.rb b/lib/pact_broker/pacts/pact_publication.rb index e786f1bcf..cd3919b80 100644 --- a/lib/pact_broker/pacts/pact_publication.rb +++ b/lib/pact_broker/pacts/pact_publication.rb @@ -26,6 +26,10 @@ def latest_tag_names LatestTaggedPactPublications.where(id: id).select(:tag_name).collect{|t| t[:tag_name]} end + def latest_verification + pact_version.latest_verification + end + def to_domain PactBroker::Domain::Pact.new( id: id, @@ -36,6 +40,7 @@ def to_domain revision_number: revision_number, json_content: pact_version.content, pact_version_sha: pact_version.sha, + latest_verification: latest_verification, created_at: created_at ) end diff --git a/lib/pact_broker/pacts/pact_version.rb b/lib/pact_broker/pacts/pact_version.rb index b95d54aaa..44d90ae98 100644 --- a/lib/pact_broker/pacts/pact_version.rb +++ b/lib/pact_broker/pacts/pact_version.rb @@ -3,7 +3,8 @@ module PactBroker module Pacts class PactVersion < Sequel::Model(:pact_versions) - one_to_many :pact_publications, :reciprocal => :pact_version + one_to_many :pact_publications, reciprocal: :pact_version + one_to_many :verifications, reciprocal: :verification, order: :id, :class => "PactBroker::Domain::Verification" def name "Pact between #{consumer_name} and #{provider_name}" @@ -31,6 +32,10 @@ def latest_pact_publication .last end + def latest_verification + verifications.last + end + def consumer_versions PactBroker::Domain::Version.where(id: PactBroker::Pacts::PactPublication.select(:consumer_version_id).where(pact_version_id: id)).order(:order) end diff --git a/lib/pact_broker/webhooks/render.rb b/lib/pact_broker/webhooks/render.rb index 3b9be428a..715cb6f55 100644 --- a/lib/pact_broker/webhooks/render.rb +++ b/lib/pact_broker/webhooks/render.rb @@ -7,12 +7,13 @@ class Render 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.pactUrl}' => pact ? PactBroker::Api::PactBrokerUrls.pact_url(base_url, pact) : "", + '${pactbroker.verificationResultUrl}' => verification_url(pact, verification), + '${pactbroker.consumerVersionNumber}' => pact ? pact.consumer_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) + '${pactbroker.consumerName}' => pact ? pact.consumer_name : "", + '${pactbroker.providerName}' => pact ? pact.provider_name : "", + '${pactbroker.githubVerificationStatus}' => github_verification_status(pact, verification) } if escaper @@ -26,9 +27,21 @@ def self.call(template, pact, verification = nil, &escaper) end end - def self.github_verification_status verification + def self.github_verification_status pact, verification if verification verification.success ? "success" : "failure" + elsif pact && pact.latest_verification + pact.latest_verification.success ? "success" : "failure" + elsif pact + "pending" + else + "" + end + end + + def self.verification_url pact, verification + if verification || (pact && pact.latest_verification) + PactBroker::Api::PactBrokerUrls.verification_url(verification || pact.latest_verification, PactBroker.configuration.base_url) else "" end diff --git a/spec/lib/pact_broker/webhooks/render_spec.rb b/spec/lib/pact_broker/webhooks/render_spec.rb index cc321dcb9..1e1a38885 100644 --- a/spec/lib/pact_broker/webhooks/render_spec.rb +++ b/spec/lib/pact_broker/webhooks/render_spec.rb @@ -7,6 +7,10 @@ module Webhooks describe "#call" do before do allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).and_return("http://foo") + allow(PactBroker::Api::PactBrokerUrls).to receive(:verification_url) do | verification, base_url | + expect(verification).to_not be nil + "http://verification" + end end let(:pact) do @@ -56,15 +60,20 @@ module Webhooks ["${pactbroker.githubVerificationStatus}", "", :nil_pact, :nil_verification], ["${pactbroker.githubVerificationStatus}", "pending", :pact_with_no_verification, :nil_verification], ["${pactbroker.githubVerificationStatus}", "success", :pact_with_successful_verification, :nil_verification], - ["${pactbroker.githubVerificationStatus}", "failure", :pact_with_failed_verification, :nil_verification] + ["${pactbroker.githubVerificationStatus}", "failure", :pact_with_failed_verification, :nil_verification], + ["${pactbroker.verificationResultUrl}", "", :pact_with_no_verification, :nil_verification], + ["${pactbroker.verificationResultUrl}", "http://verification", :pact_with_successful_verification, :nil_verification], + ["${pactbroker.verificationResultUrl}", "http://verification", :pact_with_successful_verification, :verification], ] 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 + context "with #{pact_var_name} and #{verification_var_name}" do + 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 end