Skip to content

Commit

Permalink
feat(webhooks): support ${pactbroker.buildUrl} template parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 28, 2021
1 parent 3083561 commit 9b79b33
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/publish_contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def malformed_request?
end

def process_post
handle_webhook_events(consumer_version_branch: parsed_contracts.branch) do
handle_webhook_events(consumer_version_branch: parsed_contracts.branch, build_url: parsed_contracts.build_url) do
results = contract_service.publish(parsed_contracts, base_url: base_url)
response.body = decorator_class(:publish_contracts_results_decorator).new(results).to_json(decorator_options)
end
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ en:
azureDevOpsVerificationStatus: The verification status using the correct keywords for posting to the Azure DevOps GitStatusState API. See https://docs.microsoft.com/en-us/rest/api/azure/devops/git/statuses/create?view=azure-devops-rest-6.0
gitlabVerificationStatus: The verification status using the correct keywords for posting to the Gitlab Commits API. See https://docs.gitlab.com/ee/api/commits.html#post-the-build-status-to-a-commit
eventName: The name of the event that triggered the webhook
buildUrl: The URL of the build that published the resources that have triggered the webhook (currently only supported for contracts published using the 'all in one' endpoint).
currentlyDeployedProviderVersionNumber: The version number of the currently deployed provider version (when used in a template, the webhook will be triggered once for each currently deployed provider version)
no_webhooks_enabled_for_event: No enabled webhooks found for the detected events
webhook_triggered_for_event: Webhook %{webhook_description} triggered for event %{event_name}.
Expand Down
7 changes: 7 additions & 0 deletions lib/pact_broker/webhooks/pact_and_verification_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PactAndVerificationParameters
GITLAB_VERIFICATION_STATUS = "pactbroker.gitlabVerificationStatus"
CONSUMER_LABELS = "pactbroker.consumerLabels"
PROVIDER_LABELS = "pactbroker.providerLabels"
BUILD_URL = "pactbroker.buildUrl"
EVENT_NAME = "pactbroker.eventName"
CURRENTLY_DEPLOYED_PROVIDER_VERSION_NUMBER = "pactbroker.currentlyDeployedProviderVersionNumber"

Expand All @@ -38,6 +39,7 @@ class PactAndVerificationParameters
CONSUMER_LABELS,
PROVIDER_LABELS,
EVENT_NAME,
BUILD_URL,
CURRENTLY_DEPLOYED_PROVIDER_VERSION_NUMBER
]

Expand Down Expand Up @@ -68,6 +70,7 @@ def to_hash
CONSUMER_LABELS => pacticipant_labels(pact && pact.consumer),
PROVIDER_LABELS => pacticipant_labels(pact && pact.provider),
EVENT_NAME => event_name,
BUILD_URL => build_url,
CURRENTLY_DEPLOYED_PROVIDER_VERSION_NUMBER => currently_deployed_provider_version_number
}
end
Expand Down Expand Up @@ -180,6 +183,10 @@ def event_name
webhook_context.fetch(:event_name)
end

def build_url
webhook_context[:build_url] || ""
end

def currently_deployed_provider_version_number
webhook_context[:currently_deployed_provider_version_number] || ""
end
Expand Down
2 changes: 2 additions & 0 deletions spec/integration/webhooks/contract_publication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
"consumer_version_number" => "${pactbroker.consumerVersionNumber}",
"consumer_version_branch" => "${pactbroker.consumerVersionBranch}",
"build_url" => "${pactbroker.buildUrl}",
"consumer_version_tags" => "${pactbroker.consumerVersionTags}",
}
end
Expand All @@ -16,6 +17,7 @@
consumer_version_number: "1",
consumer_version_branch: "main",
consumer_version_tags: "a, b",
build_url: "http://ci/builds/1234"
}
end

Expand Down
3 changes: 2 additions & 1 deletion spec/lib/pact_broker/webhooks/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module Webhooks
[ double("label", name: "foo"), double("label", name: "bar") ]
end

let(:webhook_context) { { base_url: base_url, event_name: "something" } }
let(:webhook_context) { { base_url: base_url, event_name: "something", build_url: "http://build"} }

let(:nil_pact) { nil }
let(:nil_verification) { nil }
Expand Down Expand Up @@ -140,6 +140,7 @@ module Webhooks
["${pactbroker.consumerVersionBranch}", "consumer-branch", :pact_with_successful_verification, :verification],
["${pactbroker.consumerLabels}", "foo, bar", :pact_with_successful_verification, :verification],
["${pactbroker.providerLabels}", "finance, IT", :pact, :nil_verification],
["${pactbroker.buildUrl}", "http://build", :nil_pact, :nil_verification]
]

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

0 comments on commit 9b79b33

Please sign in to comment.