From 2e9088ecf2dcc0384da758d8d0be63025ba1322f Mon Sep 17 00:00:00 2001 From: Dave Machado Date: Wed, 12 Jul 2017 12:10:26 -0400 Subject: [PATCH] Integrate link validation into CI --- build/main.sh | 4 ++-- build/validate_links.rb | 35 +++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/build/main.sh b/build/main.sh index 74d1566c..62636aa2 100755 --- a/build/main.sh +++ b/build/main.sh @@ -3,7 +3,7 @@ echo "running format validation..." ./validate_format.rb ../README.md if [[ $? != 0 ]]; then echo "format validation failed!" - exit $? + exit 1 else echo "format validation passed!" fi @@ -13,7 +13,7 @@ if [ "$TRAVIS_BRANCH" == "master" ]; then ./validate_links.rb ../README.md if [[ $? != 0 ]]; then echo "link validation failed!" - exit $? + exit 1 else echo "link validation passed!" fi diff --git a/build/validate_links.rb b/build/validate_links.rb index 80241835..51e157d3 100755 --- a/build/validate_links.rb +++ b/build/validate_links.rb @@ -1,10 +1,10 @@ #!/usr/bin/env ruby require 'httparty' +require 'ruby-progressbar' require 'uri' allowed_codes = [200, 302, 403] args = ARGV filename = args[0] -fail_flag = false contents = File.open(filename, 'rb') { |f| f.read } raw_links = URI.extract(contents, ['http', 'https']) # Remove trailing ')' from entry URLs @@ -23,40 +23,43 @@ if dup.uniq.length > 0 dup.uniq.each do |e| fails.push("Duplicate link: #{e}") end - fail_flag = true end # Remove any duplicates from array links = links.uniq count = 0 total = links.length +progressbar = ProgressBar.create(:total => total) # GET each link and check for valid response code from allowed_codes links.each do |link| begin count += 1 - puts "(#{count}/#{total}) #{link}" res = HTTParty.get(link, timeout: 10) + if res.code.nil? + fails.push("(NIL): #{link}") + next + end if !allowed_codes.include?(res.code) fails.push("(#{res.code}): #{link}") - fail_flag = true - else - puts "\t(#{res.code})" end + rescue Net::ReadTimeout + fails.push("(TMO): #{link}") + rescue OpenSSL::SSL::SSLError + fails.push("(SSL): #{link}") + rescue SocketError + fails.push("(SOK): #{link}") rescue - puts "FAIL: (#{res.code}) #{link}" - fails.push("(#{res.code}): #{link}") - fail_flag = true + fails.push("(ERR): #{link}") end + progressbar.increment end +puts "#{count}/#{total} links checked" if fails.length <= 0 puts "all links valid" + exit(0) else puts "-- RESULTS --" -end -fails.each do |e| - puts e -end -if fail_flag + fails.each do |e| + puts e + end exit(1) -else - exit(0) end