Skip to content

Commit

Permalink
feat(webhooks): support ${pactbroker.providerVersionDescriptions} tem…
Browse files Browse the repository at this point in the history
…plate parameter
  • Loading branch information
bethesque committed Sep 28, 2021
1 parent 9b79b33 commit 54a073e
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/deployments/deployed_version_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/deployments/released_version_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/pact_broker/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/test/http_test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions lib/pact_broker/verifications/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
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 @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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 : "",
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/webhooks/trigger_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion script/data/contract-published-requiring-verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions spec/lib/pact_broker/verifications/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 54a073e

Please sign in to comment.