From c10f858b05458640a6ae780045b65589767b6c70 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Fri, 4 Oct 2024 17:11:02 +0100 Subject: [PATCH] feat: add branch-pact-versions & latest-branch-pact-version # Branch pact versions Allowed methods: `GET` Path: `/pacts/provider/{provider}/consumer/{consumer}/branch/{branch}` Lists all the pact versions with the specified consumer, provider and consumer version branch. # Latest Branch pact versions Allowed methods: `GET` Path: `/pacts/provider/{provider}/consumer/{consumer}/branch/{branch}/latest` Returns the latest pact version with the specified consumer, provider and consumer version branch. --- lib/pact_broker/api/resources/index.rb | 12 ++++++++++++ .../api/resources/pact_versions_for_branch.rb | 12 ++++++++++-- .../doc/views/index/branch-pact-versions.markdown | 7 +++++++ .../views/index/latest-branch-pact-version.markdown | 7 +++++++ lib/pact_broker/pacts/repository.rb | 13 +++++++++++++ lib/pact_broker/pacts/service.rb | 4 ++++ 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 lib/pact_broker/doc/views/index/branch-pact-versions.markdown create mode 100644 lib/pact_broker/doc/views/index/latest-branch-pact-version.markdown diff --git a/lib/pact_broker/api/resources/index.rb b/lib/pact_broker/api/resources/index.rb index 83877f91e..ce3ba089a 100644 --- a/lib/pact_broker/api/resources/index.rb +++ b/lib/pact_broker/api/resources/index.rb @@ -49,6 +49,18 @@ def links title: "All versions of a pact for a given consumer, provider and consumer version tag", templated: false }, + "pb:latest-branch-pact-version" => + { + href: base_url + "/pacts/provider/{provider}/consumer/{consumer}/branch/{branch}/latest", + title: "Latest version of a pact for a given consumer, provider and consumer version branch", + templated: false + }, + "pb:branch-pact-versions" => + { + href: base_url + "/pacts/provider/{provider}/consumer/{consumer}/branch/{branch}", + title: "All versions of a pact for a given consumer, provider and consumer version branch", + templated: false + }, "pb:pacticipants" => { href: base_url + "/pacticipants", 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 1b69dcd5e..099b9a677 100644 --- a/lib/pact_broker/api/resources/pact_versions_for_branch.rb +++ b/lib/pact_broker/api/resources/pact_versions_for_branch.rb @@ -1,7 +1,7 @@ require "pact_broker/api/resources/base_resource" require "pact_broker/configuration" -require "pact_broker/api/decorators/tagged_pact_versions_decorator" require "pact_broker/api/resources/pact_resource_methods" +require "pact_broker/api/decorators/pact_versions_decorator" module PactBroker module Api @@ -14,13 +14,21 @@ def content_types_provided end def allowed_methods - ["DELETE", "OPTIONS"] + ["GET", "DELETE", "OPTIONS"] end def resource_exists? consumer && provider end + def to_json + decorator_class(:pact_versions_decorator).new(pacts).to_json(**decorator_options(identifier_from_path)) + end + + def pacts + @pacts ||= pact_service.find_pact_versions_for_provider_and_consumer provider_name, consumer_name, branch_name: identifier_from_path[:branch_name] + end + def delete_resource pact_service.delete_all_pact_publications_between consumer_name, and: provider_name, branch_name: identifier_from_path[:branch_name] set_post_deletion_response diff --git a/lib/pact_broker/doc/views/index/branch-pact-versions.markdown b/lib/pact_broker/doc/views/index/branch-pact-versions.markdown new file mode 100644 index 000000000..6ffcbfad7 --- /dev/null +++ b/lib/pact_broker/doc/views/index/branch-pact-versions.markdown @@ -0,0 +1,7 @@ +# Branch pact versions + +Allowed methods: `GET` + +Path: `/pacts/provider/{provider}/consumer/{consumer}/branch/{branch}` + +Lists all the pact versions with the specified consumer, provider and consumer version branch. diff --git a/lib/pact_broker/doc/views/index/latest-branch-pact-version.markdown b/lib/pact_broker/doc/views/index/latest-branch-pact-version.markdown new file mode 100644 index 000000000..24582e50e --- /dev/null +++ b/lib/pact_broker/doc/views/index/latest-branch-pact-version.markdown @@ -0,0 +1,7 @@ +# Latest Branch pact versions + +Allowed methods: `GET` + +Path: `/pacts/provider/{provider}/consumer/{consumer}/branch/{branch}/latest` + +Returns the latest pact version with the specified consumer, provider and consumer version branch. diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index f1e05291d..087b59890 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -168,6 +168,19 @@ def find_pact_versions_for_provider provider_name, tag = nil query.all.collect(&:to_domain).sort end + def find_pact_versions_for_provider_and_consumer provider_name, consumer_name, branch_name = nil + query = scope_for(PactPublication) + .eager_for_domain_with_content + .select_all_qualified + .for_consumer_name(consumer_name) + .for_provider_name(provider_name) + .remove_overridden_revisions + if branch_name + query = query.old_latest_for_consumer_branch(branch_name) + end + query.latest_by_consumer_version_order.all.collect(&:to_domain_with_content) + end + # Returns latest pact version for the consumer_version_number def find_by_consumer_version consumer_name, consumer_version_number scope_for(PactPublication) diff --git a/lib/pact_broker/pacts/service.rb b/lib/pact_broker/pacts/service.rb index 6ec1adacb..8e4f26be5 100644 --- a/lib/pact_broker/pacts/service.rb +++ b/lib/pact_broker/pacts/service.rb @@ -99,6 +99,10 @@ def find_pact_versions_for_provider provider_name, options = {} pact_repository.find_pact_versions_for_provider provider_name, options[:tag] end + def find_pact_versions_for_provider_and_consumer provider_name, consumer_name, options = {} + pact_repository.find_pact_versions_for_provider_and_consumer provider_name, consumer_name, options[:branch_name] + end + def find_previous_distinct_pact_version params pact = find_pact params return nil if pact.nil?