Skip to content
Snippets Groups Projects
Commit ad47993a authored by Ciro Santilli's avatar Ciro Santilli
Browse files

Factor error and success methods from services.

parent fda61a04
No related branches found
No related tags found
1 merge request!7807Factor error and success methods from services.
Loading
Loading
@@ -17,10 +17,8 @@ class Projects::BranchesController < Projects::ApplicationController
end
 
def create
result = CreateBranchService.new.execute(project,
params[:branch_name],
params[:ref],
current_user)
result = CreateBranchService.new(project, current_user).
execute(params[:branch_name], params[:ref])
if result[:status] == :success
@branch = result[:branch]
redirect_to project_tree_path(@project, @branch.name)
Loading
Loading
@@ -31,7 +29,7 @@ class Projects::BranchesController < Projects::ApplicationController
end
 
def destroy
DeleteBranchService.new.execute(project, params[:id], current_user)
DeleteBranchService.new(project, current_user).execute(params[:id])
@branch_name = params[:id]
 
respond_to do |format|
Loading
Loading
Loading
Loading
@@ -10,7 +10,8 @@ class Projects::EditTreeController < Projects::BaseTreeController
end
 
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
flash[:notice] = "Your changes have been successfully committed"
Loading
Loading
Loading
Loading
@@ -13,9 +13,8 @@ class Projects::TagsController < Projects::ApplicationController
end
 
def create
result = CreateTagService.new.execute(@project, params[:tag_name],
params[:ref], params[:message],
current_user)
result = CreateTagService.new(@project, current_user).
execute(params[:tag_name], params[:ref], params[:message])
if result[:status] == :success
@tag = result[:tag]
redirect_to project_tags_path(@project)
Loading
Loading
class BaseService
attr_accessor :project, :current_user, :params
 
def initialize(project, user, params)
def initialize(project, user, params = {})
@project, @current_user, @params = project, user, params.dup
end
 
Loading
Loading
@@ -32,4 +32,19 @@ class BaseService
def system_hook_service
SystemHooksService.new
end
private
def error(message)
{
message: message,
status: :error
}
end
def success
{
status: :success
}
end
end
class CreateBranchService
def execute(project, branch_name, ref, current_user)
require_relative 'base_service'
class CreateBranchService < BaseService
def execute(branch_name, ref)
valid_branch = Gitlab::GitRefValidator.validate(branch_name)
if valid_branch == false
return error('Branch name invalid')
Loading
Loading
@@ -22,17 +24,9 @@ class CreateBranchService
end
end
 
def error(message)
{
message: message,
status: :error
}
end
def success(branch)
{
branch: branch,
status: :success
}
out = super()
out[:branch] = branch
out
end
end
class CreateTagService
def execute(project, tag_name, ref, message, current_user)
require_relative 'base_service'
class CreateTagService < BaseService
def execute(tag_name, ref, message)
valid_tag = Gitlab::GitRefValidator.validate(tag_name)
if valid_tag == false
return error('Tag name invalid')
Loading
Loading
@@ -26,17 +28,9 @@ class CreateTagService
end
end
 
def error(message)
{
message: message,
status: :error
}
end
def success(branch)
{
tag: branch,
status: :success
}
out = super()
out[:tag] = branch
out
end
end
class DeleteBranchService
def execute(project, branch_name, current_user)
require_relative 'base_service'
class DeleteBranchService < BaseService
def execute(branch_name)
repository = project.repository
branch = repository.find_branch(branch_name)
 
Loading
Loading
@@ -31,17 +33,14 @@ class DeleteBranchService
end
 
def error(message, return_code = 400)
{
message: message,
return_code: return_code,
state: :error
}
out = super(message)
out[:return_code] = return_code
out
end
 
def success(message)
{
message: message,
state: :success
}
out = super()
out[:message] = message
out
end
end
Loading
Loading
@@ -10,18 +10,10 @@ module Files
 
private
 
def error(message)
{
error: message,
status: :error
}
end
def success
{
error: '',
status: :success
}
out = super()
out[:error] = ''
out
end
 
def repository
Loading
Loading
Loading
Loading
@@ -80,10 +80,8 @@ module API
# POST /projects/:id/repository/branches
post ":id/repository/branches" do
authorize_push_project
result = CreateBranchService.new.execute(user_project,
params[:branch_name],
params[:ref],
current_user)
result = CreateBranchService.new(user_project, current_user).
execute(params[:branch_name], params[:ref])
if result[:status] == :success
present result[:branch],
with: Entities::RepoObject,
Loading
Loading
@@ -102,9 +100,10 @@ module API
# DELETE /projects/:id/repository/branches/:branch
delete ":id/repository/branches/:branch" do
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
else
render_api_error!(result[:message], result[:return_code])
Loading
Loading
Loading
Loading
@@ -38,9 +38,8 @@ module API
post ':id/repository/tags' do
authorize_push_project
message = params[:message] || nil
result = CreateTagService.new.execute(user_project, params[:tag_name],
params[:ref], message,
current_user)
result = CreateTagService.new(user_project, current_user).
execute(params[:tag_name], params[:ref], message)
 
if result[:status] == :success
present result[:tag],
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment