Skip to content

Commit

Permalink
feat: add endpoint to list pacticipant versions by branch
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 28, 2023
1 parent d430a4d commit 9b4e3f6
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
24 changes: 12 additions & 12 deletions lib/pact_broker/api/decorators/versions_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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 = ""
Expand Down
16 changes: 16 additions & 0 deletions lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/versions/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions spec/lib/pact_broker/versions/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions spec/support/all_routes_spec_support.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9b4e3f6

Please sign in to comment.