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