rosa-abf/rosa-build

View on GitHub
app/models/platform_content.rb

Summary

Maintainability
B
4 hrs
Test Coverage
class PlatformContent
 
attr_reader :path
 
def initialize(platform, path, build_list = nil)
@platform, @path = platform, path
if build_list
@build_list = build_list
end
end
 
Method `build_list` has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
def build_list
return @build_list if !!@build_list
return nil if @path !~ /\/(release|updates)+\/[\w\-\.\+\~]+$/
return nil unless repository_name = @path.match(/\/[\w]+\/(release|updates)\//)
repository_name = repository_name[0].gsub(/\/(release|updates)\/$/, '').gsub('/', '')
 
repository = @platform.repositories.where(name: repository_name).first
return nil unless repository
 
if @platform.main?
build_for_platform = @platform
else
bfp_name = @path.match(/\/#{@platform.name}\/repository\/[\w]+\//)
Avoid too many `return` statements within this method.
return nil unless bfp_name
bfp_name = bfp_name[0].gsub(/\/#{@platform.name}\/repository\//, '').gsub('/', '')
build_for_platform = Platform.main.find_by name: bfp_name
Avoid too many `return` statements within this method.
return nil unless build_for_platform
end
 
@build_list = BuildList.for_status(BuildList::BUILD_PUBLISHED)
.for_platform(build_for_platform)
.scoped_to_save_platform(@platform)
.where(save_to_repository_id: repository)
.where(build_list_packages: {fullname: name, actual: true})
.joins(:packages)
.last
 
Avoid too many `return` statements within this method.
return @build_list
end
 
def name
@name ||= @path.gsub(/.*#{File::SEPARATOR}/, '')
end
 
def size
@size ||= File.size(@path) rescue nil
end
 
def is_folder?
@is_folder.nil? ? (@is_folder = File.directory?(@path)) : @is_folder
end
 
def download_url
"#{APP_CONFIG['downloads_url']}/#{@platform.name}#{subpath}"
end
 
def subpath
@subpath ||= @path.gsub(/^#{@platform.path}/, '')
end
 
Method `find_by_platform` has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
def self.find_by_platform(platform, path, term)
# Strip out the non-ascii character
path = sanitize_path(path)
 
if term =~ /^bl:\d+$/
id = term[3..-1].to_i
bl = BuildList.find_by_id(id)
if bl != nil
bl.packages.pluck(:fullname).sort_by(&:length).
map { |name| File.join(platform.path, path, name) }.
select { |p| File.file?(p) }.
map { |p| PlatformContent.new(platform, p, bl) }
else
[]
end
else
term = (term || '').strip.gsub(/[\\\/]+/, '')
.gsub(/[^\w\-\+\.\~]/, '_')
results = Dir.glob(File.join(platform.path, path, "*#{term}*"))
if term
results = results.sort_by(&:length)
else
results = results.sort
end
results.map{ |p| PlatformContent.new(platform, p) }
end
end
 
def self.remove_file(platform, path)
path = File.join(platform.path, sanitize_path(path))
FileUtils.rm_f(path) if File.exist?(path)
end
 
def self.sanitize_path(path)
path.split(File::SEPARATOR).map(&:strip).select(&:present?)
.map{ |p|
# Strip out the non-ascii character
p.gsub(/[\\\/]+/, '')
.gsub(/^[\.]+/, '')
.gsub(/[^\w\-\+\.\~]/, '_')
}.join(File::SEPARATOR)
end
 
end