-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add branch endpoint supporting GET and DELETE (#635)
- Loading branch information
Showing
14 changed files
with
246 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require "pact_broker/api/decorators/base_decorator" | ||
require "pact_broker/api/decorators/timestamps" | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class BranchDecorator < BaseDecorator | ||
|
||
property :name | ||
|
||
link :self do | user_options | | ||
{ | ||
title: "Branch", | ||
href: branch_url(represented, user_options.fetch(:base_url)) | ||
} | ||
end | ||
|
||
link "pb:latest-version" do | user_options | | ||
{ | ||
title: "Latest version for branch", | ||
href: branch_versions_url(represented, user_options.fetch(:base_url)) + "?pageSize=1" | ||
} | ||
end | ||
|
||
include Timestamps | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "pact_broker/api/resources/base_resource" | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
class Branch < BaseResource | ||
def content_types_provided | ||
[["application/hal+json", :to_json]] | ||
end | ||
|
||
def allowed_methods | ||
["GET", "DELETE", "OPTIONS"] | ||
end | ||
|
||
def resource_exists? | ||
!!branch | ||
end | ||
|
||
def to_json | ||
decorator_class(:branch_decorator).new(branch).to_json(**decorator_options) | ||
end | ||
|
||
def delete_resource | ||
branch_service.delete_branch(branch) | ||
true | ||
end | ||
|
||
def policy_name | ||
:'versions::branch' | ||
end | ||
|
||
private | ||
|
||
def branch | ||
@branch_version ||= branch_service.find_branch(**identifier_from_path.slice(:pacticipant_name, :branch_name)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module PactBroker | ||
module Versions | ||
class BranchRepository | ||
include PactBroker::Services | ||
|
||
# @param [String] pacticipant_name | ||
# @param [String] branch_name | ||
# @return [PactBroker::Versions::Branch, nil] | ||
def find_branch(pacticipant_name:, branch_name:) | ||
Branch | ||
.select_all_qualified | ||
.join(:pacticipants, { Sequel[:branches][:pacticipant_id] => Sequel[:pacticipants][:id] }) do | ||
Sequel.name_like(Sequel[:pacticipants][:name], pacticipant_name) | ||
end | ||
.where(Sequel[:branches][:name] => branch_name) | ||
.single_record | ||
end | ||
|
||
# Deletes a branch, its branch head and branch_version objects, without deleting the | ||
# pacticipant version objects | ||
# | ||
# @param [PactBroker::Versions::Branch] the branch to delete | ||
def delete_branch(branch) | ||
branch.delete | ||
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,46 @@ | ||
describe "Deleting a branch (removing all versions from a branch)" do | ||
before do | ||
td.create_consumer("foo") | ||
.create_consumer_version("1234", branch: "main") | ||
.create_consumer_version("1234", branch: "not-main") | ||
.create_consumer_version("555", branch: "main") | ||
.create_consumer("bar") | ||
.create_consumer_version("1234", branch: "main") | ||
end | ||
|
||
let(:path) { "/pacticipants/foo/branches/main" } | ||
let(:headers) { {} } | ||
let(:response_body) { JSON.parse(subject.body, symbolize_names: true) } | ||
|
||
subject { delete(path, nil, headers) } | ||
|
||
it "returns a 204 response" do | ||
expect(subject.status).to be 204 | ||
end | ||
|
||
it "deletes the branch" do | ||
expect { subject }.to change { PactBroker::Versions::Branch.count }.by(-1) | ||
end | ||
|
||
it "does not delete the pacticipant versions" do | ||
expect { subject }.to_not change { PactBroker::Domain::Version.count } | ||
end | ||
|
||
context "when the branch version does not exist" do | ||
let(:path) { "/pacticipants/waffle/branches/main" } | ||
|
||
its(:status) { is_expected.to eq 404 } | ||
end | ||
|
||
context "when there is some flag to indicate that the versions should be deleted too" do | ||
subject { delete(path, { deletedAssociatedVersions: true }, headers) } | ||
|
||
it "deletes the branch" do | ||
expect { subject }.to change { PactBroker::Versions::Branch.count }.by(-1) | ||
end | ||
|
||
it "DOES delete the pacticipant versions", pending: true do | ||
expect { subject }.to change { PactBroker::Domain::Version.count }.by(-2) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
describe "Get a branch" do | ||
before do | ||
td.create_consumer("Foo") | ||
.create_consumer_version("1234", branch: "main") | ||
end | ||
let(:path) { PactBroker::Api::PactBrokerUrls.branch_url(PactBroker::Versions::Branch.first) } | ||
let(:headers) { { "CONTENT_TYPE" => "application/json" } } | ||
|
||
subject { get(path, nil, headers) } | ||
|
||
it { is_expected.to be_a_hal_json_success_response } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "pact_broker/versions/branch_repository" | ||
|
||
module PactBroker | ||
module Versions | ||
describe BranchRepository do | ||
describe "delete_branch" do | ||
before do | ||
td.create_consumer("foo") | ||
.create_consumer_version("1", branch: "main") | ||
.create_consumer_version("2", branch: "main") | ||
.create_consumer_version("3", branch: "not-main") | ||
.create_consumer("bar") | ||
.create_consumer_version("1", branch: "main") | ||
end | ||
|
||
let(:branch) { BranchRepository.new.find_branch(pacticipant_name: "foo", branch_name: "main") } | ||
|
||
subject { BranchRepository.new.delete_branch(branch) } | ||
|
||
it "deletes the branch" do | ||
expect{ subject }.to change { Branch.count }.by(-1) | ||
end | ||
|
||
it "deletes the branch versions" do | ||
expect{ subject }.to change { BranchVersion.count }.by(-2) | ||
end | ||
|
||
it "deletes the branch head" do | ||
expect{ subject }.to change { BranchHead.count }.by(-1) | ||
end | ||
|
||
it "does not delete the versions" do | ||
expect{ subject }.to_not change { PactBroker::Domain::Version.count } | ||
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