From 9b4e3f61e7cca3332cea215981d4043be08e1226 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 28 Jul 2023 13:21:25 +1000 Subject: [PATCH] feat: add endpoint to list pacticipant versions by branch --- lib/pact_broker/api.rb | 1 + .../api/decorators/versions_decorator.rb | 24 ++++++------- lib/pact_broker/api/pact_broker_urls.rb | 6 +++- lib/pact_broker/versions/repository.rb | 16 +++++++++ lib/pact_broker/versions/service.rb | 4 +++ .../pact_broker/versions/repository_spec.rb | 35 +++++++++++++++++++ spec/support/all_routes_spec_support.yml | 1 + 7 files changed, 74 insertions(+), 13 deletions(-) diff --git a/lib/pact_broker/api.rb b/lib/pact_broker/api.rb index 478111cb3..ba98e6c99 100644 --- a/lib/pact_broker/api.rb +++ b/lib/pact_broker/api.rb @@ -88,6 +88,7 @@ def self.build_api(application_context = PactBroker::ApplicationContext.default_ # Versions add ["pacticipants", :pacticipant_name, "versions"], Api::Resources::Versions, {resource_name: "pacticipant_versions"} + add ["pacticipants", :pacticipant_name, "branches", :branch_name, "versions"], Api::Resources::BranchVersions, {resource_name: "pacticipant_branch_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::LatestVersion, {resource_name: "latest_tagged_pacticipant_version"} add ["pacticipants", :pacticipant_name, "latest-version", :tag, "can-i-deploy", "to", :to], Api::Resources::CanIDeployPacticipantVersionByTagToTag, { resource_name: "can_i_deploy_latest_tagged_version_to_tag" } diff --git a/lib/pact_broker/api/decorators/versions_decorator.rb b/lib/pact_broker/api/decorators/versions_decorator.rb index 679446f37..5637f7b50 100644 --- a/lib/pact_broker/api/decorators/versions_decorator.rb +++ b/lib/pact_broker/api/decorators/versions_decorator.rb @@ -9,43 +9,43 @@ class VersionsDecorator < BaseDecorator collection :entries, as: :versions, embedded: true, :extend => PactBroker::Api::Decorators::VersionDecorator - link :self do | context | - href = append_query_if_present(context[:resource_url], context[:query_string]) + link :self do | user_options | + href = append_query_if_present(user_options[:resource_url], user_options[:query_string]) { href: href, - title: "All application versions of #{context[:pacticipant_name]}" + title: user_options[:resource_title] || "All application versions of #{user_options[:pacticipant_name]}" } end include PaginationLinks - link :'pb:pacticipant' do | context | + link :'pb:pacticipant' do | user_options | { - href: pacticipant_url(context[:base_url], OpenStruct.new(name: context[:pacticipant_name])), - title: context[:pacticipant_name] + href: pacticipant_url(user_options[:base_url], OpenStruct.new(name: user_options[:pacticipant_name])), + title: user_options[:pacticipant_name] } end - links :'pb:versions' do | context | + links :'pb:versions' do | user_options | represented.collect do | version | { - :href => version_url(context[:base_url], version), + :href => version_url(user_options[:base_url], version), :title => version.version_and_updated_date } end end - link :pacticipant do | context | + link :pacticipant do | user_options | { - href: pacticipant_url(context[:base_url], OpenStruct.new(name: context[:pacticipant_name])), + href: pacticipant_url(user_options[:base_url], OpenStruct.new(name: user_options[:pacticipant_name])), title: "Deprecated - please use pb:pacticipant" } end - links :'versions' do | context | + links :'versions' do | user_options | represented.collect do | version | { - :href => version_url(context[:base_url], version), + :href => version_url(user_options[:base_url], version), :title => "Deprecated - please use pb:versions" } end diff --git a/lib/pact_broker/api/pact_broker_urls.rb b/lib/pact_broker/api/pact_broker_urls.rb index 397e9b708..dbff67645 100644 --- a/lib/pact_broker/api/pact_broker_urls.rb +++ b/lib/pact_broker/api/pact_broker_urls.rb @@ -227,8 +227,12 @@ def tag_url base_url, tag "#{tags_url(base_url, tag.version)}/#{url_encode(tag.name)}" end + def branch_versions_url(branch, base_url = "") + "#{pacticipant_url(base_url, branch.pacticipant)}/branches/#{url_encode(branch.name)}/versions" + end + def branch_version_url(branch_version, base_url = "") - "#{pacticipant_url(base_url, branch_version.pacticipant)}/branches/#{url_encode(branch_version.branch_name)}/versions/#{url_encode(branch_version.version_number)}" + "#{branch_versions_url(branch_version.branch, base_url)}/#{url_encode(branch_version.version_number)}" end def templated_tag_url_for_pacticipant pacticipant_name, base_url = "" diff --git a/lib/pact_broker/versions/repository.rb b/lib/pact_broker/versions/repository.rb index 06cf10eda..300532f96 100644 --- a/lib/pact_broker/versions/repository.rb +++ b/lib/pact_broker/versions/repository.rb @@ -70,6 +70,22 @@ def find_all_pacticipant_versions_in_reverse_order name, pagination_options = {} query.all_with_pagination_options(pagination_options) end + def find_pacticipant_versions_in_reverse_order(pacticipant_name, options = {}, pagination_options = {}) + pacticipant = pacticipant_repository.find_by_name!(pacticipant_name) + query = PactBroker::Domain::Version + .where(pacticipant: pacticipant) + .eager(:pacticipant) + .eager(branch_versions: [:version, :branch_head, { branch: :pacticipant }]) + .eager(tags: :head_tag) + .eager(:pact_publications) + .reverse_order(:order) + + if options[:branch_name] + query = query.where_branch_name(options[:branch_name]) + end + query.all_with_pagination_options(pagination_options) + end + # There may be a race condition if two simultaneous requests come in to create the same version def create(args) version_params = { diff --git a/lib/pact_broker/versions/service.rb b/lib/pact_broker/versions/service.rb index 5dae43d05..439133509 100644 --- a/lib/pact_broker/versions/service.rb +++ b/lib/pact_broker/versions/service.rb @@ -30,6 +30,10 @@ def self.find_all_pacticipant_versions_in_reverse_order(name, pagination_options version_repository.find_all_pacticipant_versions_in_reverse_order(name, pagination_options) end + def self.find_pacticipant_versions_in_reverse_order(pacticipant_name, options, pagination_options = {}) + version_repository.find_pacticipant_versions_in_reverse_order(pacticipant_name, options, pagination_options) + end + def self.create_or_overwrite(pacticipant_name, version_number, version) pacticipant = pacticipant_repository.find_by_name_or_create(pacticipant_name) version = version_repository.create_or_overwrite(pacticipant, version_number, version) diff --git a/spec/lib/pact_broker/versions/repository_spec.rb b/spec/lib/pact_broker/versions/repository_spec.rb index 4ec97e317..2bbc0eafb 100644 --- a/spec/lib/pact_broker/versions/repository_spec.rb +++ b/spec/lib/pact_broker/versions/repository_spec.rb @@ -49,6 +49,41 @@ module Versions end end + describe "#find_pacticipant_versions_in_reverse_order" do + before do + td + .create_consumer("Foo") + .create_consumer_version("1", branch: "main") + .create_consumer_version("2", branch: "foo") + .create_consumer_version("3", branch: "main") + .create_consumer("Bar") + .create_consumer_version("5", branch: "main") + end + + let(:options) { {} } + subject { Repository.new.find_pacticipant_versions_in_reverse_order("Foo", options) } + + it "returns all the application versions for the given consumer" do + expect(subject.collect(&:number)).to eq ["3", "2", "1"] + end + + context "with a branch_name in the options" do + let(:options) { { branch_name: "main"} } + + it "returns only the versions for the branch" do + expect(subject.collect(&:number)).to eq ["3", "1"] + end + end + + context "with pagination options" do + subject { Repository.new.find_pacticipant_versions_in_reverse_order "Foo", options, { page_number: 1, page_size: 1 } } + + it "paginates the query" do + expect(subject.collect(&:number)).to eq ["3"] + end + end + end + describe "#find_by_pacticipant_name_and_latest_tag" do before do td.create_consumer("Bar") diff --git a/spec/support/all_routes_spec_support.yml b/spec/support/all_routes_spec_support.yml index 5a13a9ccb..a40240834 100644 --- a/spec/support/all_routes_spec_support.yml +++ b/spec/support/all_routes_spec_support.yml @@ -49,6 +49,7 @@ requests_which_are_exected_to_have_no_policy_record: - pacticipant_version_tag GET - pacticipant_version_tag PUT - pacticipant_version_tag DELETE + - pacticipant_branch_versions GET - pacticipants_for_label GET - latest_pacts GET - provider_pact_publications GET