update coverage rake task

pull/1117/head
gabrielburnworth 2019-02-13 16:28:15 -08:00
parent 22a504046b
commit 9c3769d53a
2 changed files with 67 additions and 25 deletions

View File

@ -42,4 +42,4 @@ jobs:
- run: - run:
name: Check Coveralls coverage on staging name: Check Coveralls coverage on staging
command: | command: |
sudo docker-compose run -e CIRCLE_SHA1="$CIRCLE_SHA1" web rake coverage:run sudo docker-compose run -e CIRCLE_SHA1="$CIRCLE_SHA1" CIRCLE_BRANCH="$CIRCLE_BRANCH" web rake coverage:run

View File

@ -1,14 +1,34 @@
COVERAGE_FILE_PATH = "./coverage_fe/index.html" COVERAGE_FILE_PATH = "./coverage_fe/index.html"
THRESHOLD = 0.001 THRESHOLD = 0.001
REPO_URL = "https://api.github.com/repos/Farmbot/Farmbot-Web-App/git"\ REPO_URL = "https://api.github.com/repos/Farmbot/Farmbot-Web-App"
"/refs/heads/staging" CURRENT_BRANCH = ENV.fetch("CIRCLE_BRANCH", "staging")
CURRENT_COMMIT = ENV.fetch("CIRCLE_SHA1", "") CURRENT_COMMIT = ENV.fetch("CIRCLE_SHA1", "")
CSS_SELECTOR = ".fraction" CSS_SELECTOR = ".fraction"
FRACTION_DELIM = "/" FRACTION_DELIM = "/"
# Fetch JSON over HTTP. Rails probably already has a helper for this :shrug: # Fetch JSON over HTTP. Rails probably already has a helper for this :shrug:
def open_json(url) def open_json(url)
JSON.parse(open(url).read) begin
JSON.parse(open(url).read)
rescue OpenURI::HTTPError => exception
puts exception.message
return {}
end
end
# Assemble the coverage data URL using the provided branch.
def coverage_url(branch)
commit = open_json("#{REPO_URL}/git/refs/heads/#{branch}").dig("object", "sha")
return "https://coveralls.io/builds/#{commit}.json"
end
# Fetch relevant remote coverage data.
def fetch_build_data(url)
build_data = open_json(url)
return {
branch: build_data.dig("branch"),
commit: build_data.dig("commit_sha"),
percent: build_data.dig("covered_percent")}
end end
def to_percent(pair) def to_percent(pair)
@ -18,6 +38,7 @@ end
namespace :coverage do namespace :coverage do
desc "Coveralls stats stopped working :(" desc "Coveralls stats stopped working :("
task run: :environment do task run: :environment do
# Fetch current build coverage data from the HTML summary.
statements, branches, functions, lines = Nokogiri::HTML(open(COVERAGE_FILE_PATH)) statements, branches, functions, lines = Nokogiri::HTML(open(COVERAGE_FILE_PATH))
.css(CSS_SELECTOR) .css(CSS_SELECTOR)
.map(&:text) .map(&:text)
@ -30,41 +51,62 @@ namespace :coverage do
puts "Branches: #{to_percent(branches)}%" puts "Branches: #{to_percent(branches)}%"
puts "Functions: #{to_percent(functions)}%" puts "Functions: #{to_percent(functions)}%"
puts "Lines: #{to_percent(lines)}%" puts "Lines: #{to_percent(lines)}%"
puts
# Calculate an aggregate coverage percentage for the current build.
covered = lines.head + branches.head covered = lines.head + branches.head
total = lines.tail + branches.tail total = lines.tail + branches.tail
build_percent = (covered / total) * 100 build_percent = (covered / total) * 100
puts "Aggregate: #{build_percent.round(4)}%"
puts
latest_commit_staging = open_json(REPO_URL).dig("object", "sha") # Attempt to fetch remote build coverage data for the current branch.
puts "staging: #{latest_commit_staging}" remote = fetch_build_data(coverage_url(CURRENT_BRANCH))
build_url = "https://coveralls.io/builds/#{latest_commit_staging}.json"
any_build_url = "https://coveralls.io/github/FarmBot/Farmbot-Web-App.json" if remote[:percent].nil? && CURRENT_COMMIT == remote[:commit]
begin puts "Coverage already calculated for #{remote[:branch]}."
staging_percent = open_json(build_url).fetch("covered_percent") || puts "Using this build's data instead."
open_json(any_build_url).fetch("covered_percent") remote[:percent] = build_percent
rescue OpenURI::HTTPError => exception end
puts exception.message
if remote[:percent].nil? && CURRENT_BRANCH != "staging"
puts "Error getting coveralls data for #{CURRENT_BRANCH}."
puts "Attempting to use staging build coveralls data."
remote = fetch_build_data(coverage_url("staging"))
end
if remote[:percent].nil?
puts "Error getting coveralls data for staging."
puts "Attempting to use latest build coveralls data."
latest_cov_url = "https://coveralls.io/github/FarmBot/Farmbot-Web-App.json"
remote = fetch_build_data(latest_cov_url)
end
if remote[:percent].nil?
puts "Error getting coveralls data." puts "Error getting coveralls data."
puts "Wait for staging build to finish and try again." puts "Wait for build to finish and try again or check for build errors."
puts "If error continues, perhaps a blinky test failed the staging build." puts "Using 100 instead of nil for remote coverage value."
staging_percent = 100 remote = {branch: "N/A", commit: "", percent: 100}
end end
if CURRENT_COMMIT == latest_commit_staging # Adjust remote build data values for printing.
staging_percent = build_percent r = {
end branch: (remote[:branch] + ' ' * 8)[0,8],
percent: remote[:percent].round(8),
commit: remote[:commit][0,8]}
diff = (build_percent - staging_percent) # Calculate coverage difference between the current and previous build.
diff = (build_percent - remote[:percent])
pass = (diff > -THRESHOLD) pass = (diff > -THRESHOLD)
puts
puts "=" * 37 puts "=" * 37
puts "COVERAGE RESULTS" puts "COVERAGE RESULTS"
puts "This build: #{build_percent.round(8)}% #{CURRENT_COMMIT[0,8]}" puts "This build: #{build_percent.round(8)}% #{CURRENT_COMMIT[0,8]}"
puts "Staging build: #{staging_percent.round(8)}% #{latest_commit_staging[0,8]}" puts "#{r[:branch]} build: #{r[:percent]}% #{r[:commit]}"
puts "=" * 37 puts "=" * 37
puts "Difference: #{diff.round(8)}%" puts "Difference: #{diff.round(8)}%"
puts "Pass?: #{pass ? "yes" : "no"}" puts "Pass? #{pass ? "yes" : "no"}"
puts
exit pass ? 0 : 1 exit pass ? 0 : 1