Skip to content

Commit

Permalink
feat: add ${pactbroker.consumerVersionTags} and ${pactbroker.provider…
Browse files Browse the repository at this point in the history
…VersionTags} to webhook templates
  • Loading branch information
bethesque committed Jun 10, 2018
1 parent 32f7efe commit e5121b1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/pact_broker/doc/views/webhooks.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ The following variables may be used in the request parameters or body, and will
`${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.consumerVersionTags}`: the list of tag names for the most recent consumer version associated with the pact content, separated by ", ".
`${pactbroker.providerVersionTags}`: the list of tag names for the provider version associated with the verification result, separated by ", ".
`${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.
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/domain/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Version < Sequel::Model
set_primary_key :id
one_to_many :pact_publications, order: :revision_number, class: "PactBroker::Pacts::PactPublication", key: :consumer_version_id
associate(:many_to_one, :pacticipant, :class => "PactBroker::Domain::Pacticipant", :key => :pacticipant_id, :primary_key => :id)
one_to_many :tags, :reciprocal => :version
one_to_many :tags, :reciprocal => :version, order: :created_at

dataset_module do
include PactBroker::Repositories::Helpers
Expand Down
35 changes: 25 additions & 10 deletions lib/pact_broker/webhooks/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ class Render

TEMPLATE_PARAMETER_REGEXP = /\$\{pactbroker\.[^\}]+\}/

def self.call(template, pact, verification = nil, &escaper)
def self.call(template, pact, trigger_verification = nil, &escaper)
base_url = PactBroker.configuration.base_url
verification = trigger_verification || (pact && pact.latest_verification)
params = {
'${pactbroker.pactUrl}' => pact ? PactBroker::Api::PactBrokerUrls.pact_url(base_url, pact) : "",
'${pactbroker.verificationResultUrl}' => verification_url(pact, verification),
'${pactbroker.verificationResultUrl}' => verification_url(verification),
'${pactbroker.consumerVersionNumber}' => pact ? pact.consumer_version_number : "",
'${pactbroker.providerVersionNumber}' => verification ? verification.provider_version_number : "",
'${pactbroker.providerVersionTags}' => provider_version_tags(verification),
'${pactbroker.consumerVersionTags}' => consumer_version_tags(pact),
'${pactbroker.consumerName}' => pact ? pact.consumer_name : "",
'${pactbroker.providerName}' => pact ? pact.provider_name : "",
'${pactbroker.githubVerificationStatus}' => github_verification_status(pact, verification)
'${pactbroker.githubVerificationStatus}' => github_verification_status(verification)
}

if escaper
Expand All @@ -27,21 +30,33 @@ def self.call(template, pact, verification = nil, &escaper)
end
end

def self.github_verification_status pact, verification
def self.github_verification_status verification
if verification
verification.success ? "success" : "failure"
elsif pact && pact.latest_verification
pact.latest_verification.success ? "success" : "failure"
elsif pact
else
"pending"
end
end

def self.verification_url verification
if verification
PactBroker::Api::PactBrokerUrls.verification_url(verification, PactBroker.configuration.base_url)
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)
def self.consumer_version_tags pact
if pact
pact.consumer_version.tags.collect(&:name).join(", ")
else
""
end
end

def self.provider_version_tags verification
if verification
verification.provider_version.tags.collect(&:name).join(", ")
else
""
end
Expand Down
37 changes: 31 additions & 6 deletions spec/lib/pact_broker/webhooks/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,56 @@ module Webhooks
end

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

let(:pact_with_no_verification) { pact }

let(:pact_with_successful_verification) do
instance_double("pact",
double("pact",
consumer_version: consumer_version,
consumer_version_number: "1.2.3+foo",
consumer_name: "Foo",
provider_name: "Bar",
latest_verification: verification)
end

let(:pact_with_failed_verification) do
instance_double("pact",
double("pact",
consumer_version: consumer_version,
consumer_version_number: "1.2.3+foo",
consumer_name: "Foo",
provider_name: "Bar",
latest_verification: failed_verification)
end

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

let(:failed_verification) do
instance_double("verification", provider_version_number: "3", success: false)
double("verification", provider_version_number: "3", success: false, provider_version: provider_version)
end

let(:provider_version) do
double("version", tags: provider_tags)
end

let(:consumer_version) do
double("version", tags: consumer_tags)
end

let(:provider_tags) do
[ double("tag", name: "test"), double("tag", name: "prod") ]
end

let(:consumer_tags) do
[ double("tag", name: "test") ]
end

let(:nil_pact) { nil }
Expand All @@ -57,13 +80,15 @@ module Webhooks
["${pactbroker.providerName}", "Bar", :pact, :verification],
["${pactbroker.githubVerificationStatus}", "success", :pact, :verification],
["${pactbroker.githubVerificationStatus}", "failure", :pact, :failed_verification],
["${pactbroker.githubVerificationStatus}", "", :nil_pact, :nil_verification],
["${pactbroker.githubVerificationStatus}", "pending", :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.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],
["${pactbroker.providerVersionTags}", "test, prod", :pact_with_successful_verification, :verification],
["${pactbroker.consumerVersionTags}", "test", :pact_with_successful_verification, :verification],
]

TEST_CASES.each do | (template, expected_output, pact_var_name, verification_var_name) |
Expand Down

0 comments on commit e5121b1

Please sign in to comment.