From 9608be8b9ebfb10ccf75844828a29a7c940af55b Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 11 Jun 2021 10:41:16 +1000 Subject: [PATCH] feat: add endpoint to list currently supported versions for an environment --- lib/pact_broker/api.rb | 1 + .../decorators/deployed_version_decorator.rb | 5 +- .../api/decorators/environment_decorator.rb | 7 ++ .../decorators/released_version_decorator.rb | 11 +++ lib/pact_broker/api/pact_broker_urls.rb | 4 ++ ...ently_deployed_versions_for_environment.rb | 12 +++- ...ntly_supported_versions_for_environment.rb | 70 +++++++++++++++++++ .../deployments/released_version.rb | 4 ++ .../deployments/released_version_service.rb | 9 +++ lib/pact_broker/test/test_data_builder.rb | 10 ++- ..._deployed_versions_for_environment_spec.rb | 5 +- 11 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 lib/pact_broker/api/resources/currently_supported_versions_for_environment.rb diff --git a/lib/pact_broker/api.rb b/lib/pact_broker/api.rb index 99869540a..578d928cb 100644 --- a/lib/pact_broker/api.rb +++ b/lib/pact_broker/api.rb @@ -122,6 +122,7 @@ def self.build_api(application_context = PactBroker::ApplicationContext.default_ add ["environments"], Api::Resources::Environments, { resource_name: "environments" } add ["environments", :environment_uuid], Api::Resources::Environment, { resource_name: "environment" } add ["environments", :environment_uuid, "currently-deployed-versions"], Api::Resources::CurrentlyDeployedVersionsForEnvironment, { resource_name: "environment_deployed_versions" } + add ["environments", :environment_uuid, "currently-supported-versions"], Api::Resources::CurrentlySupportedVersionsForEnvironment, { resource_name: "environment_supported_versions" } add ["pacticipants", :pacticipant_name, "versions", :pacticipant_version_number, "deployed-versions", "environment", :environment_uuid], Api::Resources::DeployedVersionsForVersionAndEnvironment, { resource_name: "deployed_versions_for_version_and_environment" } add ["pacticipants", :pacticipant_name, "versions", :pacticipant_version_number, "released-versions", "environment", :environment_uuid], Api::Resources::ReleasedVersionsForVersionAndEnvironment, { resource_name: "released_versions_for_version_and_environment" } add ["released-versions", :uuid], Api::Resources::ReleasedVersion, { resource_name: "released_version" } diff --git a/lib/pact_broker/api/decorators/deployed_version_decorator.rb b/lib/pact_broker/api/decorators/deployed_version_decorator.rb index 9aaecd231..ffac00d5b 100644 --- a/lib/pact_broker/api/decorators/deployed_version_decorator.rb +++ b/lib/pact_broker/api/decorators/deployed_version_decorator.rb @@ -7,13 +7,14 @@ module Api module Decorators class DeployedVersionDecorator < BaseDecorator property :uuid - property :version, :extend => EmbeddedVersionDecorator, writeable: false, embedded: true - property :environment, :extend => EnvironmentDecorator, writeable: false, embedded: true property :currently_deployed, camelize: true property :target, camelize: true include Timestamps property :undeployedAt, getter: lambda { |_| undeployed_at ? FormatDateTime.call(undeployed_at) : nil }, writeable: false + property :version, :extend => EmbeddedVersionDecorator, writeable: false, embedded: true + property :environment, :extend => EnvironmentDecorator, writeable: false, embedded: true + link :self do | user_options | { href: deployed_version_url(represented, user_options.fetch(:base_url)) diff --git a/lib/pact_broker/api/decorators/environment_decorator.rb b/lib/pact_broker/api/decorators/environment_decorator.rb index 16495a1a8..c8edf3a28 100644 --- a/lib/pact_broker/api/decorators/environment_decorator.rb +++ b/lib/pact_broker/api/decorators/environment_decorator.rb @@ -32,6 +32,13 @@ class EnvironmentDecorator < BaseDecorator } end + link :'pb:currently-supported-versions' do | user_options | + { + title: "Versions currently supported in #{represented.display_name} environment", + href: currently_supported_versions_for_environment_url(represented, user_options.fetch(:base_url)) + } + end + link :'pb:environments' do | user_options | { title: "Environments", diff --git a/lib/pact_broker/api/decorators/released_version_decorator.rb b/lib/pact_broker/api/decorators/released_version_decorator.rb index ab75bb5b6..b83bb8096 100644 --- a/lib/pact_broker/api/decorators/released_version_decorator.rb +++ b/lib/pact_broker/api/decorators/released_version_decorator.rb @@ -1,4 +1,6 @@ require "pact_broker/api/decorators/base_decorator" +require "pact_broker/api/decorators/embedded_version_decorator" +require "pact_broker/api/decorators/environment_decorator" module PactBroker module Api @@ -8,6 +10,15 @@ class ReleasedVersionDecorator < BaseDecorator property :currently_supported, camelize: true include Timestamps property :supportEndedAt, getter: lambda { |_| support_ended_at ? FormatDateTime.call(support_ended_at) : nil }, writeable: false + + property :version, :extend => EmbeddedVersionDecorator, writeable: false, embedded: true + property :environment, :extend => EnvironmentDecorator, writeable: false, embedded: true + + link :self do | user_options | + { + href: released_version_url(represented, user_options.fetch(:base_url)) + } + end end end end diff --git a/lib/pact_broker/api/pact_broker_urls.rb b/lib/pact_broker/api/pact_broker_urls.rb index aaecb837e..e545bef3a 100644 --- a/lib/pact_broker/api/pact_broker_urls.rb +++ b/lib/pact_broker/api/pact_broker_urls.rb @@ -322,6 +322,10 @@ def currently_deployed_versions_for_environment_url(environment, base_url = "") "#{base_url}/environments/#{environment.uuid}/currently-deployed-versions" end + def currently_supported_versions_for_environment_url(environment, base_url = "") + "#{base_url}/environments/#{environment.uuid}/currently-supported-versions" + end + def record_undeployment_url(deployed_version, base_url = "") "#{deployed_version_url(deployed_version, base_url)}/record-undeployment" end diff --git a/lib/pact_broker/api/resources/currently_deployed_versions_for_environment.rb b/lib/pact_broker/api/resources/currently_deployed_versions_for_environment.rb index 140322841..e793f9f93 100644 --- a/lib/pact_broker/api/resources/currently_deployed_versions_for_environment.rb +++ b/lib/pact_broker/api/resources/currently_deployed_versions_for_environment.rb @@ -25,11 +25,19 @@ def resource_exists? end def to_json - decorator_class(:deployed_versions_decorator).new(deployed_versions).to_json(decorator_options(title: title)) + decorator_class(decorator_name).new(deployed_versions).to_json(decorator_options(title: title)) end def policy_name - :'deployments::environments' + :'deployments::environment' + end + + def policy_record + environment + end + + def decorator_name + :deployed_versions_decorator end private diff --git a/lib/pact_broker/api/resources/currently_supported_versions_for_environment.rb b/lib/pact_broker/api/resources/currently_supported_versions_for_environment.rb new file mode 100644 index 000000000..1e93b639f --- /dev/null +++ b/lib/pact_broker/api/resources/currently_supported_versions_for_environment.rb @@ -0,0 +1,70 @@ +require "pact_broker/api/resources/base_resource" +require "pact_broker/api/decorators/versions_decorator" +require "pact_broker/string_refinements" + +module PactBroker + module Api + module Resources + class CurrentlySupportedVersionsForEnvironment < BaseResource + using PactBroker::StringRefinements + + def content_types_accepted + [["application/json", :from_json]] + end + + def content_types_provided + [["application/hal+json", :to_json]] + end + + def allowed_methods + ["GET", "OPTIONS"] + end + + def resource_exists? + !!environment + end + + def to_json + decorator_class(decorator_name).new(released_versions).to_json(decorator_options(title: title)) + end + + def policy_name + :'deployments::environment' + end + + def policy_record + environment + end + + def decorator_name + :released_versions_decorator + end + + private + + def environment + @environment ||= environment_service.find(environment_uuid) + end + + def released_versions + @released_versions ||= released_version_service.find_currently_supported_versions_for_environment(environment, query_params) + end + + def environment_uuid + identifier_from_path[:environment_uuid] + end + + def query_params + { + pacticipant_name: request.query["pacticipant"], + pacticipant_version_number: request.query["version"] + }.compact + end + + def title + "Currently supported versions in #{environment.display_name}" + end + end + end + end +end diff --git a/lib/pact_broker/deployments/released_version.rb b/lib/pact_broker/deployments/released_version.rb index ce434b57a..b4bae35b8 100644 --- a/lib/pact_broker/deployments/released_version.rb +++ b/lib/pact_broker/deployments/released_version.rb @@ -26,6 +26,10 @@ def for_pacticipant_name(pacticipant_name) where(pacticipant_id: db[:pacticipants].select(:id).where(name_like(:name, pacticipant_name))) end + def for_pacticipant_version_number(pacticipant_version_number) + where(version_id: db[:versions].select(:id).where(name_like(:number, pacticipant_version_number))) + end + def for_version_and_environment(version, environment) where(version_id: version.id, environment_id: environment.id) end diff --git a/lib/pact_broker/deployments/released_version_service.rb b/lib/pact_broker/deployments/released_version_service.rb index b1d3b7ca3..d91bef1d6 100644 --- a/lib/pact_broker/deployments/released_version_service.rb +++ b/lib/pact_broker/deployments/released_version_service.rb @@ -20,6 +20,15 @@ def self.create(uuid, version, environment) ).insert_ignore end + def self.find_currently_supported_versions_for_environment(environment, pacticipant_name: nil, pacticipant_version_number: nil) + query = ReleasedVersion + .currently_supported + .for_environment(environment) + query = query.for_pacticipant_name(pacticipant_name) if pacticipant_name + query = query.for_pacticipant_version_number(pacticipant_version_number) if pacticipant_version_number + query.all + end + def self.find_released_version_for_version_and_environment(version, environment) ReleasedVersion .for_version_and_environment(version, environment) diff --git a/lib/pact_broker/test/test_data_builder.rb b/lib/pact_broker/test/test_data_builder.rb index 9c387cbf9..13926d7ed 100644 --- a/lib/pact_broker/test/test_data_builder.rb +++ b/lib/pact_broker/test/test_data_builder.rb @@ -410,16 +410,22 @@ def create_deployed_version_for_consumer_version(uuid: SecureRandom.uuid, curren self end + def create_deployed_version_for_provider_version(uuid: SecureRandom.uuid, currently_deployed: true, environment_name: environment&.name, target: nil, created_at: nil) + create_deployed_version(uuid: uuid, currently_deployed: currently_deployed, version: provider_version, environment_name: environment_name, target: target, created_at: created_at) + self + end + def create_released_version_for_consumer_version(uuid: SecureRandom.uuid, currently_supported: true, environment_name: environment&.name, created_at: nil) create_released_version(uuid: uuid, currently_supported: currently_supported, version: consumer_version, environment_name: environment_name, created_at: created_at) self end - def create_deployed_version_for_provider_version(uuid: SecureRandom.uuid, currently_deployed: true, environment_name: environment&.name, target: nil, created_at: nil) - create_deployed_version(uuid: uuid, currently_deployed: currently_deployed, version: provider_version, environment_name: environment_name, target: target, created_at: created_at) + def create_released_version_for_provider_version(uuid: SecureRandom.uuid, currently_supported: true, environment_name: environment&.name, created_at: nil) + create_released_version(uuid: uuid, currently_supported: currently_supported, version: provider_version, environment_name: environment_name, created_at: created_at) self end + def create_everything_for_an_integration create_pact_with_verification("Foo", "1", "Bar", "2") .create_label("label") diff --git a/spec/features/get_currently_deployed_versions_for_environment_spec.rb b/spec/features/get_currently_deployed_versions_for_environment_spec.rb index 62db22be5..a3f76d0c9 100644 --- a/spec/features/get_currently_deployed_versions_for_environment_spec.rb +++ b/spec/features/get_currently_deployed_versions_for_environment_spec.rb @@ -10,13 +10,10 @@ .create_deployed_version_for_provider_version(environment_name: "test", target: "customer-1") .create_provider_version("5") .create_deployed_version_for_provider_version(environment_name: "test") - end let(:path) do - PactBroker::Api::PactBrokerUrls.currently_deployed_versions_for_environment_url( - test_environment - ) + PactBroker::Api::PactBrokerUrls.currently_deployed_versions_for_environment_url(test_environment) end let(:query_params) { {} }