-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add endpoint to list branches for a pacticipant (#638)
PACT-1358
- Loading branch information
Showing
23 changed files
with
330 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
lib/pact_broker/api/decorators/pacticipant_branches_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require "pact_broker/api/decorators/base_decorator" | ||
require "pact_broker/api/decorators/timestamps" | ||
require "pact_broker/api/decorators/pagination_links" | ||
require "pact_broker/api/decorators/branch_decorator" | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class PacticipantBranchesDecorator < BaseDecorator | ||
collection :entries, as: :branches, embedded: true, :extend => PactBroker::Api::Decorators::BranchDecorator | ||
|
||
link :self do | user_options | | ||
{ | ||
title: "#{user_options.fetch(:pacticipant).name} branches", | ||
href: user_options.fetch(:request_url) | ||
} | ||
end | ||
|
||
links "pb:branches" do | user_options | | ||
represented.collect do | branch | | ||
{ | ||
name: branch.name, | ||
href: branch_url(branch, user_options.fetch(:base_url)) | ||
} | ||
end | ||
end | ||
|
||
include PaginationLinks | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require "pact_broker/api/resources/base_resource" | ||
require "pact_broker/api/resources/pagination_methods" | ||
require "pact_broker/api/resources/filter_methods" | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
class PacticipantBranches < BaseResource | ||
include PaginationMethods | ||
include FilterMethods | ||
|
||
def content_types_provided | ||
[["application/hal+json", :to_json]] | ||
end | ||
|
||
def allowed_methods | ||
["GET", "OPTIONS"] | ||
end | ||
|
||
def resource_exists? | ||
!!pacticipant | ||
end | ||
|
||
def to_json | ||
decorator_class(:pacticipant_branches_decorator).new(branches).to_json(**decorator_options(pacticipant: pacticipant)) | ||
end | ||
|
||
def policy_name | ||
:'versions::branches' | ||
end | ||
|
||
private | ||
|
||
def branches | ||
@branches ||= branch_service.find_all_branches_for_pacticipant( | ||
pacticipant, | ||
filter_options, | ||
default_pagination_options.merge(pagination_options), | ||
eager_load_associations | ||
) | ||
end | ||
|
||
def eager_load_associations | ||
decorator_class(:pacticipant_branches_decorator).eager_load_associations | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
describe "Get pacticipant branches" do | ||
before do | ||
td.create_consumer("Foo") | ||
.create_consumer_version("1", branch: "main") | ||
.create_consumer_version("2", branch: "feat/bar") | ||
.create_consumer_version("3", branch: "feat/foo") | ||
end | ||
let(:path) { PactBroker::Api::PactBrokerUrls.pacticipant_branches_url(td.and_return(:pacticipant)) } | ||
let(:headers) { { "CONTENT_TYPE" => "application/json" } } | ||
let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) } | ||
let(:params) { nil } | ||
|
||
subject { get(path, params, headers) } | ||
|
||
it { is_expected.to be_a_hal_json_success_response } | ||
|
||
it "returns a list of branches" do | ||
expect(response_body_hash[:_embedded][:branches].size).to eq 3 | ||
end | ||
|
||
it_behaves_like "a page" | ||
|
||
context "with pagination options" do | ||
subject { get(path, { "pageSize" => "2", "pageNumber" => "1" }) } | ||
|
||
it "only returns the number of items specified in the pageSize" do | ||
expect(response_body_hash[:_links][:"pb:branches"].size).to eq 2 | ||
end | ||
|
||
it_behaves_like "a paginated response" | ||
end | ||
|
||
context "with filter options" do | ||
let(:params) { { "q" => "feat" } } | ||
|
||
it "returns a list of branches matching the filter" do | ||
expect(response_body_hash[:_embedded][:branches].size).to eq 2 | ||
end | ||
end | ||
|
||
context "when the pacticipant does not exist" do | ||
let(:path) { PactBroker::Api::PactBrokerUrls.pacticipant_branches_url(OpenStruct.new(name: "Bar")) } | ||
|
||
its(:status) { is_expected.to eq 404 } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
spec/fixtures/approvals/pacticipant_branches_decorator.approved.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"_embedded": { | ||
"branches": [ | ||
{ | ||
"name": "main", | ||
"createdAt": "2020-01-01T00:00:00+00:00", | ||
"_links": { | ||
"self": { | ||
"title": "Branch", | ||
"href": "http://example.org/pacticipants/Foo/branches/main" | ||
}, | ||
"pb:latest-version": { | ||
"title": "Latest version for branch", | ||
"href": "http://example.org/pacticipants/Foo/branches/main/versions?pageSize=1" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"_links": { | ||
"self": { | ||
"title": "Foo branches", | ||
"href": "http://example.org/pacticipants/Foo/branches" | ||
}, | ||
"pb:branches": [ | ||
{ | ||
"name": "main", | ||
"href": "http://example.org/pacticipants/Foo/branches/main" | ||
} | ||
] | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
spec/lib/pact_broker/api/decorators/pacticipant_branches_decorator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require "pact_broker/api/decorators/pacticipant_branches_decorator" | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
describe PacticipantBranchesDecorator do | ||
it "ensures the pacticipant is eager loaded for the branches collection" do | ||
expect(PacticipantBranchesDecorator.eager_load_associations).to include :pacticipant | ||
end | ||
|
||
describe "to_json" do | ||
let(:branch_1) { instance_double("PactBroker::Versions::Branch", name: "main", pacticipant: pacticipant_1, created_at: td.in_utc { DateTime.new(2020, 1, 1) } ) } | ||
let(:pacticipant_1) { instance_double("PactBroker::Domain::Pacticipant", name: "Foo") } | ||
let(:branches) { [branch_1] } | ||
let(:options) do | ||
{ | ||
user_options: { | ||
pacticipant: pacticipant_1, | ||
base_url: "http://example.org", | ||
request_url: "http://example.org/pacticipants/Foo/branches" | ||
} | ||
} | ||
end | ||
let(:decorator) { PacticipantBranchesDecorator.new(branches) } | ||
|
||
subject { JSON.parse(decorator.to_json(options)) } | ||
|
||
it "generates json" do | ||
Approvals.verify(subject, :name => "pacticipant_branches_decorator", format: :json) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.