diff --git a/app/services/delete_release_service.rb b/app/services/delete_release_service.rb new file mode 100644 index 00000000000..4e595971949 --- /dev/null +++ b/app/services/delete_release_service.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +class DeleteReleaseService < BaseService + include Gitlab::Utils::StrongMemoize + + def execute + return error('Tag does not exist', 404) unless existing_tag + return error('Release does not exist', 404) unless release + return error('Access Denied', 403) unless allowed? + + if release.destory + success(release: release) + else + error(release.errors.messages || '400 Bad request', 400) + end + end + + private + + def allowed? + Ability.allowed?(current_user, :admin_release, release) + end + + def release + strong_memoize(:release) do + project.releases.find_by_tag(@tag_name) + end + end + + def existing_tag + strong_memoize(:existing_tag) do + repository.find_tag(@tag_name) + end + end + + def repository + strong_memoize(:repository) do + project.repository + end + end +end diff --git a/lib/api/releases.rb b/lib/api/releases.rb index 2d4a6a28998..fe226a5ec4c 100644 --- a/lib/api/releases.rb +++ b/lib/api/releases.rb @@ -89,6 +89,27 @@ module API render_api_error!(result[:message], result[:http_status]) end end + + desc 'Delete a release' do + detail 'This feature was introduced in GitLab 11.7.' + success Entities::Release + end + params do + requires :tag_name, type: String, desc: 'The name of the tag', as: :tag + end + delete ':id/releases/:tag_name', requirements: RELEASE_ENDPOINT_REQUIREMETS do + authorize_update_release! + + attributes = declared(params) + attributes.delete(:id) + result = DeleteReleaseService.new(user_project, current_user, attributes).execute + + if result[:status] == :success + present result[:release], with: Entities::Release + else + render_api_error!(result[:message], result[:http_status]) + end + end end end end diff --git a/spec/requests/api/releases_spec.rb b/spec/requests/api/releases_spec.rb new file mode 100644 index 00000000000..7bdbe2ac06f --- /dev/null +++ b/spec/requests/api/releases_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe API::Releases do + let(:user) { create(:user) } + let(:guest) { create(:user).tap { |u| project.add_guest(u) } } + let(:project) { create(:project, :repository, creator: user, path: 'my.project') } + let(:tag_name) { project.repository.find_tag('v1.1.0').name } + + let(:project_id) { project.id } + let(:current_user) { nil } + + before do + project.add_maintainer(user) + end + + describe 'GET /projects/:id/releases' do + # TODO: + end + + describe 'GET /projects/:id/releases/:tag_name' do + # TODO: + end + + describe 'POST /projects/:id/releases' do + # TODO: + end + + describe 'PUT /projects/:id/releases/:tag_name' do + # TODO: + end + + describe 'DELETE /projects/:id/releases/:tag_name' do + # TODO: + end +end diff --git a/spec/services/delete_release_service_spec.rb b/spec/services/delete_release_service_spec.rb new file mode 100644 index 00000000000..faefdb342bb --- /dev/null +++ b/spec/services/delete_release_service_spec.rb @@ -0,0 +1 @@ +#TODO: \ No newline at end of file