Skip to content

Commit

Permalink
feat: add ${pactbroker.consumerLabels} and ${pactbroker.providerLabel…
Browse files Browse the repository at this point in the history
…s} to webhook templates
  • Loading branch information
bethesque committed Sep 7, 2018
1 parent 8da29cd commit afebbc5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
8 changes: 8 additions & 0 deletions lib/pact_broker/api/decorators/pacticipant_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ class PacticipantDecorator < BaseDecorator
}
end

link :'pb:label' do | options |
{
title: "Get, create or delete a label for #{represented.name}",
href: templated_label_url_for_pacticipant(represented.name, options[:base_url]),
templated: true
}
end

# TODO deprecate in v3
# URL isn't implemented
# link 'latest-version' do | options |
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ def templated_tag_url_for_pacticipant pacticipant_name, base_url = ""
pacticipant_url_from_params({pacticipant_name: pacticipant_name}, base_url) + "/versions/{version}/tags/{tag}"
end

def templated_label_url_for_pacticipant pacticipant_name, base_url = ""
pacticipant_url_from_params({pacticipant_name: pacticipant_name}, base_url) + "/labels/{label}"
end

def label_url label, base_url
"#{labels_url(label.pacticipant, base_url)}/#{label.name}"
end
Expand Down
20 changes: 11 additions & 9 deletions lib/pact_broker/doc/views/webhooks.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,17 @@ To specify an XML body, you will need to use a correctly escaped string (or use

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

`${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.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.
* `${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.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.consumerLabels}`: the list of labels for the consumer associated with the pact content, separated by ", ".
* `${pactbroker.providerLabels}`: the list of labels for the provider associated with the pact content, 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.

Example usage:

Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/pacts/placeholder_pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module PactBroker
module Pacts
class PlaceholderPact < PactBroker::Domain::Pact
def initialize
consumer = OpenStruct.new(name: "placeholder-consumer")
@provider = OpenStruct.new(name: "placeholder-provider")
consumer = OpenStruct.new(name: "placeholder-consumer", labels: [OpenStruct.new(name: "placeholder-consumer-label")])
@provider = OpenStruct.new(name: "placeholder-provider", labels: [OpenStruct.new(name: "placeholder-provider-label")])
@consumer_version = OpenStruct.new(number: "gggghhhhjjjjkkkkllll66667777888899990000", pacticipant: consumer, tags: [OpenStruct.new(name: "master")])
@consumer_version_number = @consumer_version.number
@created_at = DateTime.now
Expand Down
8 changes: 7 additions & 1 deletion lib/pact_broker/webhooks/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def self.call(template, pact, trigger_verification = nil, &escaper)
'${pactbroker.consumerVersionTags}' => consumer_version_tags(pact),
'${pactbroker.consumerName}' => pact ? pact.consumer_name : "",
'${pactbroker.providerName}' => pact ? pact.provider_name : "",
'${pactbroker.githubVerificationStatus}' => github_verification_status(verification)
'${pactbroker.githubVerificationStatus}' => github_verification_status(verification),
'${pactbroker.consumerLabels}' => pacticipant_labels(pact && pact.consumer),
'${pactbroker.providerLabels}' => pacticipant_labels(pact && pact.provider)
}

if escaper
Expand Down Expand Up @@ -61,6 +63,10 @@ def self.provider_version_tags verification
""
end
end

def self.pacticipant_labels pacticipant
pacticipant && pacticipant.labels ? pacticipant.labels.collect(&:name).join(", ") : ""
end
end
end
end
24 changes: 24 additions & 0 deletions spec/lib/pact_broker/webhooks/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module Webhooks
consumer_version_number: "1.2.3+foo",
consumer_name: "Foo",
provider_name: "Bar",
consumer: consumer,
provider: provider,
latest_verification: nil)
end

Expand All @@ -32,6 +34,8 @@ module Webhooks
consumer_version_number: "1.2.3+foo",
consumer_name: "Foo",
provider_name: "Bar",
consumer: consumer,
provider: provider,
latest_verification: verification)
end

Expand All @@ -41,9 +45,19 @@ module Webhooks
consumer_version_number: "1.2.3+foo",
consumer_name: "Foo",
provider_name: "Bar",
consumer: consumer,
provider: provider,
latest_verification: failed_verification)
end

let (:provider) do
double("provider", labels: provider_labels)
end

let (:consumer) do
double("consumer", labels: consumer_labels)
end

let(:verification) do
double("verification", provider_version_number: "3", success: true, provider_version: provider_version)
end
Expand All @@ -68,6 +82,14 @@ module Webhooks
[ double("tag", name: "test") ]
end

let(:provider_labels) do
[ double("label", name: "finance"), double("label", name: "IT") ]
end

let(:consumer_labels) do
[ double("label", name: "foo"), double("label", name: "bar") ]
end

let(:nil_pact) { nil }
let(:nil_verification) { nil }

Expand All @@ -91,6 +113,8 @@ module Webhooks
["${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],
["${pactbroker.consumerLabels}", "foo, bar", :pact_with_successful_verification, :verification],
["${pactbroker.providerLabels}", "finance, IT", :pact, :nil_verification],
]

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

0 comments on commit afebbc5

Please sign in to comment.