Skip to content

Commit

Permalink
feat: add API endpoint for can-i-deploy for latest tagged pacticipant…
Browse files Browse the repository at this point in the history
… version
  • Loading branch information
bethesque committed Sep 25, 2020
1 parent 463e9cf commit 88fdc60
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions lib/pact_broker/api/resources/can_i_deploy_pacticipant_version.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 88fdc60

Please sign in to comment.