From 80e8997d860e65fa11543108e55d4d1c8e18c28a Mon Sep 17 00:00:00 2001 From: Dave Machado Date: Tue, 11 Jul 2017 00:21:32 -0400 Subject: [PATCH 1/7] Add link validation script using in-house logic --- build/main.sh | 12 +++++++---- build/validate_format.rb | 45 ++++++++++++++++++++++++++++++++++++++++ build/validate_links.rb | 33 +++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) create mode 100755 build/validate_format.rb create mode 100755 build/validate_links.rb diff --git a/build/main.sh b/build/main.sh index e3af7e03..74d1566c 100755 --- a/build/main.sh +++ b/build/main.sh @@ -1,8 +1,6 @@ #!/bin/bash - echo "running format validation..." -./validate.rb ../README.md - +./validate_format.rb ../README.md if [[ $? != 0 ]]; then echo "format validation failed!" exit $? @@ -12,5 +10,11 @@ fi if [ "$TRAVIS_BRANCH" == "master" ]; then echo "running link validation..." - awesome_bot ../README.md --allow-ssl --allow 403,302 + ./validate_links.rb ../README.md + if [[ $? != 0 ]]; then + echo "link validation failed!" + exit $? + else + echo "link validation passed!" + fi fi diff --git a/build/validate_format.rb b/build/validate_format.rb new file mode 100755 index 00000000..820ef7d2 --- /dev/null +++ b/build/validate_format.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby +auth_keys = ['apiKey', 'OAuth', 'X-Mashape-Key', 'No'] +https_keys = ['Yes', 'No'] +args = ARGV +filename = args[0] +fail_flag = false +File.foreach(filename).with_index do |line, line_num| + line_num += 1 + if line.start_with?('|') + # Skip table schema lines + if line.eql? "|---|---|---|---|---|\n" + next + end + values = line.split("|") + # Check Description to make sure first character is capitalized + desc_val = values[2].lstrip.chop + if !/[[:upper:]]/.match(desc_val[0]) + puts "(#{line_num}) Invalid Description (first char not uppercase): #{desc_val}" + fail_flag = true + end + # Check Auth values to conform to valid options only + auth_val = values[3].lstrip.chop.tr('``', '') + if !auth_keys.include?(auth_val) + puts "(#{line_num}) Invalid Auth (not a valid option): #{auth_val}" + fail_flag = true + end + # Check HTTPS Support values to be either "Yes" or "No" + https_val = values[4].lstrip.chop + if !https_keys.include?(https_val) + puts "(#{line_num}) Invalid HTTPS: (must use \"Yes\" or \"No\"): #{https_val}" + fail_flag = true + end + # Check Link to ensure url is wrapped in "[Go!]" view + link_val = values[5].lstrip.chop + if !link_val.start_with?("[Go!](") || !link_val.end_with?(')') + puts "(#{line_num}) Invalid Link: (format should be \"[Go!]()\"): #{link_val}" + fail_flag = true + end + end +end +if fail_flag + exit(1) +else + exit(0) +end diff --git a/build/validate_links.rb b/build/validate_links.rb new file mode 100755 index 00000000..813f1c16 --- /dev/null +++ b/build/validate_links.rb @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby +require 'faraday' +require 'uri' +allowed_codes = [200, 302, 403] +args = ARGV +filename = args[0] +fail_flag = false +contents = File.open(filename, 'rb') { |f| f.read } +links = URI.extract(contents, ['http', 'https']) +dup = links.select{|element| links.count(element) > 1 } +if dup.uniq.length > 0 + dup.uniq.each do |link| + if link.end_with?(')') + puts link[0...-1] + end + end + exit(1) +end +links.each do |link| + if link.end_with?(')') + link = link[0...-1] + end + res = Faraday.get(link) + if !allowed_codes.include?(res.status) + puts "(#{res.status}): #{link}" + fail_flag = true + end +end +if fail_flag + exit(1) +else + exit(0) +end From 5fbf817c1c80bf7808e036308d5897bbb62b58e6 Mon Sep 17 00:00:00 2001 From: Dave Machado Date: Wed, 12 Jul 2017 09:32:38 -0400 Subject: [PATCH 2/7] check for duplicate urls --- build/validate_links.rb | 52 ++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/build/validate_links.rb b/build/validate_links.rb index 813f1c16..8e5fd641 100755 --- a/build/validate_links.rb +++ b/build/validate_links.rb @@ -1,31 +1,55 @@ #!/usr/bin/env ruby -require 'faraday' +require 'httparty' require 'uri' allowed_codes = [200, 302, 403] args = ARGV filename = args[0] fail_flag = false contents = File.open(filename, 'rb') { |f| f.read } -links = URI.extract(contents, ['http', 'https']) -dup = links.select{|element| links.count(element) > 1 } +raw_links = URI.extract(contents, ['http', 'https']) +# Remove trailing ')' from entry URLs +links = [] +raw_links.each do |link| + if link.end_with?(')') + links.push(link[0...-1]) + else + links.push(link) + end +end +# Fail on any duplicate elements +dup = links.select{|element| links.count(element) > 1} if dup.uniq.length > 0 - dup.uniq.each do |link| - if link.end_with?(')') - puts link[0...-1] - end + dup.uniq.each do |e| + puts "Duplicate link: #{e}" end - exit(1) + fail_flag = true end +# Remove any duplicates from array +links = links.uniq +count = 0 +total = links.length +fails = [] +# GET each link and check for valid response code from allowed_codes links.each do |link| - if link.end_with?(')') - link = link[0...-1] - end - res = Faraday.get(link) - if !allowed_codes.include?(res.status) - puts "(#{res.status}): #{link}" + begin + count += 1 + puts "(#{count}/#{total}) #{link}" + res = HTTParty.get(link, timeout: 10) + if !allowed_codes.include?(res.code) + fails.push("(#{res.code}): #{link}") + fail_flag = true + else + puts "\t(#{res.code})" + end + rescue + puts "FAIL: (#{res.code}) #{link}" + fails.push("(#{res.code}): #{link}") fail_flag = true end end +fails.each do |e| + puts e +end if fail_flag exit(1) else From 708f4b7fa9d44510254fb2c940398f6099b1bf1d Mon Sep 17 00:00:00 2001 From: Dave Machado Date: Wed, 12 Jul 2017 09:37:42 -0400 Subject: [PATCH 3/7] integrate link validation into CI --- .travis.yml | 3 +-- build/validate.rb | 52 ----------------------------------------- build/validate_links.rb | 9 +++++-- 3 files changed, 8 insertions(+), 56 deletions(-) delete mode 100755 build/validate.rb diff --git a/.travis.yml b/.travis.yml index e702877d..3215f727 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ notifications: before_install: - rvm install 2.4.0 install: - - gem install awesome_bot + - gem install httparty before_script: - cd build script: @@ -12,4 +12,3 @@ script: after_success: - ./build.sh - ./deploy.sh - diff --git a/build/validate.rb b/build/validate.rb deleted file mode 100755 index eecfa514..00000000 --- a/build/validate.rb +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env ruby -auth_keys = ['apiKey', 'OAuth', 'X-Mashape-Key', 'No'] -https_keys = ['Yes', 'No'] - -args = ARGV -filename = args[0] -fail_flag = false - -File.foreach(filename).with_index do |line, line_num| - line_num += 1 - if line.start_with?('|') - # Skip table schema lines - if line.eql? "|---|---|---|---|---|\n" - next - end - values = line.split("|") - - # Check Description to make sure first character is capitalized - desc_val = values[2].lstrip.chop - if !/[[:upper:]]/.match(desc_val[0]) - puts "(#{line_num}) Invalid Description (first char not uppercase): #{desc_val}" - fail_flag = true - end - - # Check Auth values to conform to valid options only - auth_val = values[3].lstrip.chop.tr('``', '') - if !auth_keys.include?(auth_val) - puts "(#{line_num}) Invalid Auth (not a valid option): #{auth_val}" - fail_flag = true - end - - # Check HTTPS Support values to be either "Yes" or "No" - https_val = values[4].lstrip.chop - if !https_keys.include?(https_val) - puts "(#{line_num}) Invalid HTTPS: (must use \"Yes\" or \"No\"): #{https_val}" - fail_flag = true - end - - # Check Link to ensure url is wrapped in "[Go!]" view - link_val = values[5].lstrip.chop - if !link_val.start_with?("[Go!](") || !link_val.end_with?(')') - puts "(#{line_num}) Invalid Link: (format should be \"[Go!]()\"): #{link_val}" - fail_flag = true - end - end -end - -if fail_flag - exit(1) -else - exit(0) -end diff --git a/build/validate_links.rb b/build/validate_links.rb index 8e5fd641..80241835 100755 --- a/build/validate_links.rb +++ b/build/validate_links.rb @@ -16,11 +16,12 @@ raw_links.each do |link| links.push(link) end end +fails = [] # Fail on any duplicate elements dup = links.select{|element| links.count(element) > 1} if dup.uniq.length > 0 dup.uniq.each do |e| - puts "Duplicate link: #{e}" + fails.push("Duplicate link: #{e}") end fail_flag = true end @@ -28,7 +29,6 @@ end links = links.uniq count = 0 total = links.length -fails = [] # GET each link and check for valid response code from allowed_codes links.each do |link| begin @@ -47,6 +47,11 @@ links.each do |link| fail_flag = true end end +if fails.length <= 0 + puts "all links valid" +else + puts "-- RESULTS --" +end fails.each do |e| puts e end From 2e9088ecf2dcc0384da758d8d0be63025ba1322f Mon Sep 17 00:00:00 2001 From: Dave Machado Date: Wed, 12 Jul 2017 12:10:26 -0400 Subject: [PATCH 4/7] 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 From 17a0caf74953107c13ab2ecfd35c01de2fc6ec49 Mon Sep 17 00:00:00 2001 From: Dave Machado Date: Wed, 12 Jul 2017 12:13:17 -0400 Subject: [PATCH 5/7] Add Gem dependencies to Travis config --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3215f727..9d24e391 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ notifications: before_install: - rvm install 2.4.0 install: - - gem install httparty + - gem install httparty ruby-progressbar before_script: - cd build script: From 263d095c6574f391b622a73e9bd512be9f96aba1 Mon Sep 17 00:00:00 2001 From: Dave Machado Date: Wed, 12 Jul 2017 12:17:52 -0400 Subject: [PATCH 6/7] Use standard error reporting for duplicate links --- build/validate_links.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/validate_links.rb b/build/validate_links.rb index 51e157d3..ac28d86f 100755 --- a/build/validate_links.rb +++ b/build/validate_links.rb @@ -21,7 +21,7 @@ fails = [] dup = links.select{|element| links.count(element) > 1} if dup.uniq.length > 0 dup.uniq.each do |e| - fails.push("Duplicate link: #{e}") + fails.push("(DUP): #{e}") end end # Remove any duplicates from array From 92bcf9d046e2c76b53d1d319edc8128c08b3f66f Mon Sep 17 00:00:00 2001 From: Dave Machado Date: Wed, 12 Jul 2017 23:00:01 -0400 Subject: [PATCH 7/7] Sort final failfure array for easy reading --- build/validate_links.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/build/validate_links.rb b/build/validate_links.rb index ac28d86f..eda222f4 100755 --- a/build/validate_links.rb +++ b/build/validate_links.rb @@ -47,8 +47,8 @@ links.each do |link| fails.push("(SSL): #{link}") rescue SocketError fails.push("(SOK): #{link}") - rescue - fails.push("(ERR): #{link}") + rescue Errno::ECONNREFUSED + fails.push("(CON): #{link}") end progressbar.increment end @@ -58,6 +58,7 @@ if fails.length <= 0 exit(0) else puts "-- RESULTS --" + fails.sort! fails.each do |e| puts e end