Skip to content

Commit

Permalink
feat: add branch-pact-versions & latest-branch-pact-version
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
YOU54F committed Nov 15, 2024
1 parent f4c5e2d commit 35ba4d5
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 11 deletions.
6 changes: 4 additions & 2 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def self.build_api(application_context = PactBroker::ApplicationContext.default_
add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "version", :consumer_version_number, "diff", "previous-distinct"], Api::Resources::PactContentDiff, {resource_name: "previous_distinct_pact_version_diff"}
add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "version", :consumer_version_number, "diff", "version", :comparison_consumer_version], Api::Resources::PactContentDiff, {resource_name: "pact_version_diff_by_consumer_version"}
add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "pact-version", :pact_version_sha, "diff", "pact-version", :comparison_pact_version_sha], Api::Resources::PactContentDiff, {resource_name: "pact_version_diff_by_pact_version_sha"}
add ["pacts", "provider", :provider_name, "branch", :branch], Api::Resources::ProviderPactsForConsumerBranch, {resource_name: "branch_provider_pact_publications"}
add ["pacts", "provider", :provider_name, "branch", :branch_name], Api::Resources::ProviderPactsForConsumerBranch, {resource_name: "branch_provider_pact_publications"}
add ["pacts", "provider", :provider_name, "branch"], Api::Resources::ProviderPactsForConsumerBranch, {resource_name: "main_branch_provider_pact_publications"}

# Verifications
add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "pact-version", :pact_version_sha, "verification-results"], Api::Resources::Verifications, {resource_name: "verification_results"}
Expand All @@ -70,8 +71,9 @@ 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, "latest", :tag], Api::Resources::LatestProviderPacts, {resource_name: "latest_tagged_provider_pact_publications"}
add ["pacts", "provider", :provider_name, "latest", "branch", :branch], Api::Resources::LatestProviderPactsForBranch, {resource_name: "latest_branch_provider_pact_publications"}
add ["pacts", "latest"], Api::Resources::LatestPacts, {resource_name: "latest_pacts"}

# Pacts for verification
Expand Down
12 changes: 12 additions & 0 deletions lib/pact_broker/api/resources/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ class LatestProviderPactsForBranch < ProviderPacts
private

def pacts
pact_service.find_latest_pacts_for_provider_for_branch(provider_name, branch: identifier_from_path[:branch])
pact_service.find_latest_pacts_for_provider_by_consumer_branch(
provider_name,
branch_name: identifier_from_path[:branch_name],
main_branch: identifier_from_path[:branch_name] ? false : true
)
end

def resource_title
suffix = identifier_from_path[:branch] ? " with consumer version branch '#{identifier_from_path[:branch]}'" : ""
suffix = identifier_from_path[:branch_name] ? " with consumer version branch '#{identifier_from_path[:branch_name]}'" : ""
"Latest pact versions for the provider #{identifier_from_path[:provider_name]}#{suffix}"
end
end
Expand Down
10 changes: 9 additions & 1 deletion lib/pact_broker/api/resources/pact_versions_for_branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_pacts_for_provider_and_consumer_by_consumer_branch 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ def to_json
private

def pacts
pact_service.find_pact_versions_for_provider provider_name, branch: identifier_from_path[:branch]
pact_service.find_pacts_for_provider_by_consumer_branch(
provider_name,
branch_name: identifier_from_path[:branch_name],
main_branch: identifier_from_path[:branch_name] ? false : true
)
end

def resource_title
suffix = identifier_from_path[:branch] ? " with consumer version branch '#{identifier_from_path[:branch]}'" : ""
suffix = identifier_from_path[:branch_name] ? " with consumer version branch '#{identifier_from_path[:branch_name]}'" : ""
"All pact versions for the provider #{identifier_from_path[:provider_name]}#{suffix}"
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/pact_broker/doc/views/index/branch-pact-versions.markdown
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def latest_for_main_branches
.remove_overridden_revisions_from_complete_query
end

def for_main_branches
consumers_join = { Sequel[:pact_publications][:consumer_id] => Sequel[:consumers][:id] }
query = self
query
.join(:pacticipants, consumers_join, { table_alias: :consumers })
.remove_overridden_revisions_from_complete_query
end

def for_currently_deployed_versions(environment_name)
deployed_versions_join = {
Sequel[:pact_publications][:consumer_version_id] => Sequel[:deployed_versions][:version_id]
Expand Down
30 changes: 30 additions & 0 deletions lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,36 @@ def find_latest_pacts_for_provider provider_name, tag = nil
query.all.sort_by{ | p| p.consumer_name.downcase }.collect(&:to_head_pact)
end

def find_pacts_by_consumer_branch(provider_name, options = {})
consumer_name = options[:consumer]
latest = options.fetch(:latest, true)
branch = options[:branch_name]
main_branch = options.fetch(:main_branch, false)

query = scope_for(PactPublication)
.eager_for_domain_with_content
.for_provider_name(provider_name)

if consumer_name
query = query.for_consumer_name(consumer_name)
end

if main_branch
if latest
query = query.latest_for_main_branches
else
query = query.for_main_branches
end
else
if latest
query = query.latest_for_consumer_branch(branch)
else
query = query.for_branch_name(branch)
end
end
query.all.sort_by{ | p| p.consumer_name.downcase }.collect(&:to_head_pact)
end

def find_for_verification(provider_name, consumer_version_selectors)
PactsForVerificationRepository.new.find(provider_name, consumer_version_selectors)
end
Expand Down
16 changes: 14 additions & 2 deletions lib/pact_broker/pacts/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,20 @@ def find_latest_pacts_for_provider provider_name, options = {}
pact_repository.find_latest_pacts_for_provider provider_name, options[:tag]
end

def find_latest_pacts_for_provider_for_branch provider_name, options = {}
pact_repository.find_latest_pacts_for_provider provider_name, options[:branch]
def find_latest_pacts_for_provider_by_consumer_branch provider_name, options = {}
options[:latest] = true
pact_repository.find_pacts_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_pact_versions_for_provider provider_name, options = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Api
module Resources
describe LatestProviderPactsForBranch do
before do
allow(PactBroker::Pacts::Service).to receive(:find_latest_pacts_for_provider_for_branch).and_return(pacts)
allow(PactBroker::Pacts::Service).to receive(:find_latest_pacts_for_provider_by_consumer_branch).and_return(pacts)
allow(PactBroker::Api::Decorators::ProviderPactsDecorator).to receive(:new).and_return(decorator)
allow_any_instance_of(LatestProviderPactsForBranch).to receive(:resource_exists?).and_return(provider)
end
Expand All @@ -19,7 +19,7 @@ module Resources

context "with a branch" do
it "finds the pacts with a branch" do
expect(PactBroker::Pacts::Service).to receive(:find_latest_pacts_for_provider_for_branch).with("Bar", branch: "prod")
expect(PactBroker::Pacts::Service).to receive(:find_latest_pacts_for_provider_by_consumer_branch).with("Bar", branch_name: "prod", main_branch: false)
subject
end

Expand Down

0 comments on commit 35ba4d5

Please sign in to comment.