Skip to content

Commit

Permalink
[ci][api][webui] Fix rubocop offenses. Rescue StandardError instead o…
Browse files Browse the repository at this point in the history
…f Exception for Backend::File. Don't use Runtime.new to raise a Runtime error, just raise the string. Use class instance var in Backend::Connection
  • Loading branch information
Moises Deniz Aleman committed Apr 6, 2017
1 parent 0ad62af commit 636ea03
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 61 deletions.
12 changes: 6 additions & 6 deletions src/api/app/helpers/maintenance_helper.rb
Expand Up @@ -161,9 +161,9 @@ def release_package_copy_sources(action, sourcePackage, targetPackageName, targe
end
cp_path = "/source/#{CGI.escape(targetProject.name)}/#{CGI.escape(targetPackageName)}"
cp_path << Backend::Connection.build_query_from_hash(cp_params, [:cmd, :user, :oproject,
:opackage, :comment, :requestid,
:expand, :withvrev, :noservice,
:freezelink, :withacceptinfo])
:opackage, :comment, :requestid,
:expand, :withvrev, :noservice,
:freezelink, :withacceptinfo])
result = Backend::Connection.post(cp_path)
result = Xmlhash.parse(result.body)
action.set_acceptinfo(result["acceptinfo"]) if action
Expand All @@ -177,7 +177,7 @@ def copy_binaries(filterSourceRepository, sourcePackage, targetPackageName, targ
# FIXME: filter given release and/or target repos here
if releasetarget.target_repository.project == targetProject
uID = copy_binaries_to_repository(sourceRepo, sourcePackage, releasetarget.target_repository, targetPackageName, setrelease)
updateIDs << uID if uID
updateIDs << uID if uID
end
# remove maintenance release trigger in source
if releasetarget.trigger == 'maintenance'
Expand Down Expand Up @@ -215,8 +215,8 @@ def copy_single_binary(arch, target_repository, sourcePackage, sourceRepo, targe
cp_path = "/build/#{CGI.escape(target_repository.project.name)}/#{URI.escape(target_repository.name)}/#{URI.escape(arch.name)}/#{URI.escape(targetPackageName)}"
# rubocop:enable Metrics/LineLength
cp_path << Backend::Connection.build_query_from_hash(cp_params, [:cmd, :oproject, :opackage,
:orepository, :setupdateinfoid,
:resign, :setrelease, :multibuild])
:orepository, :setupdateinfoid,
:resign, :setrelease, :multibuild])
Backend::Connection.post cp_path
end

Expand Down
4 changes: 2 additions & 2 deletions src/api/app/models/bs_request_action_maintenance_incident.rb
Expand Up @@ -153,8 +153,8 @@ def _merge_pkg_into_maintenance_incident(incidentProject)
cp_params[:orev] = source_rev if source_rev
cp_path = "/source/#{CGI.escape(incidentProject.name)}/#{CGI.escape(new_pkg.name)}"
cp_path << Backend::Connection.build_query_from_hash(cp_params, [:cmd, :user, :oproject, :opackage,
:orev, :keeplink, :expand, :comment,
:requestid, :withacceptinfo])
:orev, :keeplink, :expand, :comment,
:requestid, :withacceptinfo])
result = Backend::Connection.post cp_path
result = Xmlhash.parse(result.body)
set_acceptinfo(result["acceptinfo"])
Expand Down
6 changes: 3 additions & 3 deletions src/api/app/models/bs_request_action_submit.rb
Expand Up @@ -85,9 +85,9 @@ def execute_accept(opts)

cp_path = "/source/#{self.target_project}/#{self.target_package}"
cp_path << Backend::Connection.build_query_from_hash(cp_params, [:cmd, :user, :oproject, :opackage,
:orev, :expand, :keeplink, :comment,
:requestid, :dontupdatesource, :noservice,
:withacceptinfo])
:orev, :expand, :keeplink, :comment,
:requestid, :dontupdatesource, :noservice,
:withacceptinfo])
result = Backend::Connection.post cp_path
result = Xmlhash.parse(result.body)
set_acceptinfo(result["acceptinfo"])
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/models/event/base.rb
Expand Up @@ -143,8 +143,8 @@ def notify_backend
p['time'] = created_at.to_i
logger.debug "notify_backend #{self.class.name} #{p.inspect}"
ret = Backend::Connection.post("/notify_plugins/#{self.class.raw_type}",
Yajl::Encoder.encode(p),
'Content-Type' => 'application/json')
Yajl::Encoder.encode(p),
'Content-Type' => 'application/json')
Xmlhash.parse(ret.body)['code'] == 'ok'
end

Expand Down
4 changes: 2 additions & 2 deletions src/api/app/models/project.rb
Expand Up @@ -1059,8 +1059,8 @@ def do_project_copy( params )
begin
path = "/source/#{URI.escape(name)}"
path << Backend::Connection.build_query_from_hash(params,
[:cmd, :user, :comment, :oproject, :withbinaries, :withhistory,
:makeolder, :makeoriginolder, :noservice])
[:cmd, :user, :comment, :oproject, :withbinaries, :withhistory,
:makeolder, :makeoriginolder, :noservice])
Backend::Connection.post path
rescue ActiveXML::Transport::Error => e
logger.debug "copy failed: #{e.summary}"
Expand Down
42 changes: 22 additions & 20 deletions src/api/lib/backend/connection.rb
Expand Up @@ -7,8 +7,8 @@ class Connection
@source_host = CONFIG['source_host']
@source_port = CONFIG['source_port']

@@backend_logger = Logger.new("#{Rails.root}/log/backend_access.log")
@@backend_time = 0
@backend_logger = Logger.new("#{Rails.root}/log/backend_access.log")
@backend_time = 0

def initialize
Rails.logger.debug "init backend"
Expand All @@ -26,11 +26,11 @@ def port
end

def runtime
@@backend_time
@backend_time
end

def reset_runtime
@@backend_time = 0
@backend_time = 0
end

def logger
Expand Down Expand Up @@ -84,9 +84,11 @@ def put_or_post(method, path, data, in_headers)
end
begin
http.request backend_request
# rubocop:disable Lint/ShadowedException
rescue Errno::EPIPE, Errno::ECONNRESET, SocketError, Errno::EINTR, EOFError, IOError, Errno::ETIMEDOUT
raise Timeout::Error
end
# rubocop:enable Lint/ShadowedException
end
write_backend_log method, host, port, path, response
handle_response response
Expand Down Expand Up @@ -151,19 +153,19 @@ def write_backend_log(method, host, port, path, response)
raise "write backend log without start time" unless @start_of_last
timedelta = Time.now - @start_of_last
@start_of_last = nil
@@backend_logger.info "#{now} #{method} #{host}:#{port}#{path} #{response.code} #{timedelta}"
@@backend_time += timedelta
logger.debug "request took #{timedelta} #{@@backend_time}"
@backend_logger.info "#{now} #{method} #{host}:#{port}#{path} #{response.code} #{timedelta}"
@backend_time += timedelta
logger.debug "request took #{timedelta} #{@backend_time}"

return unless CONFIG['extended_backend_log']

data = response.body
if data.nil?
@@backend_logger.info "(no data)"
@backend_logger.info "(no data)"
elsif data.class == 'String' && data[0, 1] == "<"
@@backend_logger.info data
@backend_logger.info data
else
@@backend_logger.info "(non-XML data) #{data.class}"
@backend_logger.info "(non-XML data) #{data.class}"
end
end

Expand All @@ -180,7 +182,7 @@ def handle_response(response)
end
end

@@backend = nil
@backend = nil

public

Expand All @@ -195,24 +197,24 @@ def without_global_write_through
end

def test_backend?
(!@@backend.nil? && @@backend != :dont)
(!@backend.nil? && @backend != :dont)
end

def do_not_start_test_backend
@@backend = :dont
@backend = :dont
end

def start_test_backend
# do_not_start_test_backend
return unless Rails.env.test?
return if @@backend
return if @backend
return if ENV['BACKEND_STARTED']
print "Starting test backend..."
@@backend = IO.popen("#{Rails.root}/script/start_test_backend")
logger.debug "Test backend started with pid: #{@@backend.pid}"
@backend = IO.popen("#{Rails.root}/script/start_test_backend")
logger.debug "Test backend started with pid: #{@backend.pid}"
loop do
line = @@backend.gets
raise RuntimeError.new('Backend died') unless line
line = @backend.gets
raise 'Backend died' unless line
break if line =~ /DONE NOW/
logger.debug line.strip
end
Expand All @@ -222,8 +224,8 @@ def start_test_backend
ENV['BACKEND_STARTED'] = '1'
at_exit do
puts "Killing test backend"
Process.kill "INT", @@backend.pid
@@backend = nil
Process.kill "INT", @backend.pid
@backend = nil
end
end

Expand Down
14 changes: 7 additions & 7 deletions src/api/lib/backend/file.rb
Expand Up @@ -49,7 +49,7 @@ def file(query = {})
@last_read_query = query
end
@file
rescue Exception => e
rescue => e
@backend_file_errors = e.message
valid?
nil
Expand All @@ -68,7 +68,7 @@ def reload
file(@last_read_query)
end

# Tries to save the file to the backend and return the response, otherwise will raise an Exception
# Tries to save the file to the backend and return the response, otherwise will raise an StandardError
# If "content" parameter is provided then is passed directly to the backend, otherwise it creates
# a temp file and then send it to the backend
def save!(query = {}, content = nil)
Expand All @@ -84,26 +84,26 @@ def save!(query = {}, content = nil)
backend_response
end

# Tries to save the file to the backend. Returns nil if some Exception is raised
# Tries to save the file to the backend. Returns nil if some StandardError is raised
def save(query = {}, content = nil)
save!(query, content)
rescue Exception => e
rescue => e
@backend_file_errors = e.message
valid?
nil
end

# Tries to destroy the file from the backend, freeze the object and return the response, otherwise will raise an Exception
# Tries to destroy the file from the backend, freeze the object and return the response, otherwise will raise an StandardError
def destroy!(query = {})
response = Connection.delete(full_path(query))
freeze
response
end

# Tries to destroy the file from the backend. Returns nil if some Exception is raised
# Tries to destroy the file from the backend. Returns nil if some StandardError is raised
def destroy(query = {})
destroy!(query)
rescue Exception => e
rescue => e
@backend_file_errors = e.message
valid?
nil
Expand Down

0 comments on commit 636ea03

Please sign in to comment.