diff --git a/lib/pact_broker/api/resources/branch_version.rb b/lib/pact_broker/api/resources/branch_version.rb index c025c2917..b9442c217 100644 --- a/lib/pact_broker/api/resources/branch_version.rb +++ b/lib/pact_broker/api/resources/branch_version.rb @@ -14,7 +14,7 @@ def content_types_accepted end def allowed_methods - ["GET", "PUT", "OPTIONS"] + ["GET", "PUT", "DELETE", "OPTIONS"] end def resource_exists? @@ -25,6 +25,11 @@ def to_json decorator_class(:branch_version_decorator).new(branch_version).to_json(decorator_options) end + def delete_resource + branch_service.delete_branch_version(branch_version) + true + end + def from_json already_existed = !!branch_version @branch_version = branch_service.find_or_create_branch_version(identifier_from_path) diff --git a/lib/pact_broker/doc/views/index/pacticipant-branch-version.markdown b/lib/pact_broker/doc/views/index/pacticipant-branch-version.markdown index 5e98a7fe4..e00796af4 100644 --- a/lib/pact_broker/doc/views/index/pacticipant-branch-version.markdown +++ b/lib/pact_broker/doc/views/index/pacticipant-branch-version.markdown @@ -1,14 +1,25 @@ # Pacticipant branch version -Allowed methods: `GET`, `PUT` +Allowed methods: `GET`, `PUT`, `DELETE` Path: `/pacticipants/{pacticipant}/branches/{branch}/versions/{version}` Get or add/create a pacticipant version for a branch. -## Example +## Create + +### Example Add a version to a branch. The pacticipant and branch are automatically created if they do not exist. curl -XPUT http://broker/pacticipants/Bar/branches/main/versions/1e70030c6579915e5ff56b107a0fd25cf5df7464 \ -H "Content-Type: application/json" -d "{}" + + +## Delete + +Removes a pacticipant version from a branch. Does not delete the actual pacticipant version. + +Send a `DELETE` request to the branch version resource. + + curl -XDELETE http://broker/pacticipants/Bar/branches/main/versions/1e70030c6579915e5ff56b107a0fd25cf5df7464 diff --git a/lib/pact_broker/versions/branch_service.rb b/lib/pact_broker/versions/branch_service.rb index cc16f5fe3..f58a47a21 100644 --- a/lib/pact_broker/versions/branch_service.rb +++ b/lib/pact_broker/versions/branch_service.rb @@ -1,12 +1,19 @@ require "pact_broker/logging" require "pact_broker/repositories" require "pact_broker/messages" +require "forwardable" module PactBroker module Versions class BranchService extend PactBroker::Repositories + class << self + extend Forwardable + delegate [:delete_branch_version] => :branch_version_repository + end + + def self.find_branch_version(pacticipant_name:, branch_name:, version_number:, **) BranchVersion.where( version: PactBroker::Domain::Version.where_pacticipant_name_and_version_number(pacticipant_name, version_number), diff --git a/lib/pact_broker/versions/branch_version_repository.rb b/lib/pact_broker/versions/branch_version_repository.rb index abcca842d..be187f482 100644 --- a/lib/pact_broker/versions/branch_version_repository.rb +++ b/lib/pact_broker/versions/branch_version_repository.rb @@ -16,6 +16,12 @@ def add_branch(version, branch_name, auto_created: false) branch_version end + # Deletes a branch version - that is, removes a version from a branch. + # @param [PactBroker::Versions::BranchVersion] the branch version to delete + def delete_branch_version(branch_version) + branch_version.delete + end + private def find_or_create_branch(pacticipant, branch_name) diff --git a/spec/features/delete_branch_version_spec.rb b/spec/features/delete_branch_version_spec.rb new file mode 100644 index 000000000..afd8fae7f --- /dev/null +++ b/spec/features/delete_branch_version_spec.rb @@ -0,0 +1,30 @@ +describe "Deleting a branch version (removing a version 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/versions/1234" } + let(:headers) { { "CONTENT_TYPE" => "application/json" } } + let(:response_body) { JSON.parse(subject.body, symbolize_names: true) } + + subject { delete(path, {}, headers) } + + it "returns a 204 response" do + expect(subject.status).to be 204 + end + + it "deletes the branch version" do + expect { subject }.to change { PactBroker::Versions::BranchVersion.count }.by(-1) + end + + context "when the branch version does not exist" do + let(:path) { "/pacticipants/foo/branches/main/versions/888" } + + its(:status) { is_expected.to eq 404 } + end +end