diff --git a/lib/pact_broker/api/resources/publish_contracts.rb b/lib/pact_broker/api/resources/publish_contracts.rb index e73ce6f11..e19e4b399 100644 --- a/lib/pact_broker/api/resources/publish_contracts.rb +++ b/lib/pact_broker/api/resources/publish_contracts.rb @@ -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 diff --git a/lib/pact_broker/locale/en.yml b/lib/pact_broker/locale/en.yml index ac672e962..e95f644d6 100644 --- a/lib/pact_broker/locale/en.yml +++ b/lib/pact_broker/locale/en.yml @@ -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}. diff --git a/lib/pact_broker/webhooks/pact_and_verification_parameters.rb b/lib/pact_broker/webhooks/pact_and_verification_parameters.rb index 3a12b9f7a..f828069ef 100644 --- a/lib/pact_broker/webhooks/pact_and_verification_parameters.rb +++ b/lib/pact_broker/webhooks/pact_and_verification_parameters.rb @@ -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" @@ -38,6 +39,7 @@ class PactAndVerificationParameters CONSUMER_LABELS, PROVIDER_LABELS, EVENT_NAME, + BUILD_URL, CURRENTLY_DEPLOYED_PROVIDER_VERSION_NUMBER ] @@ -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 @@ -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 diff --git a/spec/integration/webhooks/contract_publication_spec.rb b/spec/integration/webhooks/contract_publication_spec.rb index 2bdf33bd4..baa8e6165 100644 --- a/spec/integration/webhooks/contract_publication_spec.rb +++ b/spec/integration/webhooks/contract_publication_spec.rb @@ -7,6 +7,7 @@ { "consumer_version_number" => "${pactbroker.consumerVersionNumber}", "consumer_version_branch" => "${pactbroker.consumerVersionBranch}", + "build_url" => "${pactbroker.buildUrl}", "consumer_version_tags" => "${pactbroker.consumerVersionTags}", } end @@ -16,6 +17,7 @@ consumer_version_number: "1", consumer_version_branch: "main", consumer_version_tags: "a, b", + build_url: "http://ci/builds/1234" } end diff --git a/spec/lib/pact_broker/webhooks/render_spec.rb b/spec/lib/pact_broker/webhooks/render_spec.rb index bf5160885..e9b905a67 100644 --- a/spec/lib/pact_broker/webhooks/render_spec.rb +++ b/spec/lib/pact_broker/webhooks/render_spec.rb @@ -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 } @@ -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) |