Skip to content

Commit

Permalink
feat: support marking a released version as unsupported
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 10, 2021
1 parent 05d35d2 commit f6c4ee2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module Decorators
class ReleasedVersionDecorator < BaseDecorator
property :uuid
property :currently_supported, camelize: true

include Timestamps
property :supportEndedAt, getter: lambda { |_| support_ended_at ? FormatDateTime.call(support_ended_at) : nil }, writeable: false
end
end
end
Expand Down
42 changes: 41 additions & 1 deletion lib/pact_broker/api/resources/released_version.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
require "pact_broker/api/resources/base_resource"
require "pact_broker/api/decorators/released_version_decorator"
require "pact_broker/messages"

module PactBroker
module Api
module Resources
class ReleasedVersion < BaseResource
include PactBroker::Messages

def initialize
super
@currently_supported_param = params(default: {})[:currentlySupported]
end

def content_types_provided
[["application/hal+json", :to_json]]
end

def content_types_accepted
[
["application/merge-patch+json", :from_merge_patch_json]
]
end

def allowed_methods
["GET", "OPTIONS"]
["GET", "PATCH", "OPTIONS"]
end

def resource_exists?
Expand All @@ -21,6 +35,18 @@ def to_json
decorator_class(:released_version_decorator).new(released_version).to_json(decorator_options)
end

def from_merge_patch_json
if request.patch?
if resource_exists?
process_currently_supported_param
else
404
end
else
415
end
end

def policy_name
:'versions::version'
end
Expand All @@ -31,6 +57,20 @@ def policy_record

private

attr_reader :currently_supported_param

def process_currently_supported_param
if currently_supported_param == false
@released_version = released_version_service.record_version_support_ended(released_version)
response.body = to_json
elsif currently_supported_param == true
set_json_validation_error_messages(currentlySupported: [message("errors.validation.cannot_set_currently_supported_true")])
422
else
response.body = to_json
end
end

def released_version
@released_version ||= released_version_service.find_by_uuid(uuid)
end
Expand Down
7 changes: 6 additions & 1 deletion lib/pact_broker/deployments/released_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ def order_by_date_desc
end

def record_support_ended
update(support_ended_at: Sequel.datetime_class.now)
where(support_ended_at: nil).update(support_ended_at: Sequel.datetime_class.now)
end
end

def record_support_ended
self.class.where(id: id).record_support_ended
refresh
end

def currently_supported
support_ended_at == nil
end
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ en:
cannot_modify_version_branch: "The branch for a pacticipant version cannot be changed once set (currently '%{old_branch}', proposed value '%{new_branch}'). Your pact publication/verification publication configurations may be in conflict with each other if you are seeing this error. If the current branch value is incorrect, you must delete the pacticipant version resource at %{version_url} and publish the pacts/verification results again with a consistent branch name."
invalid_content_for_content_type: "The content could not be parsed as %{content_type}"
cannot_set_currently_deployed_true: The currentlyDeployed property cannot be set back to true. Please record a new deployment.
cannot_set_currently_supported_true: The currentlySupported property cannot be set back to true. Please record a new deployment.
duplicate_pacticipant: |
This is the first time a pact has been published for "%{new_name}".
The name "%{new_name}" is very similar to the following existing consumers/providers:
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/test/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ def create_released_version(uuid: , currently_supported: true, version:, environ
env = find_environment(environment_name)
@released_version = PactBroker::Deployments::ReleasedVersionService.create(uuid, version, env)
PactBroker::Deployments::ReleasedVersionService.record_version_support_ended(released_version) unless currently_supported
released_version.refresh
set_created_at_if_set(created_at, :released_versions, id: released_version.id)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/features/record_undeployment_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe "Get currently deployed versions for environment" do
RSpec.describe "Record undeployment" do
let!(:version) { td.create_consumer("Foo").create_consumer_version("1").and_return(:consumer_version) }
let!(:test_environment) { td.create_environment("test").and_return(:environment) }
let!(:deployed_version) do
Expand Down

0 comments on commit f6c4ee2

Please sign in to comment.