Commit db12e2dc authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #7807 from cirosantilli/factor-service-error

Factor error and success methods from services.
parents ed9a6bf9 ad47993a
...@@ -17,10 +17,8 @@ class Projects::BranchesController < Projects::ApplicationController ...@@ -17,10 +17,8 @@ class Projects::BranchesController < Projects::ApplicationController
end end
def create def create
result = CreateBranchService.new.execute(project, result = CreateBranchService.new(project, current_user).
params[:branch_name], execute(params[:branch_name], params[:ref])
params[:ref],
current_user)
if result[:status] == :success if result[:status] == :success
@branch = result[:branch] @branch = result[:branch]
redirect_to project_tree_path(@project, @branch.name) redirect_to project_tree_path(@project, @branch.name)
...@@ -31,7 +29,7 @@ class Projects::BranchesController < Projects::ApplicationController ...@@ -31,7 +29,7 @@ class Projects::BranchesController < Projects::ApplicationController
end end
def destroy def destroy
DeleteBranchService.new.execute(project, params[:id], current_user) DeleteBranchService.new(project, current_user).execute(params[:id])
@branch_name = params[:id] @branch_name = params[:id]
respond_to do |format| respond_to do |format|
......
...@@ -10,7 +10,8 @@ class Projects::EditTreeController < Projects::BaseTreeController ...@@ -10,7 +10,8 @@ class Projects::EditTreeController < Projects::BaseTreeController
end end
def update def update
result = Files::UpdateService.new(@project, current_user, params, @ref, @path).execute result = Files::UpdateService.
new(@project, current_user, params, @ref, @path).execute
if result[:status] == :success if result[:status] == :success
flash[:notice] = "Your changes have been successfully committed" flash[:notice] = "Your changes have been successfully committed"
......
...@@ -13,9 +13,8 @@ class Projects::TagsController < Projects::ApplicationController ...@@ -13,9 +13,8 @@ class Projects::TagsController < Projects::ApplicationController
end end
def create def create
result = CreateTagService.new.execute(@project, params[:tag_name], result = CreateTagService.new(@project, current_user).
params[:ref], params[:message], execute(params[:tag_name], params[:ref], params[:message])
current_user)
if result[:status] == :success if result[:status] == :success
@tag = result[:tag] @tag = result[:tag]
redirect_to project_tags_path(@project) redirect_to project_tags_path(@project)
......
class BaseService class BaseService
attr_accessor :project, :current_user, :params attr_accessor :project, :current_user, :params
def initialize(project, user, params) def initialize(project, user, params = {})
@project, @current_user, @params = project, user, params.dup @project, @current_user, @params = project, user, params.dup
end end
...@@ -32,4 +32,19 @@ class BaseService ...@@ -32,4 +32,19 @@ class BaseService
def system_hook_service def system_hook_service
SystemHooksService.new SystemHooksService.new
end end
private
def error(message)
{
message: message,
status: :error
}
end
def success
{
status: :success
}
end
end end
class CreateBranchService require_relative 'base_service'
def execute(project, branch_name, ref, current_user)
class CreateBranchService < BaseService
def execute(branch_name, ref)
valid_branch = Gitlab::GitRefValidator.validate(branch_name) valid_branch = Gitlab::GitRefValidator.validate(branch_name)
if valid_branch == false if valid_branch == false
return error('Branch name invalid') return error('Branch name invalid')
...@@ -22,17 +24,9 @@ class CreateBranchService ...@@ -22,17 +24,9 @@ class CreateBranchService
end end
end end
def error(message)
{
message: message,
status: :error
}
end
def success(branch) def success(branch)
{ out = super()
branch: branch, out[:branch] = branch
status: :success out
}
end end
end end
class CreateTagService require_relative 'base_service'
def execute(project, tag_name, ref, message, current_user)
class CreateTagService < BaseService
def execute(tag_name, ref, message)
valid_tag = Gitlab::GitRefValidator.validate(tag_name) valid_tag = Gitlab::GitRefValidator.validate(tag_name)
if valid_tag == false if valid_tag == false
return error('Tag name invalid') return error('Tag name invalid')
...@@ -26,17 +28,9 @@ class CreateTagService ...@@ -26,17 +28,9 @@ class CreateTagService
end end
end end
def error(message)
{
message: message,
status: :error
}
end
def success(branch) def success(branch)
{ out = super()
tag: branch, out[:tag] = branch
status: :success out
}
end end
end end
class DeleteBranchService require_relative 'base_service'
def execute(project, branch_name, current_user)
class DeleteBranchService < BaseService
def execute(branch_name)
repository = project.repository repository = project.repository
branch = repository.find_branch(branch_name) branch = repository.find_branch(branch_name)
...@@ -31,17 +33,14 @@ class DeleteBranchService ...@@ -31,17 +33,14 @@ class DeleteBranchService
end end
def error(message, return_code = 400) def error(message, return_code = 400)
{ out = super(message)
message: message, out[:return_code] = return_code
return_code: return_code, out
state: :error
}
end end
def success(message) def success(message)
{ out = super()
message: message, out[:message] = message
state: :success out
}
end end
end end
...@@ -10,18 +10,10 @@ module Files ...@@ -10,18 +10,10 @@ module Files
private private
def error(message)
{
error: message,
status: :error
}
end
def success def success
{ out = super()
error: '', out[:error] = ''
status: :success out
}
end end
def repository def repository
......
...@@ -80,10 +80,8 @@ module API ...@@ -80,10 +80,8 @@ module API
# POST /projects/:id/repository/branches # POST /projects/:id/repository/branches
post ":id/repository/branches" do post ":id/repository/branches" do
authorize_push_project authorize_push_project
result = CreateBranchService.new.execute(user_project, result = CreateBranchService.new(user_project, current_user).
params[:branch_name], execute(params[:branch_name], params[:ref])
params[:ref],
current_user)
if result[:status] == :success if result[:status] == :success
present result[:branch], present result[:branch],
with: Entities::RepoObject, with: Entities::RepoObject,
...@@ -102,9 +100,10 @@ module API ...@@ -102,9 +100,10 @@ module API
# DELETE /projects/:id/repository/branches/:branch # DELETE /projects/:id/repository/branches/:branch
delete ":id/repository/branches/:branch" do delete ":id/repository/branches/:branch" do
authorize_push_project authorize_push_project
result = DeleteBranchService.new.execute(user_project, params[:branch], current_user) result = DeleteBranchService.new(user_project, current_user).
execute(params[:branch])
if result[:state] == :success if result[:status] == :success
true true
else else
render_api_error!(result[:message], result[:return_code]) render_api_error!(result[:message], result[:return_code])
......
...@@ -38,9 +38,8 @@ module API ...@@ -38,9 +38,8 @@ module API
post ':id/repository/tags' do post ':id/repository/tags' do
authorize_push_project authorize_push_project
message = params[:message] || nil message = params[:message] || nil
result = CreateTagService.new.execute(user_project, params[:tag_name], result = CreateTagService.new(user_project, current_user).
params[:ref], message, execute(params[:tag_name], params[:ref], message)
current_user)
if result[:status] == :success if result[:status] == :success
present result[:tag], present result[:tag],
......
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