# frozen_string_literal: true require "json" require "net/http" namespace :simplecov_coverage do desc "Publish simplecov coverage" task :publish do branch = `git rev-parse --abbrev-ref HEAD`.chomp.strip next unless branch == "main" repo = `git rev-parse --show-toplevel`.split("/").last.chomp.strip coverage_json = JSON.parse(File.read("#{Rails.root}/coverage/.last_run.json")) coverage = { app_name: repo, value: coverage_json.dig("result", "line") } uri = URI("https://neeto-library-stable-versions.herokuapp.com/neeto_test_coverage_data/upload") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == "https") request = Net::HTTP::Post.new(uri, "Content-Type" => "application/json") request.body = { coverage_data: coverage }.to_json response = http.request(request) raise "Simplecov coverage upload failed" unless response.is_a?(Net::HTTPSuccess) end end