Skip to content

Commit ad47993

Browse files
committed
Factor error and success methods from services.
1 parent fda61a0 commit ad47993

10 files changed

+57
-67
lines changed

app/controllers/projects/branches_controller.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ def recent
1717
end
1818

1919
def create
20-
result = CreateBranchService.new.execute(project,
21-
params[:branch_name],
22-
params[:ref],
23-
current_user)
20+
result = CreateBranchService.new(project, current_user).
21+
execute(params[:branch_name], params[:ref])
2422
if result[:status] == :success
2523
@branch = result[:branch]
2624
redirect_to project_tree_path(@project, @branch.name)
@@ -31,7 +29,7 @@ def create
3129
end
3230

3331
def destroy
34-
DeleteBranchService.new.execute(project, params[:id], current_user)
32+
DeleteBranchService.new(project, current_user).execute(params[:id])
3533
@branch_name = params[:id]
3634

3735
respond_to do |format|

app/controllers/projects/edit_tree_controller.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def show
1010
end
1111

1212
def update
13-
result = Files::UpdateService.new(@project, current_user, params, @ref, @path).execute
13+
result = Files::UpdateService.
14+
new(@project, current_user, params, @ref, @path).execute
1415

1516
if result[:status] == :success
1617
flash[:notice] = "Your changes have been successfully committed"

app/controllers/projects/tags_controller.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ def index
1313
end
1414

1515
def create
16-
result = CreateTagService.new.execute(@project, params[:tag_name],
17-
params[:ref], params[:message],
18-
current_user)
16+
result = CreateTagService.new(@project, current_user).
17+
execute(params[:tag_name], params[:ref], params[:message])
1918
if result[:status] == :success
2019
@tag = result[:tag]
2120
redirect_to project_tags_path(@project)

app/services/base_service.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class BaseService
22
attr_accessor :project, :current_user, :params
33

4-
def initialize(project, user, params)
4+
def initialize(project, user, params = {})
55
@project, @current_user, @params = project, user, params.dup
66
end
77

@@ -32,4 +32,19 @@ def log_info message
3232
def system_hook_service
3333
SystemHooksService.new
3434
end
35+
36+
private
37+
38+
def error(message)
39+
{
40+
message: message,
41+
status: :error
42+
}
43+
end
44+
45+
def success
46+
{
47+
status: :success
48+
}
49+
end
3550
end

app/services/create_branch_service.rb

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
class CreateBranchService
2-
def execute(project, branch_name, ref, current_user)
1+
require_relative 'base_service'
2+
3+
class CreateBranchService < BaseService
4+
def execute(branch_name, ref)
35
valid_branch = Gitlab::GitRefValidator.validate(branch_name)
46
if valid_branch == false
57
return error('Branch name invalid')
@@ -22,17 +24,9 @@ def execute(project, branch_name, ref, current_user)
2224
end
2325
end
2426

25-
def error(message)
26-
{
27-
message: message,
28-
status: :error
29-
}
30-
end
31-
3227
def success(branch)
33-
{
34-
branch: branch,
35-
status: :success
36-
}
28+
out = super()
29+
out[:branch] = branch
30+
out
3731
end
3832
end

app/services/create_tag_service.rb

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
class CreateTagService
2-
def execute(project, tag_name, ref, message, current_user)
1+
require_relative 'base_service'
2+
3+
class CreateTagService < BaseService
4+
def execute(tag_name, ref, message)
35
valid_tag = Gitlab::GitRefValidator.validate(tag_name)
46
if valid_tag == false
57
return error('Tag name invalid')
@@ -26,17 +28,9 @@ def execute(project, tag_name, ref, message, current_user)
2628
end
2729
end
2830

29-
def error(message)
30-
{
31-
message: message,
32-
status: :error
33-
}
34-
end
35-
3631
def success(branch)
37-
{
38-
tag: branch,
39-
status: :success
40-
}
32+
out = super()
33+
out[:tag] = branch
34+
out
4135
end
4236
end

app/services/delete_branch_service.rb

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
class DeleteBranchService
2-
def execute(project, branch_name, current_user)
1+
require_relative 'base_service'
2+
3+
class DeleteBranchService < BaseService
4+
def execute(branch_name)
35
repository = project.repository
46
branch = repository.find_branch(branch_name)
57

@@ -31,17 +33,14 @@ def execute(project, branch_name, current_user)
3133
end
3234

3335
def error(message, return_code = 400)
34-
{
35-
message: message,
36-
return_code: return_code,
37-
state: :error
38-
}
36+
out = super(message)
37+
out[:return_code] = return_code
38+
out
3939
end
4040

4141
def success(message)
42-
{
43-
message: message,
44-
state: :success
45-
}
42+
out = super()
43+
out[:message] = message
44+
out
4645
end
4746
end

app/services/files/base_service.rb

+3-11
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,10 @@ def initialize(project, user, params, ref, path = nil)
1010

1111
private
1212

13-
def error(message)
14-
{
15-
error: message,
16-
status: :error
17-
}
18-
end
19-
2013
def success
21-
{
22-
error: '',
23-
status: :success
24-
}
14+
out = super()
15+
out[:error] = ''
16+
out
2517
end
2618

2719
def repository

lib/api/branches.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,8 @@ class Branches < Grape::API
8080
# POST /projects/:id/repository/branches
8181
post ":id/repository/branches" do
8282
authorize_push_project
83-
result = CreateBranchService.new.execute(user_project,
84-
params[:branch_name],
85-
params[:ref],
86-
current_user)
83+
result = CreateBranchService.new(user_project, current_user).
84+
execute(params[:branch_name], params[:ref])
8785
if result[:status] == :success
8886
present result[:branch],
8987
with: Entities::RepoObject,
@@ -102,9 +100,10 @@ class Branches < Grape::API
102100
# DELETE /projects/:id/repository/branches/:branch
103101
delete ":id/repository/branches/:branch" do
104102
authorize_push_project
105-
result = DeleteBranchService.new.execute(user_project, params[:branch], current_user)
103+
result = DeleteBranchService.new(user_project, current_user).
104+
execute(params[:branch])
106105

107-
if result[:state] == :success
106+
if result[:status] == :success
108107
true
109108
else
110109
render_api_error!(result[:message], result[:return_code])

lib/api/repositories.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ def handle_project_member_errors(errors)
3838
post ':id/repository/tags' do
3939
authorize_push_project
4040
message = params[:message] || nil
41-
result = CreateTagService.new.execute(user_project, params[:tag_name],
42-
params[:ref], message,
43-
current_user)
41+
result = CreateTagService.new(user_project, current_user).
42+
execute(params[:tag_name], params[:ref], message)
4443

4544
if result[:status] == :success
4645
present result[:tag],

0 commit comments

Comments
 (0)