Skip to content

Commit

Permalink
Merge branch 'add_share_url' of https://github.com/rahulpuroht/skunk
Browse files Browse the repository at this point in the history
…into rahulpuroht-add_share_url
  • Loading branch information
etagwerker committed Nov 13, 2020
2 parents 5bf5292 + c6c6d6d commit 5ee89ce
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/skunk.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require "skunk/version"

require "skunk/share"
# Knows how to calculate the `SkunkScore` for each file analyzed by `RubyCritic`
# and `SimpleCov`
module Skunk
Expand Down
7 changes: 7 additions & 0 deletions lib/skunk/cli/commands/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def execute
RubyCritic::Config.formats = []

report(critique)

if ENV['SHARE'] || ENV['SHARE_URL']
require 'skunk/share'
share = Share.new @status_reporter
share.share
end

status_reporter
end

Expand Down
45 changes: 45 additions & 0 deletions lib/skunk/share.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'net/http'
require 'net/https'
require 'json'

module Skunk
class Share
DEFAULT_URL = "https://skunk.fastruby.io"
def initialize(status_reporter)
@status_reporter = status_reporter
end

def share
base = (ENV['SHARE_URL'] || DEFAULT_URL)
url = URI(File.join(base, "reports"))

req = Net::HTTP::Post.new(url)

data = {
"entries" => @status_reporter.update_status_message,
"options" => {
"compare" => "false"
}
}

req.body = JSON.generate(data)

http = Net::HTTP.new(url.hostname, url.port)
if url.scheme == "https"
http.use_ssl = true
http.ssl_version = :TLSv1_2
end

res = http.start do |h|
h.request req
end

if Net::HTTPOK === res
data = JSON.parse res.body
puts "Shared at: #{File.join(base, data["id"])}"
else
puts "Error sharing report"
end
end
end
end
22 changes: 22 additions & 0 deletions test/lib/skunk/application_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,27 @@
.must_equal File.read("test/samples/console_output.txt")
end
end

context "when passing an environment variable SHARE=true" do
let(:success_code) { 0 }

it "share report to default server" do
ENV["SHARE"] = "true"
mock = Minitest::Mock.new

mock.expect :start, nil
Net::HTTP.stub_any_instance(:start, mock) do
RubyCritic::AnalysedModule.stub_any_instance(:churn, 1) do
RubyCritic::AnalysedModule.stub_any_instance(:coverage, 100.0) do
result = application.execute
_(result).must_equal success_code
end
end
end
mock.verify
ENV["SHARE"] = nil
end
end

end
end

0 comments on commit 5ee89ce

Please sign in to comment.