From 88fdc60a90922a9a5b5ac9b269d046e743e0770f Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 25 Sep 2020 12:58:03 +1000 Subject: [PATCH] feat: add API endpoint for can-i-deploy for latest tagged pacticipant version --- lib/pact_broker/api.rb | 1 + lib/pact_broker/api/pact_broker_urls.rb | 6 ++- .../can_i_deploy_pacticipant_version.rb | 40 +++++++++++++++++++ .../can_i_deploy_pacticipant_version_spec.rb | 31 ++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 lib/pact_broker/api/resources/can_i_deploy_pacticipant_version.rb create mode 100644 spec/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version_spec.rb diff --git a/lib/pact_broker/api.rb b/lib/pact_broker/api.rb index 61c73b0c3..6f24d0f4e 100644 --- a/lib/pact_broker/api.rb +++ b/lib/pact_broker/api.rb @@ -71,6 +71,7 @@ module PactBroker add ['pacticipants', :pacticipant_name, 'versions'], Api::Resources::Versions, {resource_name: "pacticipant_versions"} add ['pacticipants', :pacticipant_name, 'versions', :pacticipant_version_number], Api::Resources::Version, {resource_name: "pacticipant_version"} add ['pacticipants', :pacticipant_name, 'latest-version', :tag], Api::Resources::Version, {resource_name: "latest_tagged_pacticipant_version"} + add ['pacticipants', :pacticipant_name, 'latest-version', :tag, 'can-i-deploy', 'to', :to], Api::Resources::CanIDeployPacticipantVersion, { resource_name: "can_i_deploy_latest_tagged_version" } add ['pacticipants', :pacticipant_name, 'latest-version', :tag, 'can-i-deploy', 'to', :to, 'badge'], Api::Resources::CanIDeployBadge, { resource_name: "can_i_deploy_badge" } add ['pacticipants', :pacticipant_name, 'latest-version'], Api::Resources::Version, {resource_name: "latest_pacticipant_version"} add ['pacticipants', :pacticipant_name, 'versions', :pacticipant_version_number, 'tags', :tag_name], Api::Resources::Tag, {resource_name: "pacticipant_version_tag"} diff --git a/lib/pact_broker/api/pact_broker_urls.rb b/lib/pact_broker/api/pact_broker_urls.rb index 917c9bb9b..dccb837e7 100644 --- a/lib/pact_broker/api/pact_broker_urls.rb +++ b/lib/pact_broker/api/pact_broker_urls.rb @@ -204,8 +204,12 @@ def templated_label_url_for_pacticipant pacticipant_name, base_url = "" pacticipant_url_from_params({ pacticipant_name: pacticipant_name }, base_url) + "/labels/{label}" end + def templated_can_i_deploy_url pacticipant_name, base_url = "" + pacticipant_url_from_params({ pacticipant_name: pacticipant_name }, base_url) + "/latest-version/{tag}/can-i-deploy/to/{environmentTag}" + end + def templated_can_i_deploy_badge_url pacticipant_name, base_url = "" - pacticipant_url_from_params({ pacticipant_name: pacticipant_name }, base_url) + "/latest-version/{tag}/can-i-deploy/to/{environmentTag}/badge" + templated_can_i_deploy_url(pacticipant_name, base_url) + "/badge" end def label_url label, base_url diff --git a/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version.rb b/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version.rb new file mode 100644 index 000000000..6660ebe53 --- /dev/null +++ b/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version.rb @@ -0,0 +1,40 @@ +require 'pact_broker/api/resources/matrix' +require 'pact_broker/matrix/can_i_deploy_query_schema' +require 'pact_broker/matrix/parse_can_i_deploy_query' + +module PactBroker + module Api + module Resources + class CanIDeployPacticipantVersion < Matrix + def initialize + super + selector = PactBroker::Matrix::UnresolvedSelector.new(pacticipant_name: pacticipant_name, latest: true, tag: identifier_from_path[:tag]) + @options = { + latestby: 'cvp', + latest: true, + tag: identifier_from_path[:to] + } + @selectors = [selector] + end + + def resource_exists? + !!version + end + + def policy_name + :'matrix::can_i_deploy' + end + + private + + def version + @version ||= version_service.find_by_pacticipant_name_and_latest_tag(identifier_from_path[:pacticipant_name], identifier_from_path[:tag]) + end + + def results + @results ||= matrix_service.find(selectors, options) + end + end + end + end +end diff --git a/spec/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version_spec.rb b/spec/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version_spec.rb new file mode 100644 index 000000000..e59a823d4 --- /dev/null +++ b/spec/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version_spec.rb @@ -0,0 +1,31 @@ +require 'pact_broker/api/resources/can_i_deploy_pacticipant_version' +require 'pact_broker/matrix/service' + +module PactBroker + module Api + module Resources + describe CanIDeployPacticipantVersion do + include_context "stubbed services" + + before do + allow(PactBroker::Matrix::Service).to receive(:find).and_return([]) + allow(pacticipant_service).to receive(:find_pacticipant_by_name).and_return(pacticipant) + allow(version_service).to receive(:find_by_pacticipant_name_and_latest_tag).and_return(version) + allow(PactBroker::Api::Decorators::MatrixDecorator).to receive(:new).and_return(decorator) + end + + let(:pacticipant) { double('pacticipant') } + let(:version) { double('version') } + let(:path) { "/pacticipants/Foo/latest-version/main/can-i-deploy/to/prod" } + let(:json_response_body) { JSON.parse(subject.body, symbolize_names: true) } + let(:decorator) { double('decorator', to_json: 'response_body') } + let(:selectors) { double('selectors') } + let(:options) { double('options') } + + subject { get(path, nil, 'Content-Type' => 'application/hal+json') } + + it { is_expected.to be_a_hal_json_success_response } + end + end + end +end