diff --git a/lib/pact_broker/deployments/deployed_version_service.rb b/lib/pact_broker/deployments/deployed_version_service.rb index 8879baf58..1fb364c40 100644 --- a/lib/pact_broker/deployments/deployed_version_service.rb +++ b/lib/pact_broker/deployments/deployed_version_service.rb @@ -65,6 +65,7 @@ def self.find_currently_deployed_versions_for_pacticipant(pacticipant) .where(pacticipant_id: pacticipant.id) .eager(:version) .eager(:environment) + .order(:created_at, :id) .all end diff --git a/lib/pact_broker/deployments/released_version_service.rb b/lib/pact_broker/deployments/released_version_service.rb index a7c898812..016b712ae 100644 --- a/lib/pact_broker/deployments/released_version_service.rb +++ b/lib/pact_broker/deployments/released_version_service.rb @@ -52,6 +52,7 @@ def self.find_currently_supported_versions_for_pacticipant(pacticipant) .where(pacticipant_id: pacticipant.id) .eager(:version) .eager(:environment) + .order(:created_at, :id) .all end end diff --git a/lib/pact_broker/locale/en.yml b/lib/pact_broker/locale/en.yml index e95f644d6..d1f5c8804 100644 --- a/lib/pact_broker/locale/en.yml +++ b/lib/pact_broker/locale/en.yml @@ -34,10 +34,11 @@ en: consumerVersionTags: The list of tag names for the most recent consumer version associated with the pact content, separated by ", " consumerVersionBranch: The repository branch associated with the consumer version providerVersionTags: The list of tag names for the provider version associated with the verification result, separated by ", ". + providerVersionDescriptions: The descriptions of the provider version(s) for which the contract_requiring_verification_published webhook has been triggered. providerVersionBranch: The repository branch associated with the provider version consumerLabels: The list of labels for the consumer associated with the pact content, separated by ", ". providerLabels: The list of labels for the provider associated with the pact content, separated by ", ". - pactUrl: The "permalink" URL to the newly published pact (the URL specifying the consumer version URL, rather than the "/latest" format. + pactUrl: The "permalink" URL to the newly published pact content (using the pact version SHA). verificationResultUrl: The URL to the relevant verification result. githubVerificationStatus: The verification status using the correct keywords for posting to the Github commit status API. See https://developer.github.com/v3/repos/statuses bitbucketVerificationStatus: The verification status using the correct keywords for posting to the Bitbucket commit status API. See https://developer.atlassian.com/server/bitbucket/how-tos/updating-build-status-for-commits/ diff --git a/lib/pact_broker/test/http_test_data_builder.rb b/lib/pact_broker/test/http_test_data_builder.rb index d5f81055f..90061f869 100644 --- a/lib/pact_broker/test/http_test_data_builder.rb +++ b/lib/pact_broker/test/http_test_data_builder.rb @@ -230,6 +230,7 @@ def create_webhook_for_event(uuid: nil, url: "https://postman-echo.com/post", bo "providerVersionBranch" => "${pactbroker.providerVersionBranch}", "providerName" => "${pactbroker.providerName}", "providerVersionNumber" => "${pactbroker.providerVersionNumber}", + "providerVersionDescriptions" => "${pactbroker.providerVersionDescriptions}", "consumerVersionBranch" => "${pactbroker.consumerVersionBranch}", } request_body = { diff --git a/lib/pact_broker/verifications/service.rb b/lib/pact_broker/verifications/service.rb index 1965fead8..4fe52e055 100644 --- a/lib/pact_broker/verifications/service.rb +++ b/lib/pact_broker/verifications/service.rb @@ -138,7 +138,7 @@ def required_verifications_for_main_branch(pact_version) latest_version_from_main_branch = [version_service.find_latest_version_from_main_branch(pact_version.provider)].compact latest_version_from_main_branch.collect do | main_branch_version | - identify_required_verification(pact_version, main_branch_version, "latest version from main branch") + identify_required_verification(pact_version, main_branch_version, "latest from main branch") end.compact end private :required_verifications_for_main_branch @@ -148,7 +148,7 @@ def required_verifications_for_deployed_versions(pact_version) unscoped_service.find_currently_deployed_versions_for_pacticipant(pact_version.provider) end deployed_versions.collect do | deployed_version | - identify_required_verification(pact_version, deployed_version.version, "currently deployed version (#{deployed_version.environment_name})") + identify_required_verification(pact_version, deployed_version.version, "deployed in #{deployed_version.environment_name}") end.compact end private :required_verifications_for_deployed_versions @@ -158,7 +158,7 @@ def required_verifications_for_released_versions(pact_version) unscoped_service.find_currently_supported_versions_for_pacticipant(pact_version.provider) end released_versions.collect do | released_version | - identify_required_verification(pact_version, released_version.version, "currently released version (#{released_version.environment_name})") + identify_required_verification(pact_version, released_version.version, "released in #{released_version.environment_name}") end.compact end private :required_verifications_for_released_versions diff --git a/lib/pact_broker/webhooks/pact_and_verification_parameters.rb b/lib/pact_broker/webhooks/pact_and_verification_parameters.rb index f828069ef..20353ba06 100644 --- a/lib/pact_broker/webhooks/pact_and_verification_parameters.rb +++ b/lib/pact_broker/webhooks/pact_and_verification_parameters.rb @@ -7,6 +7,7 @@ class PactAndVerificationParameters PROVIDER_VERSION_NUMBER = "pactbroker.providerVersionNumber" PROVIDER_VERSION_TAGS = "pactbroker.providerVersionTags" PROVIDER_VERSION_BRANCH = "pactbroker.providerVersionBranch" + PROVIDER_VERSION_DESCRIPTIONS = "pactbroker.providerVersionDescriptions" CONSUMER_VERSION_TAGS = "pactbroker.consumerVersionTags" CONSUMER_VERSION_BRANCH = "pactbroker.consumerVersionBranch" CONSUMER_NAME = "pactbroker.consumerName" @@ -27,6 +28,7 @@ class PactAndVerificationParameters CONSUMER_VERSION_NUMBER, PROVIDER_VERSION_NUMBER, PROVIDER_VERSION_TAGS, + PROVIDER_VERSION_DESCRIPTIONS, PROVIDER_VERSION_BRANCH, CONSUMER_VERSION_TAGS, CONSUMER_VERSION_BRANCH, @@ -59,6 +61,7 @@ def to_hash PROVIDER_VERSION_NUMBER => provider_version_number, PROVIDER_VERSION_TAGS => provider_version_tags, PROVIDER_VERSION_BRANCH => provider_version_branch, + PROVIDER_VERSION_DESCRIPTIONS => provider_version_descriptions, CONSUMER_VERSION_TAGS => consumer_version_tags, CONSUMER_VERSION_BRANCH => consumer_version_branch, CONSUMER_NAME => pact ? pact.consumer_name : "", @@ -175,6 +178,10 @@ def provider_version_branch end end + def provider_version_descriptions + webhook_context[:provider_version_descriptions]&.join(", ") || "" + end + def pacticipant_labels pacticipant pacticipant && pacticipant.labels ? pacticipant.labels.collect(&:name).join(", ") : "" end diff --git a/lib/pact_broker/webhooks/trigger_service.rb b/lib/pact_broker/webhooks/trigger_service.rb index 63341c552..444684f27 100644 --- a/lib/pact_broker/webhooks/trigger_service.rb +++ b/lib/pact_broker/webhooks/trigger_service.rb @@ -135,7 +135,7 @@ def expand_events_for_required_verifications(event_name, pact, event_contexts) required_verifications = verification_service.calculate_required_verifications_for_pact(pact) event_contexts.flat_map do | event_context | required_verifications.collect do | required_verification | - event_context.merge(provider_version_number: required_verification.provider_version.number, provider_version_descriptions: required_verification.provider_version_descriptions) + event_context.merge(provider_version_number: required_verification.provider_version.number, provider_version_descriptions: required_verification.provider_version_descriptions.uniq) end end else diff --git a/script/data/contract-published-requiring-verification.rb b/script/data/contract-published-requiring-verification.rb index fe55e077c..212006584 100755 --- a/script/data/contract-published-requiring-verification.rb +++ b/script/data/contract-published-requiring-verification.rb @@ -14,7 +14,6 @@ .create_pacticipant("NewWebhookTestConsumer") .create_pacticipant("NewWebhookTestProvider") .create_tagged_pacticipant_version(pacticipant: "NewWebhookTestProvider", version: "1", tag: "main") - .deploy_to_prod(pacticipant: "NewWebhookTestProvider", version: "1") .record_deployment(pacticipant: "NewWebhookTestProvider", version: "1", environment_name: "test") .record_deployment(pacticipant: "NewWebhookTestProvider", version: "1", environment_name: "prod") .create_version(pacticipant: "NewWebhookTestProvider", version: "2", branch: "main") diff --git a/spec/integration/webhooks/contract_requiring_verification_published_spec.rb b/spec/integration/webhooks/contract_requiring_verification_published_spec.rb new file mode 100644 index 000000000..34d48f760 --- /dev/null +++ b/spec/integration/webhooks/contract_requiring_verification_published_spec.rb @@ -0,0 +1,67 @@ +RSpec.describe "triggering a contract_requiring_verification_published webhook for a contract publication" do + before do + td.create_global_webhook(event_names: ["contract_requiring_verification_published"], body: webhook_body_template) + .create_environment("test") + .create_provider("Bar", main_branch: "main") + .create_provider_version("1", branch: "main") + end + + let(:webhook_body_template) do + { + "provider_version_number" => "${pactbroker.providerVersionNumber}", + "provider_version_descriptions" => "${pactbroker.providerVersionDescriptions}" + } + end + + let(:expected_webhook_body) do + { + provider_version_number: "1", + provider_version_descriptions: a_kind_of(String) + } + end + + let(:request_body_hash) do + { + :pacticipantName => "Foo", + :pacticipantVersionNumber => "1", + :branch => "main", + :tags => ["a", "b"], + :buildUrl => "http://ci/builds/1234", + :contracts => [ + { + :consumerName => "Foo", + :providerName => "Bar", + :specification => "pact", + :contentType => "application/json", + :content => encoded_contract + } + ] + } + end + + let(:rack_headers) do + { + "CONTENT_TYPE" => "application/json", + "HTTP_ACCEPT" => "application/hal+json", + "pactbroker.database_connector" => database_connector} + end + + let(:contract) { { consumer: { name: "Foo" }, provider: { name: "Bar" }, interactions: [] }.to_json } + let(:encoded_contract) { Base64.strict_encode64(contract) } + let(:path) { "/contracts/publish" } + + let!(:request) do + stub_request(:post, /http/).to_return(:status => 200) + end + + subject { post(path, request_body_hash.to_json, rack_headers) } + + let(:database_connector) { ->(&block) { block.call } } + + it { is_expected.to be_a_hal_json_success_response } + + it "passes through the correct parameters to the webhook" do + subject + expect(a_request(:post, /http/).with(body: expected_webhook_body)).to have_been_made + end +end diff --git a/spec/lib/pact_broker/verifications/service_spec.rb b/spec/lib/pact_broker/verifications/service_spec.rb index add1614b7..f57db42dc 100644 --- a/spec/lib/pact_broker/verifications/service_spec.rb +++ b/spec/lib/pact_broker/verifications/service_spec.rb @@ -115,7 +115,7 @@ module Verifications expect(subject).to eq [ RequiredVerification.new( provider_version: td.find_version("Bar", "1"), - provider_version_descriptions: ["latest version from main branch"] + provider_version_descriptions: ["latest from main branch"] ) ] end @@ -153,7 +153,7 @@ module Verifications expect(subject).to eq [ RequiredVerification.new( provider_version: td.find_version("Bar", "1"), - provider_version_descriptions: ["currently deployed version (test)"] + provider_version_descriptions: ["deployed in test"] ) ] end @@ -193,7 +193,7 @@ module Verifications expect(subject).to eq [ RequiredVerification.new( provider_version: td.find_version("Bar", "1"), - provider_version_descriptions: ["currently released version (test)"] + provider_version_descriptions: ["released in test"] ) ] end @@ -235,9 +235,9 @@ module Verifications RequiredVerification.new( provider_version: td.find_version("Bar", "1"), provider_version_descriptions: [ - "latest version from main branch", - "currently deployed version (test)", - "currently released version (test)" + "latest from main branch", + "deployed in test", + "released in test" ]) ] end