From 99ace433210d379a0290dffe079669c89a09d612 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Fri, 15 Nov 2024 17:13:29 +0000 Subject: [PATCH] feat: add get routes for pact via main branch --- lib/pact_broker/api.rb | 6 ++++-- lib/pact_broker/api/resources/index.rb | 2 +- lib/pact_broker/api/resources/pact_versions_for_branch.rb | 8 +++++++- .../index/latest-provider-pacts-with-branch.markdown | 2 +- lib/pact_broker/pacts/repository.rb | 2 +- lib/pact_broker/pacts/service.rb | 8 ++++++-- .../resources/latest_provider_pacts_for_branch_spec.rb | 2 +- 7 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/pact_broker/api.rb b/lib/pact_broker/api.rb index b9b68ba88..32b0e925f 100644 --- a/lib/pact_broker/api.rb +++ b/lib/pact_broker/api.rb @@ -33,6 +33,8 @@ def self.build_api(application_context = PactBroker::ApplicationContext.default_ add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "versions"], Api::Resources::PactVersions, {resource_name: "pact_publications"} add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "versions", :consumer_version_number], Api::Resources::Pact, {resource_name: "pact_publication", deprecated: true} # Not the standard URL, but keep for backwards compatibility add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "tag", :tag], Api::Resources::TaggedPactVersions, {resource_name: "tagged_pact_publications"} + add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "branch"], Api::Resources::PactVersionsForBranch, {resource_name: "pact_publications_for_main_branch"} + add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "branch", "latest"], Api::Resources::PactVersionsForBranch, {resource_name: "latest_pact_publications_for_main_branch"} add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "branch", :branch_name], Api::Resources::PactVersionsForBranch, {resource_name: "pact_publications_for_branch"} # Pacts @@ -71,8 +73,8 @@ def self.build_api(application_context = PactBroker::ApplicationContext.default_ add ["pacts", "provider", :provider_name, "tag", :tag], Api::Resources::ProviderPacts, {resource_name: "tagged_provider_pact_publications"} add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "latest-untagged"], Api::Resources::LatestPact, {resource_name: "latest_untagged_pact_publication", tag: :untagged} add ["pacts", "provider", :provider_name, "latest"], Api::Resources::LatestProviderPacts, {resource_name: "latest_provider_pact_publications"} - add ["pacts", "provider", :provider_name, "latest", "branch", :branch_name], Api::Resources::LatestProviderPactsForBranch, {resource_name: "latest_branch_provider_pact_publications"} - add ["pacts", "provider", :provider_name, "latest", "branch"], Api::Resources::LatestProviderPactsForBranch, {resource_name: "latest_main_branch_provider_pact_publications"} + add ["pacts", "provider", :provider_name, "branch", :branch_name, "latest"], Api::Resources::LatestProviderPactsForBranch, {resource_name: "latest_branch_provider_pact_publications"} + add ["pacts", "provider", :provider_name, "branch", "latest"], Api::Resources::LatestProviderPactsForBranch, {resource_name: "latest_main_branch_provider_pact_publications"} add ["pacts", "provider", :provider_name, "latest", :tag], Api::Resources::LatestProviderPacts, {resource_name: "latest_tagged_provider_pact_publications"} add ["pacts", "latest"], Api::Resources::LatestPacts, {resource_name: "latest_pacts"} diff --git a/lib/pact_broker/api/resources/index.rb b/lib/pact_broker/api/resources/index.rb index f0be159ff..593fa61c3 100644 --- a/lib/pact_broker/api/resources/index.rb +++ b/lib/pact_broker/api/resources/index.rb @@ -87,7 +87,7 @@ def links }, "pb:latest-provider-pacts-with-branch" => { - href: base_url + "/pacts/provider/{provider}/latest/branch/{branch}", + href: base_url + "/pacts/provider/{provider}/branch/{branch}/latest", title: "Latest pacts for provider with the specified branch", templated: true }, diff --git a/lib/pact_broker/api/resources/pact_versions_for_branch.rb b/lib/pact_broker/api/resources/pact_versions_for_branch.rb index f11c50382..ea01ee09a 100644 --- a/lib/pact_broker/api/resources/pact_versions_for_branch.rb +++ b/lib/pact_broker/api/resources/pact_versions_for_branch.rb @@ -26,7 +26,13 @@ def to_json end def pacts - @pacts ||= pact_service.find_pacts_for_provider_and_consumer_by_consumer_branch provider_name, consumer_name, branch_name: identifier_from_path[:branch_name] + @pacts ||= pact_service.find_pacts_for_provider_and_consumer_by_consumer_branch( + provider_name, + consumer_name, + branch_name: identifier_from_path[:branch_name], + main_branch: identifier_from_path[:branch_name] ? false : true, + latest: identifier_from_path[:resource_name] == "latest_pact_publications_for_main_branch" + ) end def delete_resource diff --git a/lib/pact_broker/doc/views/index/latest-provider-pacts-with-branch.markdown b/lib/pact_broker/doc/views/index/latest-provider-pacts-with-branch.markdown index 790eb6773..600f795b8 100644 --- a/lib/pact_broker/doc/views/index/latest-provider-pacts-with-branch.markdown +++ b/lib/pact_broker/doc/views/index/latest-provider-pacts-with-branch.markdown @@ -2,6 +2,6 @@ Allowed methods: `GET` -Path: `/pacts/provider/{provider}/latest/branch/{branch}` +Path: `/pacts/provider/{provider}/branch/{branch}/latest` Given a provider name and a consumer version branch name, this resource returns the latest pact for each consumer that has the specified branch. diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index 0bf40927a..548ebac2f 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -153,7 +153,7 @@ def find_latest_pacts_for_provider provider_name, tag = nil def find_pacts_by_consumer_branch(provider_name, options = {}) consumer_name = options[:consumer] - latest = options.fetch(:latest, true) + latest = options.fetch(:latest, false) branch = options[:branch_name] main_branch = options.fetch(:main_branch, false) diff --git a/lib/pact_broker/pacts/service.rb b/lib/pact_broker/pacts/service.rb index 0f4d393b1..8ef6f3e3d 100644 --- a/lib/pact_broker/pacts/service.rb +++ b/lib/pact_broker/pacts/service.rb @@ -101,16 +101,20 @@ def find_latest_pacts_for_provider_by_consumer_branch provider_name, options = { end def find_pacts_for_provider_by_consumer_branch provider_name, options = {} - options[:latest] = false pact_repository.find_pacts_by_consumer_branch provider_name, options end def find_pacts_for_provider_and_consumer_by_consumer_branch provider_name, consumer, options = {} - options[:latest] = false options[:consumer] = consumer pact_repository.find_pacts_by_consumer_branch provider_name, options end + def find_latest_pacts_for_provider_and_consumer_by_consumer_branch provider_name, consumer, options = {} + options[:consumer] = consumer + options[:latest] = true + pact_repository.find_pacts_by_consumer_branch provider_name, options + end + def find_pact_versions_for_provider provider_name, options = {} pact_repository.find_pact_versions_for_provider provider_name, options[:tag] end diff --git a/spec/lib/pact_broker/api/resources/latest_provider_pacts_for_branch_spec.rb b/spec/lib/pact_broker/api/resources/latest_provider_pacts_for_branch_spec.rb index d3c013ea5..4681bb736 100644 --- a/spec/lib/pact_broker/api/resources/latest_provider_pacts_for_branch_spec.rb +++ b/spec/lib/pact_broker/api/resources/latest_provider_pacts_for_branch_spec.rb @@ -12,7 +12,7 @@ module Resources let(:provider) { double("provider") } let(:pacts) { double("pacts") } - let(:path) { "/pacts/provider/Bar/latest/branch/prod" } + let(:path) { "/pacts/provider/Bar/branch/prod/latest" } let(:decorator) { instance_double("PactBroker::Api::Decorators::ProviderPactsDecorator") } subject { get path; last_response }