diff --git a/lib/pact_broker/api/resources/deployed_versions_for_environment.rb b/lib/pact_broker/api/resources/deployed_versions_for_environment.rb new file mode 100644 index 000000000..6650aa874 --- /dev/null +++ b/lib/pact_broker/api/resources/deployed_versions_for_environment.rb @@ -0,0 +1,54 @@ +require 'pact_broker/api/resources/base_resource' +require 'pact_broker/api/decorators/versions_decorator' + +module PactBroker + module Api + module Resources + class DeployedVersionsForEnvironment < BaseResource + 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(:deployed_versions_decorator).new(deployed_versions).to_json(decorator_options(title: title)) + end + + def policy_name + :'versions::versions' + end + + private + + attr_reader :deployed_versions + + def environment + @environment ||= environment_service.find(environment_uuid) + end + + def deployed_versions + @deployed_versions ||= deployed_version_service.find_deployed_versions_for_environment(environment) + end + + def environment_uuid + identifier_from_path[:environment_uuid] + end + + def title + "Deployed versions for #{environment.display_name}" + end + end + end + end +end diff --git a/lib/pact_broker/api/resources/deployed_versions_for_version_and_environment.rb b/lib/pact_broker/api/resources/deployed_versions_for_version_and_environment.rb index 21c2f2c4d..c1948aea5 100644 --- a/lib/pact_broker/api/resources/deployed_versions_for_version_and_environment.rb +++ b/lib/pact_broker/api/resources/deployed_versions_for_version_and_environment.rb @@ -1,5 +1,4 @@ require 'pact_broker/api/resources/base_resource' -require 'pact_broker/configuration' require 'pact_broker/api/decorators/versions_decorator' module PactBroker diff --git a/lib/pact_broker/deployments/deployed_version.rb b/lib/pact_broker/deployments/deployed_version.rb index e31e44d1e..4d6267f76 100644 --- a/lib/pact_broker/deployments/deployed_version.rb +++ b/lib/pact_broker/deployments/deployed_version.rb @@ -35,6 +35,10 @@ def for_version_and_environment(version, environment) where(version_id: version.id, environment_id: environment.id) end + def for_environment(environment) + where(environment_id: environment.id) + end + def order_by_date_desc order(Sequel.desc(:created_at), Sequel.desc(:id)) end diff --git a/lib/pact_broker/deployments/deployed_version_service.rb b/lib/pact_broker/deployments/deployed_version_service.rb index 1b33030f2..ec94f997a 100644 --- a/lib/pact_broker/deployments/deployed_version_service.rb +++ b/lib/pact_broker/deployments/deployed_version_service.rb @@ -28,6 +28,13 @@ def self.find_deployed_versions_for_version_and_environment(version, environment .all end + def self.find_deployed_versions_for_environment(environment) + DeployedVersion + .for_environment(environment) + .order_by_date_desc + .all + end + def self.record_previous_version_undeployed(pacticipant, environment) DeployedVersion.last_deployed_version(pacticipant, environment)&.record_undeployed end