Skip to content

Commit

Permalink
feat: add ${pactbroker.verificationResultUrl} to webhook templates
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 9, 2018
1 parent a4b69db commit e19c9c9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/pact_broker/doc/views/webhooks.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/domain/pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
5 changes: 5 additions & 0 deletions lib/pact_broker/pacts/pact_publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/pact_broker/pacts/pact_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions lib/pact_broker/webhooks/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 15 additions & 6 deletions spec/lib/pact_broker/webhooks/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e19c9c9

Please sign in to comment.