Skip to content

Commit afebbc5

Browse files
committed
feat: add ${pactbroker.consumerLabels} and ${pactbroker.providerLabels} to webhook templates
1 parent 8da29cd commit afebbc5

File tree

6 files changed

+56
-12
lines changed

6 files changed

+56
-12
lines changed

lib/pact_broker/api/decorators/pacticipant_decorator.rb

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class PacticipantDecorator < BaseDecorator
3333
}
3434
end
3535

36+
link :'pb:label' do | options |
37+
{
38+
title: "Get, create or delete a label for #{represented.name}",
39+
href: templated_label_url_for_pacticipant(represented.name, options[:base_url]),
40+
templated: true
41+
}
42+
end
43+
3644
# TODO deprecate in v3
3745
# URL isn't implemented
3846
# link 'latest-version' do | options |

lib/pact_broker/api/pact_broker_urls.rb

+4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ def templated_tag_url_for_pacticipant pacticipant_name, base_url = ""
140140
pacticipant_url_from_params({pacticipant_name: pacticipant_name}, base_url) + "/versions/{version}/tags/{tag}"
141141
end
142142

143+
def templated_label_url_for_pacticipant pacticipant_name, base_url = ""
144+
pacticipant_url_from_params({pacticipant_name: pacticipant_name}, base_url) + "/labels/{label}"
145+
end
146+
143147
def label_url label, base_url
144148
"#{labels_url(label.pacticipant, base_url)}/#{label.name}"
145149
end

lib/pact_broker/doc/views/webhooks.markdown

+11-9
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,17 @@ To specify an XML body, you will need to use a correctly escaped string (or use
8484

8585
The following variables may be used in the request path, parameters or body, and will be replaced with their appropriate values at runtime.
8686

87-
`${pactbroker.consumerName}`: the consumer name
88-
`${pactbroker.providerName}`: the provider name
89-
`${pactbroker.consumerVersionNumber}`: the version number of the most recent consumer version associated with the pact content.
90-
`${pactbroker.providerVersionNumber}`: the provider version number for the verification result
91-
`${pactbroker.consumerVersionTags}`: the list of tag names for the most recent consumer version associated with the pact content, separated by ", ".
92-
`${pactbroker.providerVersionTags}`: the list of tag names for the provider version associated with the verification result, separated by ", ".
93-
`${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).
94-
`${pactbroker.pactUrl}`: the "permalink" URL to the newly published pact (the URL specifying the consumer version URL, rather than the "/latest" format.)
95-
`${pactbroker.verificationResultUrl}`: the URL to the relevant verification result.
87+
* `${pactbroker.consumerName}`: the consumer name
88+
* `${pactbroker.providerName}`: the provider name
89+
* `${pactbroker.consumerVersionNumber}`: the version number of the most recent consumer version associated with the pact content.
90+
* `${pactbroker.providerVersionNumber}`: the provider version number for the verification result
91+
* `${pactbroker.consumerVersionTags}`: the list of tag names for the most recent consumer version associated with the pact content, separated by ", ".
92+
* `${pactbroker.providerVersionTags}`: the list of tag names for the provider version associated with the verification result, separated by ", ".
93+
* `${pactbroker.consumerLabels}`: the list of labels for the consumer associated with the pact content, separated by ", ".
94+
* `${pactbroker.providerLabels}`: the list of labels for the provider associated with the pact content, separated by ", ".
95+
* `${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).
96+
* `${pactbroker.pactUrl}`: the "permalink" URL to the newly published pact (the URL specifying the consumer version URL, rather than the "/latest" format.)
97+
* `${pactbroker.verificationResultUrl}`: the URL to the relevant verification result.
9698

9799
Example usage:
98100

lib/pact_broker/pacts/placeholder_pact.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module PactBroker
44
module Pacts
55
class PlaceholderPact < PactBroker::Domain::Pact
66
def initialize
7-
consumer = OpenStruct.new(name: "placeholder-consumer")
8-
@provider = OpenStruct.new(name: "placeholder-provider")
7+
consumer = OpenStruct.new(name: "placeholder-consumer", labels: [OpenStruct.new(name: "placeholder-consumer-label")])
8+
@provider = OpenStruct.new(name: "placeholder-provider", labels: [OpenStruct.new(name: "placeholder-provider-label")])
99
@consumer_version = OpenStruct.new(number: "gggghhhhjjjjkkkkllll66667777888899990000", pacticipant: consumer, tags: [OpenStruct.new(name: "master")])
1010
@consumer_version_number = @consumer_version.number
1111
@created_at = DateTime.now

lib/pact_broker/webhooks/render.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def self.call(template, pact, trigger_verification = nil, &escaper)
1616
'${pactbroker.consumerVersionTags}' => consumer_version_tags(pact),
1717
'${pactbroker.consumerName}' => pact ? pact.consumer_name : "",
1818
'${pactbroker.providerName}' => pact ? pact.provider_name : "",
19-
'${pactbroker.githubVerificationStatus}' => github_verification_status(verification)
19+
'${pactbroker.githubVerificationStatus}' => github_verification_status(verification),
20+
'${pactbroker.consumerLabels}' => pacticipant_labels(pact && pact.consumer),
21+
'${pactbroker.providerLabels}' => pacticipant_labels(pact && pact.provider)
2022
}
2123

2224
if escaper
@@ -61,6 +63,10 @@ def self.provider_version_tags verification
6163
""
6264
end
6365
end
66+
67+
def self.pacticipant_labels pacticipant
68+
pacticipant && pacticipant.labels ? pacticipant.labels.collect(&:name).join(", ") : ""
69+
end
6470
end
6571
end
6672
end

spec/lib/pact_broker/webhooks/render_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module Webhooks
2121
consumer_version_number: "1.2.3+foo",
2222
consumer_name: "Foo",
2323
provider_name: "Bar",
24+
consumer: consumer,
25+
provider: provider,
2426
latest_verification: nil)
2527
end
2628

@@ -32,6 +34,8 @@ module Webhooks
3234
consumer_version_number: "1.2.3+foo",
3335
consumer_name: "Foo",
3436
provider_name: "Bar",
37+
consumer: consumer,
38+
provider: provider,
3539
latest_verification: verification)
3640
end
3741

@@ -41,9 +45,19 @@ module Webhooks
4145
consumer_version_number: "1.2.3+foo",
4246
consumer_name: "Foo",
4347
provider_name: "Bar",
48+
consumer: consumer,
49+
provider: provider,
4450
latest_verification: failed_verification)
4551
end
4652

53+
let (:provider) do
54+
double("provider", labels: provider_labels)
55+
end
56+
57+
let (:consumer) do
58+
double("consumer", labels: consumer_labels)
59+
end
60+
4761
let(:verification) do
4862
double("verification", provider_version_number: "3", success: true, provider_version: provider_version)
4963
end
@@ -68,6 +82,14 @@ module Webhooks
6882
[ double("tag", name: "test") ]
6983
end
7084

85+
let(:provider_labels) do
86+
[ double("label", name: "finance"), double("label", name: "IT") ]
87+
end
88+
89+
let(:consumer_labels) do
90+
[ double("label", name: "foo"), double("label", name: "bar") ]
91+
end
92+
7193
let(:nil_pact) { nil }
7294
let(:nil_verification) { nil }
7395

@@ -91,6 +113,8 @@ module Webhooks
91113
["${pactbroker.verificationResultUrl}", "http://verification", :pact_with_successful_verification, :verification],
92114
["${pactbroker.providerVersionTags}", "test, prod", :pact_with_successful_verification, :verification],
93115
["${pactbroker.consumerVersionTags}", "test", :pact_with_successful_verification, :verification],
116+
["${pactbroker.consumerLabels}", "foo, bar", :pact_with_successful_verification, :verification],
117+
["${pactbroker.providerLabels}", "finance, IT", :pact, :nil_verification],
94118
]
95119

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

0 commit comments

Comments
 (0)