Skip to content

Commit

Permalink
feat: feat: add provider pacts by branch endpoints
Browse files Browse the repository at this point in the history
      "pb:latest-provider-pacts-with-branch": {
        "href": "http://localhost:9292/pacts/provider/{provider}/latest/branch/{branch}",
        "title": "Latest pacts for provider with the specified branch",
        "templated": true
      },
      "pb:provider-pacts-with-branch": {
        "href": "http://localhost:9292/pacts/provider/{provider}/branch/{branch}",
        "title": "All pact versions for the provider with the specified consumer version branch",
        "templated": true
      },
  • Loading branch information
YOU54F committed Oct 4, 2024
1 parent 87669c3 commit f4c5e2d
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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"}

# Verifications
add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "pact-version", :pact_version_sha, "verification-results"], Api::Resources::Verifications, {resource_name: "verification_results"}
Expand Down Expand Up @@ -70,6 +71,7 @@ def self.build_api(application_context = PactBroker::ApplicationContext.default_
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", :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 @@ -73,12 +73,24 @@ def links
title: "Latest pacts for provider with the specified tag",
templated: true
},
"pb:latest-provider-pacts-with-branch" =>
{
href: base_url + "/pacts/provider/{provider}/latest/branch/{branch}",
title: "Latest pacts for provider with the specified branch",
templated: true
},
"pb:provider-pacts-with-tag" =>
{
href: base_url + "/pacts/provider/{provider}/tag/{tag}",
title: "All pact versions for the provider with the specified consumer version tag",
templated: true
},
"pb:provider-pacts-with-branch" =>
{
href: base_url + "/pacts/provider/{provider}/branch/{branch}",
title: "All pact versions for the provider with the specified consumer version branch",
templated: true
},
"pb:provider-pacts" =>
{
href: base_url + "/pacts/provider/{provider}",
Expand Down
22 changes: 22 additions & 0 deletions lib/pact_broker/api/resources/latest_provider_pacts_for_branch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "pact_broker/api/resources/provider_pacts"
require "pact_broker/configuration"
require "pact_broker/api/decorators/provider_pacts_decorator"

module PactBroker
module Api
module Resources
class LatestProviderPactsForBranch < ProviderPacts
private

def pacts
pact_service.find_latest_pacts_for_provider_for_branch(provider_name, branch: identifier_from_path[:branch])
end

def resource_title
suffix = identifier_from_path[:branch] ? " with consumer version branch '#{identifier_from_path[:branch]}'" : ""
"Latest pact versions for the provider #{identifier_from_path[:provider_name]}#{suffix}"
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "pact_broker/api/resources/base_resource"
require "pact_broker/configuration"
require "pact_broker/api/decorators/provider_pacts_decorator"

module PactBroker
module Api
module Resources
class ProviderPactsForConsumerBranch < BaseResource

def content_types_provided
[["application/hal+json", :to_json]]
end

def allowed_methods
["GET", "OPTIONS"]
end

def resource_exists?
!!provider
end

def policy_name
:'pacts::provider_pacts'
end

def to_json
decorator_class(:provider_pacts_decorator).new(pacts).to_json(**decorator_options(identifier_from_path.merge(title: resource_title)))
end

private

def pacts
pact_service.find_pact_versions_for_provider provider_name, branch: identifier_from_path[:branch]
end

def resource_title
suffix = identifier_from_path[:branch] ? " with consumer version branch '#{identifier_from_path[:branch]}'" : ""
"All pact versions for the provider #{identifier_from_path[:provider_name]}#{suffix}"
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Latest pacts for provider with the specified consumer branch

Allowed methods: `GET`

Path: `/pacts/provider/{provider}/latest/branch/{branch}`

Given a provider name and a consumer version branch name, this resource returns the latest pact for each consumer that has the specified branch.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Latest pacts for provider with the specified tag
# Latest pacts for provider with the specified consumer version tag

Allowed methods: `GET`

Path: `/pacts/provider/{provider}/latest/{tag}`

Given a provider name and a consumer version tag name, this resource returns the latest pact for each consumer that has the specified tag.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Provider pacts with consumer branch

Allowed methods: `GET`

Path: `/pacts/provider/{provider}/branch/{branch}`

Given a pacticipant name and a consumer branch, this resource returns all the pact versions for all consumers of this provider with the specified consumer branch. For most use cases, the `latest-provider-pacts-with-branch` relation will better serve consumer needs by only returning the latest pact version for specified consumer branches.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

Allowed methods: `GET`

Path: `/pacts/provider/{provider}/tag/{tag}`

Given a pacticipant name and a consumer version tag, this resource returns all the pact versions for all consumers of this provider with the specified tag. The most common use of this resource is to find all the `production` pact versions for the mobile consumers of an API, so that backwards compatibility can be maintained.
4 changes: 4 additions & 0 deletions lib/pact_broker/pacts/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ 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]
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
@@ -0,0 +1,36 @@
require "pact_broker/api/resources/latest_provider_pacts_for_branch"

module PactBroker
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::Api::Decorators::ProviderPactsDecorator).to receive(:new).and_return(decorator)
allow_any_instance_of(LatestProviderPactsForBranch).to receive(:resource_exists?).and_return(provider)
end

let(:provider) { double("provider") }
let(:pacts) { double("pacts") }
let(:path) { "/pacts/provider/Bar/latest/branch/prod" }
let(:decorator) { instance_double("PactBroker::Api::Decorators::ProviderPactsDecorator") }

subject { get path; last_response }

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")
subject
end

it "sets the correct resource title" do
expect(decorator).to receive(:to_json) do | options |
expect(options[:user_options][:title]).to eq "Latest pact versions for the provider Bar with consumer version branch 'prod'"
end
subject
end
end
end
end
end
end

0 comments on commit f4c5e2d

Please sign in to comment.