From 9bfafc8af4714d60423e11b697d27c7c1dd6f155 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 15 Oct 2021 10:05:30 +1100 Subject: [PATCH] feat: add applicationInstance to deployed version resource as a replacement for target --- .../decorators/deployed_version_decorator.rb | 3 +- ...ently_deployed_versions_for_environment.rb | 4 ++- ...ed_versions_for_version_and_environment.rb | 7 +++-- ..._deployed_versions_for_environment_spec.rb | 29 +++++++++++++++---- spec/features/record_deployment_spec.rb | 21 +++++++++++--- 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/lib/pact_broker/api/decorators/deployed_version_decorator.rb b/lib/pact_broker/api/decorators/deployed_version_decorator.rb index 32e0d755b..0d8c3fed2 100644 --- a/lib/pact_broker/api/decorators/deployed_version_decorator.rb +++ b/lib/pact_broker/api/decorators/deployed_version_decorator.rb @@ -9,7 +9,8 @@ module Decorators class DeployedVersionDecorator < BaseDecorator property :uuid property :currently_deployed, camelize: true - property :target, camelize: true + property :target, camelize: true # deprecated + property :applicationInstance, getter: lambda { |_| target } include Timestamps property :undeployedAt, getter: lambda { |_| undeployed_at ? FormatDateTime.call(undeployed_at) : nil }, writeable: false 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 e793f9f93..45a1f48a3 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 @@ -60,7 +60,9 @@ def query_params query = Rack::Utils.parse_query(request.env["QUERY_STRING"]) q = {} q[:pacticipant_name] = request.query["pacticipant"] if query["pacticipant"] - if query["target"] + if query["applicationInstance"] + q[:target] = query["applicationInstance"].blank? ? nil : query["applicationInstance"] + elsif query["target"] q[:target] = query["target"].blank? ? nil : query["target"] end q 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 4321d5679..80303b0f4 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 @@ -30,7 +30,7 @@ def create_path end def from_json - @deployed_version = deployed_version_service.find_or_create(deployed_version_uuid, version, environment, target) + @deployed_version = deployed_version_service.find_or_create(deployed_version_uuid, version, environment, application_instance) response.headers["Location"] = deployed_version_url(deployed_version, base_url) response.body = decorator_class(:deployed_version_decorator).new(deployed_version).to_json(decorator_options) end @@ -72,8 +72,9 @@ def deployed_version_uuid end # TODO disallow an empty string because that is used as a NULL indicator in the database - def target - params(default: {})[:target]&.to_s + def application_instance + parameters = params(default: {}) + (parameters[:applicationInstance] || parameters[:target])&.to_s end def title 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 a3f76d0c9..f33ac00bd 100644 --- a/spec/features/get_currently_deployed_versions_for_environment_spec.rb +++ b/spec/features/get_currently_deployed_versions_for_environment_spec.rb @@ -29,9 +29,8 @@ end context "with query params" do - - context "with a pacticipant name and target" do - let(:query_params) { { pacticipant: "Bar", target: "customer-1" } } + context "with a pacticipant name and applicationInstance" do + let(:query_params) { { pacticipant: "Bar", applicationInstance: "customer-1" } } it "returns a list of matching deployed versions" do expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 1 @@ -39,8 +38,8 @@ end end - context "with pacticipant name and a blank target" do - let(:query_params) { { pacticipant: "Bar", target: "" } } + context "with pacticipant name and a blank applicationInstance" do + let(:query_params) { { pacticipant: "Bar", applicationInstance: "" } } it "returns a list of matching deployed versions" do expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 1 @@ -48,12 +47,30 @@ end end - context "with pacticipant name and no target" do + context "with pacticipant name and no target or applicationInstance" do let(:query_params) { { pacticipant: "Bar" } } it "returns a list of matching deployed versions" do expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 2 end end + + context "with a pacticipant name and target (deprecated)" do + let(:query_params) { { pacticipant: "Bar", target: "customer-1" } } + + it "returns a list of matching deployed versions" do + expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 1 + expect(response_body_hash[:_embedded][:deployedVersions].first[:_embedded][:version][:number]).to eq "4" + end + end + + context "with pacticipant name and a blank target (deprecated)" do + let(:query_params) { { pacticipant: "Bar", target: "" } } + + it "returns a list of matching deployed versions" do + expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 1 + expect(response_body_hash[:_embedded][:deployedVersions].first[:_embedded][:version][:number]).to eq "5" + end + end end end diff --git a/spec/features/record_deployment_spec.rb b/spec/features/record_deployment_spec.rb index 08769b2df..e5210347e 100644 --- a/spec/features/record_deployment_spec.rb +++ b/spec/features/record_deployment_spec.rb @@ -14,13 +14,13 @@ let(:response_body) { JSON.parse(subject.body, symbolize_names: true) } let(:version_path) { "/pacticipants/Foo/versions/2" } let(:version_response) { get(version_path, nil, { "HTTP_ACCEPT" => "application/hal+json" } ) } - let(:target) { nil } + let(:application_instance) { nil } let(:path) do JSON.parse(version_response.body)["_links"]["pb:record-deployment"] .find{ |relation| relation["name"] == "test" } .fetch("href") end - let(:request_body) { { target: target }.to_json } + let(:request_body) { { applicationInstance: application_instance }.to_json } subject { post(path, request_body, headers) } @@ -33,6 +33,7 @@ it "returns the newly created deployment" do expect(response_body[:currentlyDeployed]).to be true expect(response_body).to_not have_key(:target) + expect(response_body).to_not have_key(:application_instance) end it "creates a new deployed version" do @@ -79,8 +80,20 @@ it { is_expected.to be_a_hal_json_created_response } end - context "when the deployment is to a different target" do - let(:target) { "foo" } + context "when the deployment is to a different application instance" do + let(:application_instance) { "foo" } + + it "creates a new deployed version" do + expect { subject }.to change { PactBroker::Deployments::DeployedVersion.currently_deployed.count }.by(1) + end + + it "sets the applicationInstance" do + expect(response_body).to have_key(:applicationInstance) + end + end + + context "when the deployment is to a different target (deprecated)" do + let(:request_body) { { target: "foo" }.to_json } it "creates a new deployed version" do expect { subject }.to change { PactBroker::Deployments::DeployedVersion.currently_deployed.count }.by(1)