BigW Consortium Gitlab

Commit 613856fa by dixpac

Move tag services to `Tags` namespace

CreateTagService and DeleteTagService where in root namespace, by following service code organization I moved them in Tags::CreateService and Tags::DestroyService
parent f802ad37
...@@ -27,7 +27,7 @@ class Projects::TagsController < Projects::ApplicationController ...@@ -27,7 +27,7 @@ class Projects::TagsController < Projects::ApplicationController
end end
def create def create
result = CreateTagService.new(@project, current_user). result = Tags::CreateService.new(@project, current_user).
execute(params[:tag_name], params[:ref], params[:message], params[:release_description]) execute(params[:tag_name], params[:ref], params[:message], params[:release_description])
if result[:status] == :success if result[:status] == :success
...@@ -41,7 +41,7 @@ class Projects::TagsController < Projects::ApplicationController ...@@ -41,7 +41,7 @@ class Projects::TagsController < Projects::ApplicationController
end end
def destroy def destroy
DeleteTagService.new(project, current_user).execute(params[:id]) Tags::DestroyService.new(project, current_user).execute(params[:id])
respond_to do |format| respond_to do |format|
format.html do format.html do
......
class CreateTagService < BaseService module Tags
def execute(tag_name, target, message, release_description = nil) class CreateService < BaseService
valid_tag = Gitlab::GitRefValidator.validate(tag_name) def execute(tag_name, target, message, release_description = nil)
return error('Tag name invalid') unless valid_tag valid_tag = Gitlab::GitRefValidator.validate(tag_name)
return error('Tag name invalid') unless valid_tag
repository = project.repository repository = project.repository
message&.strip! message&.strip!
new_tag = nil new_tag = nil
begin begin
new_tag = repository.add_tag(current_user, tag_name, target, message) new_tag = repository.add_tag(current_user, tag_name, target, message)
rescue Rugged::TagError rescue Rugged::TagError
return error("Tag #{tag_name} already exists") return error("Tag #{tag_name} already exists")
rescue GitHooksService::PreReceiveError => ex rescue GitHooksService::PreReceiveError => ex
return error(ex.message) return error(ex.message)
end end
if new_tag
if release_description
CreateReleaseService.new(@project, @current_user).
execute(tag_name, release_description)
end
if new_tag success.merge(tag: new_tag)
if release_description else
CreateReleaseService.new(@project, @current_user). error("Target #{target} is invalid")
execute(tag_name, release_description)
end end
success.merge(tag: new_tag)
else
error("Target #{target} is invalid")
end end
end end
end end
class DeleteTagService < BaseService module Tags
def execute(tag_name) class DestroyService < BaseService
repository = project.repository def execute(tag_name)
tag = repository.find_tag(tag_name) repository = project.repository
tag = repository.find_tag(tag_name)
unless tag unless tag
return error('No such tag', 404) return error('No such tag', 404)
end end
if repository.rm_tag(current_user, tag_name) if repository.rm_tag(current_user, tag_name)
release = project.releases.find_by(tag: tag_name) release = project.releases.find_by(tag: tag_name)
release&.destroy release&.destroy
push_data = build_push_data(tag) push_data = build_push_data(tag)
EventCreateService.new.push(project, current_user, push_data) EventCreateService.new.push(project, current_user, push_data)
project.execute_hooks(push_data.dup, :tag_push_hooks) project.execute_hooks(push_data.dup, :tag_push_hooks)
project.execute_services(push_data.dup, :tag_push_hooks) project.execute_services(push_data.dup, :tag_push_hooks)
success('Tag was removed') success('Tag was removed')
else else
error('Failed to remove tag') error('Failed to remove tag')
end
end end
end
def error(message, return_code = 400) def error(message, return_code = 400)
super(message).merge(return_code: return_code) super(message).merge(return_code: return_code)
end end
def success(message) def success(message)
super().merge(message: message) super().merge(message: message)
end end
def build_push_data(tag) def build_push_data(tag)
Gitlab::DataBuilder::Push.build( Gitlab::DataBuilder::Push.build(
project, project,
current_user, current_user,
tag.dereferenced_target.sha, tag.dereferenced_target.sha,
Gitlab::Git::BLANK_SHA, Gitlab::Git::BLANK_SHA,
"#{Gitlab::Git::TAG_REF_PREFIX}#{tag.name}", "#{Gitlab::Git::TAG_REF_PREFIX}#{tag.name}",
[]) [])
end
end end
end end
---
title: Move tag services to Tags namespace
merge_request:
author: dixpac
...@@ -40,7 +40,7 @@ module API ...@@ -40,7 +40,7 @@ module API
post ':id/repository/tags' do post ':id/repository/tags' do
authorize_push_project authorize_push_project
result = CreateTagService.new(user_project, current_user). result = ::Tags::CreateService.new(user_project, current_user).
execute(params[:tag_name], params[:ref], params[:message], params[:release_description]) execute(params[:tag_name], params[:ref], params[:message], params[:release_description])
if result[:status] == :success if result[:status] == :success
...@@ -59,7 +59,7 @@ module API ...@@ -59,7 +59,7 @@ module API
delete ":id/repository/tags/:tag_name", requirements: { tag_name: /.+/ } do delete ":id/repository/tags/:tag_name", requirements: { tag_name: /.+/ } do
authorize_push_project authorize_push_project
result = DeleteTagService.new(user_project, current_user). result = ::Tags::DestroyService.new(user_project, current_user).
execute(params[:tag_name]) execute(params[:tag_name])
if result[:status] == :success if result[:status] == :success
......
require 'spec_helper' require 'spec_helper'
describe CreateTagService, services: true do describe Tags::CreateService, services: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:repository) { project.repository } let(:repository) { project.repository }
let(:user) { create(:user) } let(:user) { create(:user) }
......
require 'spec_helper' require 'spec_helper'
describe DeleteTagService, services: true do describe Tags::DestroyService, services: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:repository) { project.repository } let(:repository) { project.repository }
let(:user) { create(:user) } let(:user) { create(:user) }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment