Skip to content

Commit

Permalink
feat: add get routes for pact via main branch
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Nov 15, 2024
1 parent 35ba4d5 commit 99ace43
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}

Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
8 changes: 7 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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 6 additions & 2 deletions lib/pact_broker/pacts/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 99ace43

Please sign in to comment.