summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsh McKenzie <amckenzie@gitlab.com>2018-12-06 19:37:03 +1100
committerAsh McKenzie <amckenzie@gitlab.com>2018-12-13 14:22:03 +1100
commite203bdd1dab0a05835e065a0e1dee9b1d53990a2 (patch)
tree93bb406a94f96294910e0f16286c77f5d3f8ad89
parent7f931ae0ed720106a3b87fc79191b1feecb00519 (diff)
downloadgitlab-shell-e203bdd1dab0a05835e065a0e1dee9b1d53990a2.tar.gz
Address rubocop offences
-rw-r--r--.rubocop.yml6
-rw-r--r--lib/action/custom.rb2
-rw-r--r--lib/gitlab_access.rb4
-rw-r--r--lib/gitlab_keys.rb7
-rw-r--r--lib/gitlab_lfs_authentication.rb2
-rw-r--r--lib/gitlab_logger.rb6
-rw-r--r--lib/gitlab_net.rb22
-rw-r--r--lib/gitlab_shell.rb12
-rw-r--r--lib/http_helper.rb2
-rw-r--r--lib/httpunix.rb2
-rw-r--r--spec/gitlab_logger_spec.rb8
11 files changed, 41 insertions, 32 deletions
diff --git a/.rubocop.yml b/.rubocop.yml
index 69b76d6..5516c3c 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -36,6 +36,9 @@ Metrics/CyclomaticComplexity:
Metrics/PerceivedComplexity:
Enabled: false
+Naming/AccessorMethodName:
+ Enabled: false
+
Style/Documentation:
Enabled: false
@@ -48,9 +51,6 @@ Style/StringLiterals:
Style/GlobalVars:
Enabled: false
-Style/AccessorMethodName:
- Enabled: false
-
Style/GuardClause:
Enabled: false
diff --git a/lib/action/custom.rb b/lib/action/custom.rb
index 689bfa1..3382e83 100644
--- a/lib/action/custom.rb
+++ b/lib/action/custom.rb
@@ -102,7 +102,7 @@ module Action
end
def inform_client(str)
- $stderr.puts(format_gitlab_output(str))
+ warn(format_gitlab_output(str))
end
def format_gitlab_output(str)
diff --git a/lib/gitlab_access.rb b/lib/gitlab_access.rb
index e1a5e35..ce299fe 100644
--- a/lib/gitlab_access.rb
+++ b/lib/gitlab_access.rb
@@ -31,10 +31,10 @@ class GitlabAccess
true
rescue GitlabNet::ApiUnreachableError
- $stderr.puts "GitLab: Failed to authorize your Git request: internal API unreachable"
+ warn "GitLab: Failed to authorize your Git request: internal API unreachable"
false
rescue AccessDeniedError => ex
- $stderr.puts "GitLab: #{ex.message}"
+ warn "GitLab: #{ex.message}"
false
end
diff --git a/lib/gitlab_keys.rb b/lib/gitlab_keys.rb
index d1b477c..aeebf62 100644
--- a/lib/gitlab_keys.rb
+++ b/lib/gitlab_keys.rb
@@ -14,7 +14,7 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength
end
def self.command_key(key_id)
- unless /\A[a-z0-9-]+\z/ =~ key_id
+ unless /\A[a-z0-9-]+\z/ =~ key_id # rubocop:disable Performance/RegexpMatch
raise KeyError, "Invalid key_id: #{key_id.inspect}"
end
@@ -140,6 +140,7 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength
open_auth_file('r+') do |f|
while line = f.gets # rubocop:disable Lint/AssignmentInCondition
next unless line.start_with?("command=\"#{self.class.command_key(@key_id)}\"")
+
f.seek(-line.length, IO::SEEK_CUR)
# Overwrite the line with #'s. Because the 'line' variable contains
# a terminating '\n', we write line.length - 1 '#' characters.
@@ -157,7 +158,7 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength
def check_permissions
open_auth_file(File::RDWR | File::CREAT) { true }
- rescue => ex
+ rescue StandardError => ex
puts "error: could not open #{auth_file}: #{ex}"
if File.exist?(auth_file)
system('ls', '-l', auth_file)
@@ -170,7 +171,7 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength
def lock(timeout = 10)
File.open(lock_file, "w+") do |f|
- begin
+ begin # rubocop:disable Style/RedundantBegin
f.flock File::LOCK_EX
Timeout.timeout(timeout) { yield }
ensure
diff --git a/lib/gitlab_lfs_authentication.rb b/lib/gitlab_lfs_authentication.rb
index ccd6d69..ff917bc 100644
--- a/lib/gitlab_lfs_authentication.rb
+++ b/lib/gitlab_lfs_authentication.rb
@@ -13,7 +13,7 @@ class GitlabLfsAuthentication
def self.build_from_json(json)
values = JSON.parse(json)
new(values['username'], values['lfs_token'], values['repository_http_path'])
- rescue
+ rescue StandardError
nil
end
diff --git a/lib/gitlab_logger.rb b/lib/gitlab_logger.rb
index 67f6030..2eaeac7 100644
--- a/lib/gitlab_logger.rb
+++ b/lib/gitlab_logger.rb
@@ -7,15 +7,15 @@ require_relative 'gitlab_config'
def convert_log_level(log_level)
Logger.const_get(log_level.upcase)
rescue NameError
- $stderr.puts "WARNING: Unrecognized log level #{log_level.inspect}."
- $stderr.puts "WARNING: Falling back to INFO."
+ warn "WARNING: Unrecognized log level #{log_level.inspect}."
+ warn "WARNING: Falling back to INFO."
Logger::INFO
end
class GitlabLogger
# Emulate the quoting logic of logrus
# https://github.com/sirupsen/logrus/blob/v1.0.5/text_formatter.go#L143-L156
- SHOULD_QUOTE = /[^a-zA-Z0-9\-._\/@^+]/
+ SHOULD_QUOTE = /[^a-zA-Z0-9\-._\/@^+]/.freeze
LEVELS = {
Logger::INFO => 'info'.freeze,
diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb
index 4eac8f5..1c2954c 100644
--- a/lib/gitlab_net.rb
+++ b/lib/gitlab_net.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require 'net/http'
require 'openssl'
require 'json'
@@ -11,7 +13,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
include HTTPHelper
CHECK_TIMEOUT = 5
- API_INACCESSIBLE_MESSAGE = 'API is not accessible'.freeze
+ API_INACCESSIBLE_MESSAGE = 'API is not accessible'
def check_access(cmd, gl_repository, repo, who, changes, protocol, env: {})
changes = changes.join("\n") unless changes.is_a?(String)
@@ -76,8 +78,8 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
def merge_request_urls(gl_repository, repo_path, changes)
changes = changes.join("\n") unless changes.is_a?(String)
changes = changes.encode('UTF-8', 'ASCII', invalid: :replace, replace: '')
- url = "#{internal_api_endpoint}/merge_request_urls?project=#{URI.escape(repo_path)}&changes=#{URI.escape(changes)}"
- url += "&gl_repository=#{URI.escape(gl_repository)}" if gl_repository
+ url = "#{internal_api_endpoint}/merge_request_urls?project=#{uri_escape(repo_path)}&changes=#{uri_escape(changes)}"
+ url += "&gl_repository=#{uri_escape(gl_repository)}" if gl_repository
resp = get(url)
if resp.code == '200'
@@ -85,7 +87,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
else
[]
end
- rescue
+ rescue StandardError
[]
end
@@ -94,9 +96,9 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
end
def authorized_key(key)
- resp = get("#{internal_api_endpoint}/authorized_keys?key=#{URI.escape(key, '+/=')}")
+ resp = get("#{internal_api_endpoint}/authorized_keys?key=#{URI.escape(key, '+/=')}") # rubocop:disable Lint/UriEscapeUnescape
JSON.parse(resp.body) if resp.code == "200"
- rescue
+ rescue StandardError
nil
end
@@ -106,7 +108,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
resp = post("#{internal_api_endpoint}/two_factor_recovery_codes", id_sym => id)
JSON.parse(resp.body) if resp.code == '200'
- rescue
+ rescue StandardError
{}
end
@@ -115,7 +117,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
resp = post("#{internal_api_endpoint}/notify_post_receive", params)
resp.code == '200'
- rescue
+ rescue StandardError
false
end
@@ -165,4 +167,8 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
def sanitize_path(repo)
repo.delete("'")
end
+
+ def uri_escape(str)
+ URI.escape(str) # rubocop:disable Lint/UriEscapeUnescape
+ end
end
diff --git a/lib/gitlab_shell.rb b/lib/gitlab_shell.rb
index 3515d41..87da693 100644
--- a/lib/gitlab_shell.rb
+++ b/lib/gitlab_shell.rb
@@ -81,23 +81,23 @@ class GitlabShell # rubocop:disable Metrics/ClassLength
true
rescue GitlabNet::ApiUnreachableError
- $stderr.puts "GitLab: Failed to authorize your Git request: internal API unreachable"
+ warn "GitLab: Failed to authorize your Git request: internal API unreachable"
false
rescue AccessDeniedError => ex
$logger.warn('Access denied', command: origin_cmd, user: log_username)
- $stderr.puts "GitLab: #{ex.message}"
+ warn "GitLab: #{ex.message}"
false
rescue DisallowedCommandError
$logger.warn('Denied disallowed command', command: origin_cmd, user: log_username)
- $stderr.puts "GitLab: Disallowed command"
+ warn "GitLab: Disallowed command"
false
rescue InvalidRepositoryPathError
- $stderr.puts "GitLab: Invalid repository path"
+ warn "GitLab: Invalid repository path"
false
rescue Action::Custom::BaseError => ex
$logger.warn('Custom action error', exception: ex.class, message: ex.message,
command: origin_cmd, user: log_username)
- $stderr.puts ex.message
+ warn ex.message
false
end
@@ -203,7 +203,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength
begin
if defined?(@who)
@user = api.discover(@who)
- @gl_id = "user-#{@user['id']}" if @user && @user.key?('id')
+ @gl_id = "user-#{@user['id']}" if @user&.key?('id')
else
@user = api.discover(@gl_id)
end
diff --git a/lib/http_helper.rb b/lib/http_helper.rb
index c66df53..f6001ea 100644
--- a/lib/http_helper.rb
+++ b/lib/http_helper.rb
@@ -74,7 +74,7 @@ module HTTPHelper
begin
start_time = Time.new
response = http.start { http.request(request) }
- rescue => e
+ rescue StandardError => e
$logger.warn('Failed to connect', method: method.to_s.upcase, url: url, error: e)
raise GitlabNet::ApiUnreachableError
ensure
diff --git a/lib/httpunix.rb b/lib/httpunix.rb
index 4f68d9a..230db46 100644
--- a/lib/httpunix.rb
+++ b/lib/httpunix.rb
@@ -12,7 +12,7 @@ module URI
def hostname
# decode %XX from path to file
v = host
- URI.decode(v)
+ URI.decode(v) # rubocop:disable Lint/UriEscapeUnescape
end
# port is not allowed in URI
diff --git a/spec/gitlab_logger_spec.rb b/spec/gitlab_logger_spec.rb
index a9cd3fb..71f4737 100644
--- a/spec/gitlab_logger_spec.rb
+++ b/spec/gitlab_logger_spec.rb
@@ -3,11 +3,13 @@ require_relative '../lib/gitlab_logger'
require 'securerandom'
describe :convert_log_level do
- subject { convert_log_level :extreme }
+ it "returns desired Logger::<type>" do
+ expect(convert_log_level(:debug)).to eq(Logger::DEBUG)
+ end
it "converts invalid log level to Logger::INFO" do
- expect($stderr).to receive(:puts).at_least(:once)
- is_expected.to eq(Logger::INFO)
+ expect(Warning).to receive(:warn).at_least(:once)
+ expect(convert_log_level(:extreme)).to eq(Logger::INFO)
end
end