Skip to content

Commit

Permalink
rubocop (ACAB) linter improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bmos committed Feb 27, 2024
1 parent d2ee179 commit a4ad874
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require:
- rubocop-rake

Layout/LineLength:
Max: 120
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ gem 'pluto-models'
gem 'rake'
gem 'rss'
gem 'webrick'
gem 'rubocop', require: false
gem 'rubocop-rake', require: false
27 changes: 27 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ GEM
logutils (>= 0.6.1)
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
colorator (1.1.0)
concurrent-ruby (1.2.2)
date-formatter (0.1.1)
Expand Down Expand Up @@ -83,10 +84,12 @@ GEM
sass-embedded (~> 1.54)
jekyll-watch (2.2.1)
listen (~> 3.0)
json (2.7.1)
kramdown (2.4.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
language_server-protocol (3.17.0.3)
liquid (4.0.4)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
Expand All @@ -108,6 +111,10 @@ GEM
fetcher (>= 0.4.5)
liquid (>= 4.0.0)
logutils (>= 0.6.1)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
racc
pathutil (0.16.2)
forwardable-extended (~> 2.6)
pluto (1.3.4)
Expand Down Expand Up @@ -150,14 +157,32 @@ GEM
props (>= 1.2.0)
public_suffix (5.0.3)
racc (1.7.1)
rainbow (3.1.1)
rake (13.0.6)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
regexp_parser (2.9.0)
rexml (3.2.5)
rouge (4.1.2)
rss (0.2.9)
rexml
rubocop (1.60.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
safe_yaml (1.0.5)
Expand Down Expand Up @@ -196,6 +221,8 @@ DEPENDENCIES
pluto-models
rake
rss
rubocop
rubocop-rake
webrick

BUNDLED WITH
Expand Down
119 changes: 52 additions & 67 deletions tests/feedcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,27 @@
require 'faraday/follow_redirects'
require 'iniparser'
require 'nokogiri'
require 'thread'
require 'uri'

INI_FILE = 'planet.ini'
AV_DIR = 'hackergotchi'

def initialize_faraday
Faraday.new(request: {open_timeout: 10}) do |f|
f.response :follow_redirects
f.adapter :net_http
end
end

def check_avatar(avatar, av_dir, faraday)
result = ["_ ", false]
result = ['_ ', false]

if avatar
if avatar.include? '//'
result = check_url(avatar, faraday)
else
result = ['✓ ', false]
unless File.file?("#{av_dir}/#{avatar}")
result = ["✗\nAvatar not found: hackergotchi/#{avatar} ", true]
end
result = ["✗\nAvatar not found: hackergotchi/#{avatar} ", true] unless File.file?("#{av_dir}/#{avatar}")
end
end
result
end

def check_url(url, faraday)
error_message = "✗ "
error_message = '✗ '

begin
res = faraday.get(URI(url))
Expand All @@ -45,7 +35,9 @@ def check_url(url, faraday)
end

error = "#{error_message}Non successful status code #{res.status} when trying to access '#{url}' "
return ["#{error}\nTry using '#{res.headers['location']}' instead", true] if res.status.to_i.between?(300, 399) && res.headers.key?('location')
if res.status.to_i.between?(300, 399) && res.headers.key?('location')
return ["#{error}\nTry using '#{res.headers['location']}' instead", true]
end

return [error, true] unless res.status.to_i == 200

Expand All @@ -54,23 +46,22 @@ def check_url(url, faraday)

def check_urls(url_arr, faraday)
results = url_arr.map { |url| check_url(url, faraday) }
[results.map(&:first).join, results.any? { |r| r.last }]
[results.map(&:first).join, results.any?(&:last)]
end

def parse_xml(feed, faraday)
result = ["✗ ", true]
result = ['✗ ', true]

begin
xml = faraday.get(URI(feed))
rescue Faraday::ConnectionFailed
return ["#{result.first}Connection Failure when trying to read XML from '#{feed}' ", true]
["#{result.first}Connection Failure when trying to read XML from '#{feed}' ", true]
rescue Faraday::SSLError
return ["#{result.first}SSL Error when trying to read XML from '#{feed}' ", true]
["#{result.first}SSL Error when trying to read XML from '#{feed}' ", true]
else
xml_err = Nokogiri::XML(xml.body).errors
unless xml_err.empty?
return ["#{result.first}Unusable XML syntax: #{feed}\n#{xml_err} ", true]
end
return ["#{result.first}Unusable XML syntax: #{feed}\n#{xml_err} ", true] unless xml_err.empty?

['✓ ', false]
end
end
Expand All @@ -79,9 +70,7 @@ def check_unused_files(av_dir, avatars)
hackergotchis = Dir.foreach(av_dir).select { |f| File.file?("#{av_dir}/#{f}") }
diff = (hackergotchis - avatars)

if diff.empty? || avatars.empty?
return [nil, false]
end
return [nil, false] if diff.empty? || avatars.empty?

["There are unused files in hackergotchis:\n#{diff.join(', ')}", true]
end
Expand All @@ -108,71 +97,67 @@ def check_source(key, section, faraday)
did_fail |= xml_result.last
end

return [result.compact.join, did_fail], avatar
[[result.compact.join, did_fail], avatar]
end

def create_job_summary(error_messages)
job_summary = ["# Feed Sources\n"]
job_summary = ['# Feed Sources\n']
error_messages.each do |error_message|
error_message_parts = error_message.split('=>')

header = error_message_parts[0]&.strip.sub(/^:: /, '')
header = error_message_parts[0]&.strip&.sub(/^:: /, '')
body = error_message_parts[1]&.strip

if header && body
job_summary << "\n## #{header}\n"
job_summary << "\n#{body}\n"
end
end
File.open("error-summary.md", "w") do |file|
File.open('error-summary.md', 'w') do |file|
file.write job_summary.reduce(:+)
end
end

def main
faraday = initialize_faraday()
planet_srcs = INI.load_file(INI_FILE)
planet_srcs = INI.load_file(INI_FILE)
did_any_fail = false
error_messages = []
avatars = []

did_any_fail = false
error_messages = []
avatars = []
faraday = Faraday.new(request: { open_timeout: 10 }) do |f|
f.response :follow_redirects
f.adapter :net_http
end

queue = Queue.new
planet_srcs.each do |key, section|
if ARGV.empty? || ARGV.include?(key)
queue.push([key, section])
end
end
queue = Queue.new
planet_srcs.each do |key, section|
queue.push([key, section]) if ARGV.empty? || ARGV.include?(key)
end

workers = (0...3).map do
Thread.new do
until queue.empty?
key, section = queue.pop
next unless section.is_a?(Hash)

res, avatar = check_source(key, section, faraday)
avatars << avatar
puts res.first if res.first
error_messages << res.first if res.last
did_any_fail ||= res.last
end
workers = (0...3).map do
Thread.new do
until queue.empty?
key, section = queue.pop
next unless section.is_a?(Hash)

res, avatar = check_source(key, section, faraday)
avatars << avatar
puts res.first if res.first
error_messages << res.first if res.last
did_any_fail ||= res.last
end
end
workers.each(&:join)

unused_files_result = check_unused_files(AV_DIR, avatars)
if unused_files_result.last
error_messages << unused_files_result.first
puts "[WARNING] #{unused_files_result.first}"
end
end
workers.each(&:join)

if did_any_fail
create_job_summary(error_messages)
abort
else
File.delete('error-summary.md') if File.exist?('error-summary.md')
puts "All feeds passed checks!"
end
unused_files_result = check_unused_files(AV_DIR, avatars)
if unused_files_result.last
error_messages << unused_files_result.first
puts "[WARNING] #{unused_files_result.first}"
end

main()
if did_any_fail
create_job_summary(error_messages)
abort
end
File.delete('error-summary.md') if File.exist?('error-summary.md')
puts 'All feeds passed checks!'

0 comments on commit a4ad874

Please sign in to comment.