Skip to content

Commit

Permalink
feat: add endpoint to list currently supported versions for an enviro…
Browse files Browse the repository at this point in the history
…nment
  • Loading branch information
bethesque committed Jun 11, 2021
1 parent f6c4ee2 commit 9608be8
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 3 additions & 2 deletions lib/pact_broker/api/decorators/deployed_version_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions lib/pact_broker/api/decorators/environment_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions lib/pact_broker/api/decorators/released_version_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/pact_broker/deployments/released_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/pact_broker/deployments/released_version_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions lib/pact_broker/test/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { {} }
Expand Down

0 comments on commit 9608be8

Please sign in to comment.