Skip to content

Commit e5121b1

Browse files
committed
feat: add ${pactbroker.consumerVersionTags} and ${pactbroker.providerVersionTags} to webhook templates
1 parent 32f7efe commit e5121b1

File tree

4 files changed

+59
-17
lines changed

4 files changed

+59
-17
lines changed

lib/pact_broker/doc/views/webhooks.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ The following variables may be used in the request parameters or body, and will
9090
`${pactbroker.providerName}`: the provider name
9191
`${pactbroker.consumerVersionNumber}`: the version number of the most recent consumer version associated with the pact content.
9292
`${pactbroker.providerVersionNumber}`: the provider version number for the verification result
93+
`${pactbroker.consumerVersionTags}`: the list of tag names for the most recent consumer version associated with the pact content, separated by ", ".
94+
`${pactbroker.providerVersionTags}`: the list of tag names for the provider version associated with the verification result, separated by ", ".
9395
`${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).
9496
`${pactbroker.pactUrl}`: the "permalink" URL to the newly published pact (the URL specifying the consumer version URL, rather than the "/latest" format.)
9597
`${pactbroker.verificationResultUrl}`: the URL to the relevant verification result.

lib/pact_broker/domain/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Version < Sequel::Model
1111
set_primary_key :id
1212
one_to_many :pact_publications, order: :revision_number, class: "PactBroker::Pacts::PactPublication", key: :consumer_version_id
1313
associate(:many_to_one, :pacticipant, :class => "PactBroker::Domain::Pacticipant", :key => :pacticipant_id, :primary_key => :id)
14-
one_to_many :tags, :reciprocal => :version
14+
one_to_many :tags, :reciprocal => :version, order: :created_at
1515

1616
dataset_module do
1717
include PactBroker::Repositories::Helpers

lib/pact_broker/webhooks/render.rb

+25-10
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ class Render
44

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

7-
def self.call(template, pact, verification = nil, &escaper)
7+
def self.call(template, pact, trigger_verification = nil, &escaper)
88
base_url = PactBroker.configuration.base_url
9+
verification = trigger_verification || (pact && pact.latest_verification)
910
params = {
1011
'${pactbroker.pactUrl}' => pact ? PactBroker::Api::PactBrokerUrls.pact_url(base_url, pact) : "",
11-
'${pactbroker.verificationResultUrl}' => verification_url(pact, verification),
12+
'${pactbroker.verificationResultUrl}' => verification_url(verification),
1213
'${pactbroker.consumerVersionNumber}' => pact ? pact.consumer_version_number : "",
1314
'${pactbroker.providerVersionNumber}' => verification ? verification.provider_version_number : "",
15+
'${pactbroker.providerVersionTags}' => provider_version_tags(verification),
16+
'${pactbroker.consumerVersionTags}' => consumer_version_tags(pact),
1417
'${pactbroker.consumerName}' => pact ? pact.consumer_name : "",
1518
'${pactbroker.providerName}' => pact ? pact.provider_name : "",
16-
'${pactbroker.githubVerificationStatus}' => github_verification_status(pact, verification)
19+
'${pactbroker.githubVerificationStatus}' => github_verification_status(verification)
1720
}
1821

1922
if escaper
@@ -27,21 +30,33 @@ def self.call(template, pact, verification = nil, &escaper)
2730
end
2831
end
2932

30-
def self.github_verification_status pact, verification
33+
def self.github_verification_status verification
3134
if verification
3235
verification.success ? "success" : "failure"
33-
elsif pact && pact.latest_verification
34-
pact.latest_verification.success ? "success" : "failure"
35-
elsif pact
36+
else
3637
"pending"
38+
end
39+
end
40+
41+
def self.verification_url verification
42+
if verification
43+
PactBroker::Api::PactBrokerUrls.verification_url(verification, PactBroker.configuration.base_url)
3744
else
3845
""
3946
end
4047
end
4148

42-
def self.verification_url pact, verification
43-
if verification || (pact && pact.latest_verification)
44-
PactBroker::Api::PactBrokerUrls.verification_url(verification || pact.latest_verification, PactBroker.configuration.base_url)
49+
def self.consumer_version_tags pact
50+
if pact
51+
pact.consumer_version.tags.collect(&:name).join(", ")
52+
else
53+
""
54+
end
55+
end
56+
57+
def self.provider_version_tags verification
58+
if verification
59+
verification.provider_version.tags.collect(&:name).join(", ")
4560
else
4661
""
4762
end

spec/lib/pact_broker/webhooks/render_spec.rb

+31-6
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,56 @@ module Webhooks
1414
end
1515

1616
let(:pact) do
17-
instance_double("pact", consumer_version_number: "1.2.3+foo", consumer_name: "Foo", provider_name: "Bar", latest_verification: nil)
17+
double("pact",
18+
consumer_version: consumer_version,
19+
consumer_version_number: "1.2.3+foo",
20+
consumer_name: "Foo",
21+
provider_name: "Bar",
22+
latest_verification: nil)
1823
end
1924

2025
let(:pact_with_no_verification) { pact }
2126

2227
let(:pact_with_successful_verification) do
23-
instance_double("pact",
28+
double("pact",
29+
consumer_version: consumer_version,
2430
consumer_version_number: "1.2.3+foo",
2531
consumer_name: "Foo",
2632
provider_name: "Bar",
2733
latest_verification: verification)
2834
end
2935

3036
let(:pact_with_failed_verification) do
31-
instance_double("pact",
37+
double("pact",
38+
consumer_version: consumer_version,
3239
consumer_version_number: "1.2.3+foo",
3340
consumer_name: "Foo",
3441
provider_name: "Bar",
3542
latest_verification: failed_verification)
3643
end
3744

3845
let(:verification) do
39-
instance_double("verification", provider_version_number: "3", success: true)
46+
double("verification", provider_version_number: "3", success: true, provider_version: provider_version)
4047
end
4148

4249
let(:failed_verification) do
43-
instance_double("verification", provider_version_number: "3", success: false)
50+
double("verification", provider_version_number: "3", success: false, provider_version: provider_version)
51+
end
52+
53+
let(:provider_version) do
54+
double("version", tags: provider_tags)
55+
end
56+
57+
let(:consumer_version) do
58+
double("version", tags: consumer_tags)
59+
end
60+
61+
let(:provider_tags) do
62+
[ double("tag", name: "test"), double("tag", name: "prod") ]
63+
end
64+
65+
let(:consumer_tags) do
66+
[ double("tag", name: "test") ]
4467
end
4568

4669
let(:nil_pact) { nil }
@@ -57,13 +80,15 @@ module Webhooks
5780
["${pactbroker.providerName}", "Bar", :pact, :verification],
5881
["${pactbroker.githubVerificationStatus}", "success", :pact, :verification],
5982
["${pactbroker.githubVerificationStatus}", "failure", :pact, :failed_verification],
60-
["${pactbroker.githubVerificationStatus}", "", :nil_pact, :nil_verification],
83+
["${pactbroker.githubVerificationStatus}", "pending", :nil_pact, :nil_verification],
6184
["${pactbroker.githubVerificationStatus}", "pending", :pact_with_no_verification, :nil_verification],
6285
["${pactbroker.githubVerificationStatus}", "success", :pact_with_successful_verification, :nil_verification],
6386
["${pactbroker.githubVerificationStatus}", "failure", :pact_with_failed_verification, :nil_verification],
6487
["${pactbroker.verificationResultUrl}", "", :pact_with_no_verification, :nil_verification],
6588
["${pactbroker.verificationResultUrl}", "http://verification", :pact_with_successful_verification, :nil_verification],
6689
["${pactbroker.verificationResultUrl}", "http://verification", :pact_with_successful_verification, :verification],
90+
["${pactbroker.providerVersionTags}", "test, prod", :pact_with_successful_verification, :verification],
91+
["${pactbroker.consumerVersionTags}", "test", :pact_with_successful_verification, :verification],
6792
]
6893

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

0 commit comments

Comments
 (0)