Skip to content

Commit 3baed8c

Browse files
committed
Services: code style fixes, minor refactoring
1 parent cfd5870 commit 3baed8c

18 files changed

+37
-43
lines changed

app/services/audit_event_service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def for_authentication
77
@details = {
88
with: @details[:with],
99
target_id: @author.id,
10-
target_type: "User",
10+
target_type: 'User',
1111
target_details: @author.name,
1212
}
1313

app/services/auth/container_registry_authentication_service.rb

+2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ def self.full_access_token(*names)
2020
token.issuer = registry.issuer
2121
token.audience = AUDIENCE
2222
token.expire_time = token_expire_at
23+
2324
token[:access] = names.map do |name|
2425
{ type: 'repository', name: name, actions: %w(*) }
2526
end
27+
2628
token.encoded
2729
end
2830

app/services/create_branch_service.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
class CreateBranchService < BaseService
44
def execute(branch_name, ref, source_project: @project)
55
valid_branch = Gitlab::GitRefValidator.validate(branch_name)
6-
if valid_branch == false
6+
7+
unless valid_branch
78
return error('Branch name is invalid')
89
end
910

1011
repository = project.repository
1112
existing_branch = repository.find_branch(branch_name)
13+
1214
if existing_branch
1315
return error('Branch already exists')
1416
end
1517

1618
new_branch = nil
19+
1720
if source_project != @project
1821
repository.with_tmp_ref do |tmp_ref|
1922
repository.fetch_ref(
@@ -29,7 +32,6 @@ def execute(branch_name, ref, source_project: @project)
2932
end
3033

3134
if new_branch
32-
# GitPushService handles execution of services and hooks for branch pushes
3335
success(new_branch)
3436
else
3537
error('Invalid reference name')
@@ -39,8 +41,6 @@ def execute(branch_name, ref, source_project: @project)
3941
end
4042

4143
def success(branch)
42-
out = super()
43-
out[:branch] = branch
44-
out
44+
super().merge(branch: branch)
4545
end
4646
end

app/services/create_release_service.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ def execute(tag_name, release_description)
2323
end
2424

2525
def success(release)
26-
out = super()
27-
out[:release] = release
28-
out
26+
super().merge(release: release)
2927
end
3028
end

app/services/create_snippet_service.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class CreateSnippetService < BaseService
22
def execute
3-
if project.nil?
4-
snippet = PersonalSnippet.new(params)
5-
else
6-
snippet = project.snippets.build(params)
7-
end
3+
snippet = if project
4+
project.snippets.build(params)
5+
else
6+
PersonalSnippet.new(params)
7+
end
88

99
unless Gitlab::VisibilityLevel.allowed_for?(current_user, params[:visibility_level])
1010
deny_visibility_level(snippet)

app/services/create_tag_service.rb

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def execute(tag_name, target, message, release_description = nil)
99
message.strip! if message
1010

1111
new_tag = nil
12+
1213
begin
1314
new_tag = repository.add_tag(current_user, tag_name, target, message)
1415
rescue Rugged::TagError
@@ -22,6 +23,7 @@ def execute(tag_name, target, message, release_description = nil)
2223
CreateReleaseService.new(@project, @current_user).
2324
execute(tag_name, release_description)
2425
end
26+
2527
success.merge(tag: new_tag)
2628
else
2729
error("Target #{target} is invalid")

app/services/delete_branch_service.rb

+2-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ def execute(branch_name)
55
repository = project.repository
66
branch = repository.find_branch(branch_name)
77

8-
# No such branch
98
unless branch
109
return error('No such branch', 404)
1110
end
@@ -14,18 +13,15 @@ def execute(branch_name)
1413
return error('Cannot remove HEAD branch', 405)
1514
end
1615

17-
# Dont allow remove of protected branch
1816
if project.protected_branch?(branch_name)
1917
return error('Protected branch cant be removed', 405)
2018
end
2119

22-
# Dont allow user to remove branch if he is not allowed to push
2320
unless current_user.can?(:push_code, project)
2421
return error('You dont have push access to repo', 405)
2522
end
2623

2724
if repository.rm_branch(current_user, branch_name)
28-
# GitPushService handles execution of services and hooks for branch pushes
2925
success('Branch was removed')
3026
else
3127
error('Failed to remove branch')
@@ -35,15 +31,11 @@ def execute(branch_name)
3531
end
3632

3733
def error(message, return_code = 400)
38-
out = super(message)
39-
out[:return_code] = return_code
40-
out
34+
super(message).merge(return_code: return_code)
4135
end
4236

4337
def success(message)
44-
out = super()
45-
out[:message] = message
46-
out
38+
super().merge(message: message)
4739
end
4840

4941
def build_push_data(branch)

app/services/delete_tag_service.rb

+2-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ def execute(tag_name)
55
repository = project.repository
66
tag = repository.find_tag(tag_name)
77

8-
# No such tag
98
unless tag
109
return error('No such tag', 404)
1110
end
@@ -26,15 +25,11 @@ def execute(tag_name)
2625
end
2726

2827
def error(message, return_code = 400)
29-
out = super(message)
30-
out[:return_code] = return_code
31-
out
28+
super(message).merge(return_code: return_code)
3229
end
3330

3431
def success(message)
35-
out = super()
36-
out[:message] = message
37-
out
32+
super().merge(message: message)
3833
end
3934

4035
def build_push_data(tag)

app/services/files/base_service.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ def execute
1515
params[:file_content]
1616
end
1717

18-
# Validate parameters
1918
validate
2019

2120
# Create new branch if it different from source_branch
@@ -26,7 +25,7 @@ def execute
2625
if commit
2726
success
2827
else
29-
error("Something went wrong. Your changes were not committed")
28+
error('Something went wrong. Your changes were not committed')
3029
end
3130
rescue Repository::CommitError, Gitlab::Git::Repository::InvalidBlobName, GitHooksService::PreReceiveError, ValidationError => ex
3231
error(ex.message)
@@ -51,12 +50,12 @@ def validate
5150

5251
unless project.empty_repo?
5352
unless @source_project.repository.branch_names.include?(@source_branch)
54-
raise_error("You can only create or edit files when you are on a branch")
53+
raise_error('You can only create or edit files when you are on a branch')
5554
end
5655

5756
if different_branch?
5857
if repository.branch_names.include?(@target_branch)
59-
raise_error("Branch with such name already exists. You need to switch to this branch in order to make changes")
58+
raise_error('Branch with such name already exists. You need to switch to this branch in order to make changes')
6059
end
6160
end
6261
end

app/services/files/create_service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def validate
2929
blob = repository.blob_at_branch(@source_branch, @file_path)
3030

3131
if blob
32-
raise_error("Your changes could not be committed because a file with the same name already exists")
32+
raise_error('Your changes could not be committed because a file with the same name already exists')
3333
end
3434
end
3535
end

app/services/git_tag_push_service.rb

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def build_push_data
2626
unless Gitlab::Git.blank_ref?(params[:newrev])
2727
tag_name = Gitlab::Git.ref_name(params[:ref])
2828
tag = project.repository.find_tag(tag_name)
29+
2930
if tag && tag.target == params[:newrev]
3031
commit = project.commit(tag.target)
3132
commits = [commit].compact

app/services/issues/bulk_update_service.rb

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def execute
99
end
1010

1111
issues = Issue.where(id: issues_ids)
12+
1213
issues.each do |issue|
1314
next unless can?(current_user, :update_issue, issue)
1415

app/services/merge_requests/post_merge_service.rb

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def close_issues(merge_request)
2020
return unless merge_request.target_branch == project.default_branch
2121

2222
closed_issues = merge_request.closes_issues(current_user)
23+
2324
closed_issues.each do |issue|
2425
if can?(current_user, :update_issue, issue)
2526
Issues::CloseService.new(project, current_user, {}).execute(issue, commit: merge_request)

app/services/notification_service.rb

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def new_note(note)
153153
else
154154
mentioned_users
155155
end
156+
156157
recipients = recipients.concat(participants)
157158

158159
# Merge project watchers
@@ -176,6 +177,7 @@ def new_note(note)
176177

177178
# build notify method like 'note_commit_email'
178179
notify_method = "note_#{note.noteable_type.underscore}_email".to_sym
180+
179181
recipients.each do |recipient|
180182
mailer.send(notify_method, recipient.id, note.id).deliver_later
181183
end

app/services/projects/update_service.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ class UpdateService < BaseService
33
def execute
44
# check that user is allowed to set specified visibility_level
55
new_visibility = params[:visibility_level]
6+
67
if new_visibility && new_visibility.to_i != project.visibility_level
78
unless can?(current_user, :change_visibility_level, project) &&
89
Gitlab::VisibilityLevel.allowed_for?(current_user, new_visibility)
9-
10+
1011
deny_visibility_level(project, new_visibility)
1112
return project
1213
end

app/services/system_note_service.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def self.change_label(noteable, project, author, added_labels, removed_labels)
8282
end
8383

8484
body << ' ' << 'label'.pluralize(labels_count)
85-
body = "#{body.capitalize}"
85+
body = body.capitalize
8686

8787
create_note(noteable: noteable, project: project, author: author, note: body)
8888
end
@@ -125,7 +125,7 @@ def self.change_milestone(noteable, project, author, milestone)
125125
# Returns the created Note object
126126
def self.change_status(noteable, project, author, status, source)
127127
body = "Status changed to #{status}"
128-
body += " by #{source.gfm_reference(project)}" if source
128+
body << " by #{source.gfm_reference(project)}" if source
129129

130130
create_note(noteable: noteable, project: project, author: author, note: body)
131131
end
@@ -139,7 +139,7 @@ def self.merge_when_build_succeeds(noteable, project, author, last_commit)
139139

140140
# Called when 'merge when build succeeds' is canceled
141141
def self.cancel_merge_when_build_succeeds(noteable, project, author)
142-
body = "Canceled the automatic merge"
142+
body = 'Canceled the automatic merge'
143143

144144
create_note(noteable: noteable, project: project, author: author, note: body)
145145
end
@@ -236,6 +236,7 @@ def self.change_branch_presence(noteable, project, author, branch_type, branch,
236236
else
237237
'deleted'
238238
end
239+
239240
body = "#{verb} #{branch_type.to_s} branch `#{branch}`".capitalize
240241
create_note(noteable: noteable, project: project, author: author, note: body)
241242
end

app/services/update_release_service.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ def execute(tag_name, release_description)
2121
end
2222

2323
def success(release)
24-
out = super()
25-
out[:release] = release
26-
out
24+
super().merge(release: release)
2725
end
2826
end

app/services/update_snippet_service.rb

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def initialize(project, user, snippet, params)
99
def execute
1010
# check that user is allowed to set specified visibility_level
1111
new_visibility = params[:visibility_level]
12+
1213
if new_visibility && new_visibility.to_i != snippet.visibility_level
1314
unless Gitlab::VisibilityLevel.allowed_for?(current_user, new_visibility)
1415
deny_visibility_level(snippet, new_visibility)

0 commit comments

Comments
 (0)