Skip to content

Commit f83801a

Browse files
committed
Update files to pass modern rubocop checks. Disable some of them
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
1 parent 8f9046a commit f83801a

12 files changed

+93
-971
lines changed

.rubocop.yml

Lines changed: 32 additions & 913 deletions
Large diffs are not rendered by default.

lib/gitlab_access_status.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def initialize(status, message, gl_repository:, gl_username:, repository_path:,
1414

1515
def self.create_from_json(json)
1616
values = JSON.parse(json)
17-
self.new(values["status"],
18-
values["message"],
19-
gl_repository: values["gl_repository"],
20-
gl_username: values["gl_username"],
21-
repository_path: values["repository_path"],
22-
gitaly: values["gitaly"])
17+
new(values["status"],
18+
values["message"],
19+
gl_repository: values["gl_repository"],
20+
gl_username: values["gl_username"],
21+
repository_path: values["repository_path"],
22+
gitaly: values["gitaly"])
2323
end
2424

2525
def allowed?

lib/gitlab_custom_hook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def call_receive_hook(hook, changes)
4747
# Submit changes to the hook via its stdin.
4848
begin
4949
IO.copy_stream(StringIO.new(changes), stdin_writer)
50-
rescue Errno::EPIPE
50+
rescue Errno::EPIPE # rubocop:disable Lint/HandleExceptions
5151
# It is not an error if the hook does not consume all of its input.
5252
end
5353

lib/gitlab_keys.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,26 @@
44
require_relative 'gitlab_logger'
55
require_relative 'gitlab_metrics'
66

7-
class GitlabKeys
8-
class KeyError < StandardError ; end
7+
class GitlabKeys # rubocop:disable Metrics/ClassLength
8+
class KeyError < StandardError; end
99

1010
attr_accessor :auth_file, :key
1111

1212
def self.command(key_id)
13-
raise KeyError.new("Invalid key_id: #{key_id.inspect}") unless /\A[a-z0-9-]+\z/ =~ key_id
13+
unless /\A[a-z0-9-]+\z/ =~ key_id
14+
raise KeyError, "Invalid key_id: #{key_id.inspect}"
15+
end
16+
1417
"#{ROOT_PATH}/bin/gitlab-shell #{key_id}"
1518
end
1619

1720
def self.key_line(key_id, public_key)
1821
public_key.chomp!
19-
raise KeyError.new("Invalid public_key: #{public_key.inspect}") if public_key.include?("\n")
22+
23+
if public_key.include?("\n")
24+
raise KeyError, "Invalid public_key: #{public_key.inspect}"
25+
end
26+
2027
"command=\"#{command(key_id)}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{public_key}"
2128
end
2229

@@ -31,19 +38,19 @@ def initialize
3138
def exec
3239
GitlabMetrics.measure("command-#{@command}") do
3340
case @command
34-
when 'add-key';
41+
when 'add-key'
3542
add_key
36-
when 'batch-add-keys';
43+
when 'batch-add-keys'
3744
batch_add_keys
38-
when 'rm-key';
45+
when 'rm-key'
3946
rm_key
40-
when 'list-keys';
47+
when 'list-keys'
4148
list_keys
42-
when 'list-key-ids';
49+
when 'list-key-ids'
4350
list_key_ids
44-
when 'clear';
51+
when 'clear'
4552
clear
46-
when 'check-permissions';
53+
when 'check-permissions'
4754
check_permissions
4855
else
4956
$logger.warn "Attempt to execute invalid gitlab-keys command #{@command.inspect}."
@@ -111,7 +118,7 @@ def rm_key
111118
lock do
112119
$logger.info "Removing key #{@key_id}"
113120
open_auth_file('r+') do |f|
114-
while line = f.gets do
121+
while line = f.gets # rubocop:disable Style/AssignmentInCondition
115122
next unless line.start_with?("command=\"#{self.class.command(@key_id)}\"")
116123
f.seek(-line.length, IO::SEEK_CUR)
117124
# Overwrite the line with #'s. Because the 'line' variable contains
@@ -145,7 +152,7 @@ def lock(timeout = 10)
145152
File.open(lock_file, "w+") do |f|
146153
begin
147154
f.flock File::LOCK_EX
148-
Timeout::timeout(timeout) { yield }
155+
Timeout.timeout(timeout) { yield }
149156
ensure
150157
f.flock File::LOCK_UN
151158
end

lib/gitlab_lfs_authentication.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ def initialize(username, lfs_token, repository_http_path)
1111
end
1212

1313
def self.build_from_json(json)
14-
begin
15-
values = JSON.parse(json)
16-
self.new(values['username'], values['lfs_token'], values['repository_http_path'])
17-
rescue
18-
nil
19-
end
14+
values = JSON.parse(json)
15+
new(values['username'], values['lfs_token'], values['repository_http_path'])
16+
rescue
17+
nil
2018
end
2119

2220
def authentication_payload

lib/gitlab_logger.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require_relative 'gitlab_config'
44

5-
def convert_log_level log_level
5+
def convert_log_level(log_level)
66
Logger.const_get(log_level.upcase)
77
rescue NameError
88
$stderr.puts "WARNING: Unrecognized log level #{log_level.inspect}."

lib/gitlab_net.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
require_relative 'gitlab_lfs_authentication'
99
require_relative 'httpunix'
1010

11-
class GitlabNet
11+
class GitlabNet # rubocop:disable Metrics/ClassLength
1212
class ApiUnreachableError < StandardError; end
1313
class NotFound < StandardError; end
1414

1515
CHECK_TIMEOUT = 5
1616
READ_TIMEOUT = 300
1717

1818
def check_access(cmd, gl_repository, repo, actor, changes, protocol, env: {})
19-
changes = changes.join("\n") unless changes.kind_of?(String)
19+
changes = changes.join("\n") unless changes.is_a?(String)
2020

2121
params = {
2222
action: cmd,
@@ -73,7 +73,7 @@ def broadcast_message
7373
end
7474

7575
def merge_request_urls(gl_repository, repo_path, changes)
76-
changes = changes.join("\n") unless changes.kind_of?(String)
76+
changes = changes.join("\n") unless changes.is_a?(String)
7777
changes = changes.encode('UTF-8', 'ASCII', invalid: :replace, replace: '')
7878
url = "#{host}/merge_request_urls?project=#{URI.escape(repo_path)}&changes=#{URI.escape(changes)}"
7979
url += "&gl_repository=#{URI.escape(gl_repository)}" if gl_repository
@@ -152,7 +152,7 @@ def host
152152
"#{config.gitlab_url}/api/v4/internal"
153153
end
154154

155-
def http_client_for(uri, options={})
155+
def http_client_for(uri, options = {})
156156
http = if uri.is_a?(URI::HTTPUNIX)
157157
Net::HTTPUNIX.new(uri.hostname)
158158
else
@@ -189,7 +189,7 @@ def http_request_for(method, uri, params = {})
189189
request
190190
end
191191

192-
def request(method, url, params = {}, options={})
192+
def request(method, url, params = {}, options = {})
193193
$logger.debug "Performing #{method.to_s.upcase} #{url}"
194194

195195
uri = URI.parse(url)
@@ -205,7 +205,7 @@ def request(method, url, params = {}, options={})
205205
raise ApiUnreachableError
206206
ensure
207207
$logger.info do
208-
sprintf('%s %s %0.5f', method.to_s.upcase, url, Time.new - start_time)
208+
sprintf('%s %s %0.5f', method.to_s.upcase, url, Time.new - start_time) # rubocop:disable Style/FormatString
209209
end
210210
end
211211

@@ -218,7 +218,7 @@ def request(method, url, params = {}, options={})
218218
response
219219
end
220220

221-
def get(url, options={})
221+
def get(url, options = {})
222222
request(:get, url, {}, options)
223223
end
224224

@@ -231,13 +231,11 @@ def cert_store
231231
store = OpenSSL::X509::Store.new
232232
store.set_default_paths
233233

234-
if ca_file = config.http_settings['ca_file']
235-
store.add_file(ca_file)
236-
end
234+
ca_file = config.http_settings['ca_file']
235+
store.add_file(ca_file) if ca_file
237236

238-
if ca_path = config.http_settings['ca_path']
239-
store.add_path(ca_path)
240-
end
237+
ca_path = config.http_settings['ca_path']
238+
store.add_path(ca_path) if ca_path
241239

242240
store
243241
end

lib/gitlab_post_receive.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def print_merge_request_links(merge_request_urls)
5050
def print_merge_request_link(merge_request)
5151
message =
5252
if merge_request["new_merge_request"]
53-
"To create a merge request for #{merge_request["branch_name"]}, visit:"
53+
"To create a merge request for #{merge_request['branch_name']}, visit:"
5454
else
55-
"View merge request for #{merge_request["branch_name"]}:"
55+
"View merge request for #{merge_request['branch_name']}:"
5656
end
5757

5858
puts message

lib/gitlab_shell.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
require_relative 'gitlab_net'
55
require_relative 'gitlab_metrics'
66

7-
class GitlabShell
7+
class GitlabShell # rubocop:disable Metrics/ClassLength
88
class AccessDeniedError < StandardError; end
99
class DisallowedCommandError < StandardError; end
1010
class InvalidRepositoryPathError < StandardError; end
1111

1212
GIT_COMMANDS = %w(git-upload-pack git-receive-pack git-upload-archive git-lfs-authenticate).freeze
1313
GITALY_MIGRATED_COMMANDS = {
1414
'git-upload-pack' => File.join(ROOT_PATH, 'bin', 'gitaly-upload-pack'),
15-
'git-receive-pack' => File.join(ROOT_PATH, 'bin', 'gitaly-receive-pack'),
15+
'git-receive-pack' => File.join(ROOT_PATH, 'bin', 'gitaly-receive-pack')
1616
}.freeze
1717
API_COMMANDS = %w(2fa_recovery_codes).freeze
1818
GL_PROTOCOL = 'ssh'.freeze
@@ -44,7 +44,7 @@ def exec(origin_cmd)
4444
process_cmd(args)
4545

4646
true
47-
rescue GitlabNet::ApiUnreachableError => ex
47+
rescue GitlabNet::ApiUnreachableError
4848
$stderr.puts "GitLab: Failed to authorize your Git request: internal API unreachable"
4949
false
5050
rescue AccessDeniedError => ex
@@ -53,13 +53,13 @@ def exec(origin_cmd)
5353

5454
$stderr.puts "GitLab: #{ex.message}"
5555
false
56-
rescue DisallowedCommandError => ex
56+
rescue DisallowedCommandError
5757
message = "gitlab-shell: Attempt to execute disallowed command <#{origin_cmd}> by #{log_username}."
5858
$logger.warn message
5959

6060
$stderr.puts "GitLab: Disallowed command"
6161
false
62-
rescue InvalidRepositoryPathError => ex
62+
rescue InvalidRepositoryPathError
6363
$stderr.puts "GitLab: Invalid repository path"
6464
false
6565
end
@@ -113,7 +113,7 @@ def verify_access
113113
end
114114

115115
def process_cmd(args)
116-
return self.send("api_#{@command}") if API_COMMANDS.include?(@command)
116+
return send("api_#{@command}") if API_COMMANDS.include?(@command)
117117

118118
if @command == 'git-lfs-authenticate'
119119
GitlabMetrics.measure('lfs-authenticate') do
@@ -126,7 +126,7 @@ def process_cmd(args)
126126
executable = @command
127127
args = [repo_path]
128128

129-
if GITALY_MIGRATED_COMMANDS.has_key?(executable) && @gitaly
129+
if GITALY_MIGRATED_COMMANDS.key?(executable) && @gitaly
130130
executable = GITALY_MIGRATED_COMMANDS[executable]
131131

132132
gitaly_address = @gitaly['address']
@@ -172,15 +172,15 @@ def exec_cmd(*args)
172172
end
173173

174174
if git_trace_available?
175-
env.merge!({
175+
env.merge!(
176176
'GIT_TRACE' => @config.git_trace_log_file,
177177
'GIT_TRACE_PACKET' => @config.git_trace_log_file,
178-
'GIT_TRACE_PERFORMANCE' => @config.git_trace_log_file,
179-
})
178+
'GIT_TRACE_PERFORMANCE' => @config.git_trace_log_file
179+
)
180180
end
181181

182182
# We use 'chdir: ROOT_PATH' to let the next executable know where config.yml is.
183-
Kernel::exec(env, *args, unsetenv_others: true, chdir: ROOT_PATH)
183+
Kernel.exec(env, *args, unsetenv_others: true, chdir: ROOT_PATH)
184184
end
185185

186186
def api

lib/httpunix.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module URI
1111
class HTTPUNIX < HTTP
1212
def hostname
1313
# decode %XX from path to file
14-
v = self.host
14+
v = host
1515
URI.decode(v)
1616
end
1717

@@ -30,7 +30,7 @@ def set_port(v)
3030
# - Net::HTTP::connect
3131
module Net
3232
class HTTPUNIX < HTTP
33-
def initialize(socketpath, port=nil)
33+
def initialize(socketpath, port = nil)
3434
super(socketpath, port)
3535
@port = nil # HTTP will set it to default - override back -> set DEFAULT_PORT
3636
end

0 commit comments

Comments
 (0)