-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for delete-branch command (#151)
PACT-1466
- Loading branch information
Showing
10 changed files
with
399 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require "pact_broker/client/base_command" | ||
|
||
module PactBroker | ||
module Client | ||
module Branches | ||
class DeleteBranch < PactBroker::Client::BaseCommand | ||
|
||
NOT_SUPPORTED_MESSAGE_PACT_BROKER = "This version of the Pact Broker does not support deleting branches. Please upgrade to version 2.108.0 or later." | ||
NOT_SUPPORTED_MESSAGE_PACTFLOW = "This version of PactFlow does not support deleting branches. Please upgrade to the latest version." | ||
|
||
def initialize(params, options, pact_broker_client_options) | ||
super | ||
@pacticipant_name = params.fetch(:pacticipant) | ||
@branch_name = params.fetch(:branch) | ||
@error_when_not_found = params.fetch(:error_when_not_found) | ||
end | ||
|
||
def do_call | ||
check_if_command_supported | ||
@deleted_resource = branch_link.delete | ||
PactBroker::Client::CommandResult.new(success?, result_message) | ||
end | ||
|
||
private | ||
|
||
attr_reader :pacticipant_name, :branch_name, :error_when_not_found, :deleted_resource | ||
|
||
def branch_link | ||
index_resource._link("pb:pacticipant-branch").expand(pacticipant: pacticipant_name, branch: branch_name) | ||
end | ||
|
||
def check_if_command_supported | ||
unless index_resource.can?("pb:pacticipant-branch") | ||
raise PactBroker::Client::Error.new(is_pactflow? ? NOT_SUPPORTED_MESSAGE_PACTFLOW : NOT_SUPPORTED_MESSAGE_PACT_BROKER) | ||
end | ||
end | ||
|
||
def success? | ||
if deleted_resource.success? | ||
true | ||
elsif deleted_resource.response.status == 404 && !error_when_not_found | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
def result_message | ||
if deleted_resource.success? | ||
green("Successfully deleted branch #{branch_name} of pacticipant #{pacticipant_name}") | ||
elsif deleted_resource.response.status == 404 | ||
if error_when_not_found | ||
red("Could not delete branch #{branch_name} of pacticipant #{pacticipant_name} as it was not found") | ||
else | ||
green("Branch #{branch_name} of pacticipant #{pacticipant_name} not found") | ||
end | ||
else | ||
red(deleted_resource.response.raw_body) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module PactBroker | ||
module Client | ||
module CLI | ||
module BranchCommands | ||
def self.included(thor) | ||
thor.class_eval do | ||
method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that the branch belongs to." | ||
method_option :branch, required: true, desc: "The pacticipant branch name." | ||
method_option :error_when_not_found, type: :boolean, default: true, desc: "Raise an error if the branch that is to be deleted is not found." | ||
shared_authentication_options | ||
|
||
desc "delete-branch", "Deletes a pacticipant branch. Does not delete the versions or pacts/verifications associated with the branch, but does make the pacts inaccessible for verification via consumer versions selectors or WIP pacts." | ||
|
||
def delete_branch | ||
require "pact_broker/client/branches/delete_branch" | ||
|
||
validate_credentials | ||
params = { | ||
pacticipant: options.pacticipant, | ||
branch: options.branch, | ||
error_when_not_found: options.error_when_not_found | ||
} | ||
|
||
result = PactBroker::Client::Branches::DeleteBranch.call(params, {}, pact_broker_client_options) | ||
$stdout.puts result.message | ||
exit(1) unless result.success | ||
end | ||
|
||
no_commands do | ||
def validate_delete_branch_params | ||
raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.pacticipant.strip.size == 0 | ||
raise ::Thor::RequiredArgumentMissingError, "Pacticipant branch name cannot be blank" if options.branch.strip.size == 0 | ||
end | ||
end | ||
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
103 changes: 103 additions & 0 deletions
103
spec/lib/pact_broker/client/branches/delete_branch_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,103 @@ | ||
require "pact_broker/client/branches/delete_branch" | ||
|
||
module PactBroker | ||
module Client | ||
module Branches | ||
describe DeleteBranch do | ||
before do | ||
allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:sleep) | ||
allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:default_max_tries).and_return(1) | ||
end | ||
|
||
let(:params) do | ||
{ | ||
pacticipant: "Foo", | ||
branch: "main", | ||
error_when_not_found: error_when_not_found | ||
} | ||
end | ||
let(:options) do | ||
{ | ||
verbose: verbose | ||
} | ||
end | ||
let(:error_when_not_found) { true } | ||
let(:pact_broker_base_url) { "http://example.org" } | ||
let(:pact_broker_client_options) { { pact_broker_base_url: pact_broker_base_url } } | ||
let(:response_headers) { { "Content-Type" => "application/hal+json"} } | ||
let(:verbose) { false } | ||
|
||
before do | ||
stub_request(:get, "http://example.org/").to_return(status: 200, body: index_response_body, headers: response_headers) | ||
stub_request(:delete, "http://example.org/pacticipants/Foo/branches/main").to_return(status: delete_response_status, body: delete_response_body, headers: response_headers) | ||
end | ||
let(:delete_response_status) { 200 } | ||
|
||
let(:index_response_body) do | ||
{ | ||
"_links" => { | ||
"pb:pacticipant-branch" => { | ||
"href" => "http://example.org/pacticipants/{pacticipant}/branches/{branch}" | ||
} | ||
} | ||
}.to_json | ||
end | ||
|
||
let(:delete_response_body) do | ||
{ "some" => "error message" }.to_json | ||
end | ||
|
||
subject { DeleteBranch.call(params, options, pact_broker_client_options) } | ||
|
||
context "when the branch is deleted" do | ||
it "returns a success result" do | ||
expect(subject.success).to be true | ||
expect(subject.message).to include "Successfully deleted branch main of pacticipant Foo" | ||
end | ||
end | ||
|
||
context "when there is a non-404 error" do | ||
let(:delete_response_status) { 403 } | ||
|
||
it "returns an error result with the response body" do | ||
expect(subject.success).to be false | ||
expect(subject.message).to include "error message" | ||
end | ||
end | ||
|
||
context "when the branch is not found" do | ||
let(:delete_response_status) { 404 } | ||
|
||
context "when error_when_not_found is true" do | ||
it "returns an error" do | ||
expect(subject.success).to be false | ||
expect(subject.message).to include "Could not delete branch main of pacticipant Foo as it was not found" | ||
end | ||
end | ||
|
||
context "when error_when_not_found is false" do | ||
let(:error_when_not_found) { false } | ||
|
||
it "return a success" do | ||
expect(subject.success).to be true | ||
expect(subject.message).to include "Branch main of pacticipant Foo not found" | ||
end | ||
end | ||
end | ||
|
||
context "when deleting branches is not supported" do | ||
let(:index_response_body) do | ||
{ | ||
_links: {} | ||
}.to_json | ||
end | ||
|
||
it "returns an error" do | ||
expect(subject.success).to be false | ||
expect(subject.message).to include "not support" | ||
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
Oops, something went wrong.