From 797c82109651a70338658351853525638fd20abe Mon Sep 17 00:00:00 2001 From: Aaron Tinio Date: Thu, 1 Oct 2020 10:33:20 +0800 Subject: [PATCH] Make :hackney an optional dependency --- README.md | 1 + lib/excoveralls/poster.ex | 75 +++++++++++++++++++++++---------------- mix.exs | 2 +- 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index da53e68f..5cb8edcd 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ end defp deps do [ {:excoveralls, "~> 0.10", only: :test}, + {:hackney, "~> 1.16", only: :test} # only required if posting to the coveralls.io service ] end ``` diff --git a/lib/excoveralls/poster.ex b/lib/excoveralls/poster.ex index fba50f3d..040cbbc9 100644 --- a/lib/excoveralls/poster.ex +++ b/lib/excoveralls/poster.ex @@ -11,7 +11,6 @@ defmodule ExCoveralls.Poster do def execute(json, options \\ []) do File.write!(@file_name, json |> :zlib.gzip()) response = send_file(@file_name, options) - File.rm!(@file_name) case response do {:ok, message} -> @@ -20,42 +19,58 @@ defmodule ExCoveralls.Poster do {:error, message} -> raise ExCoveralls.ReportUploadError, message: message end + after + File.rm!(@file_name) end - defp send_file(file_name, options) do - Application.ensure_all_started(:hackney) - endpoint = options[:endpoint] || "https://coveralls.io" - - response = - :hackney.request( - :post, - "#{endpoint}/api/v1/jobs", - [], - {:multipart, - [ - {:file, file_name, {"form-data", [{"name", "json_file"}, {"filename", file_name}]}, - [{"Content-Type", "gzip/json"}]} - ]}, - [{:recv_timeout, 10_000}] - ) + if Code.ensure_loaded?(:hackney) do + defp send_file(file_name, options) do + Application.ensure_all_started(:hackney) + endpoint = options[:endpoint] || "https://coveralls.io" - case response do - {:ok, status_code, _, _} when status_code in 200..299 -> - {:ok, "Successfully uploaded the report to '#{endpoint}'."} + response = + :hackney.request( + :post, + "#{endpoint}/api/v1/jobs", + [], + {:multipart, + [ + {:file, file_name, {"form-data", [{"name", "json_file"}, {"filename", file_name}]}, + [{"Content-Type", "gzip/json"}]} + ]}, + [{:recv_timeout, 10_000}] + ) + + case response do + {:ok, status_code, _, _} when status_code in 200..299 -> + {:ok, "Successfully uploaded the report to '#{endpoint}'."} - {:ok, status_code, _, client} -> - {:ok, body} = :hackney.body(client) + {:ok, status_code, _, client} -> + {:ok, body} = :hackney.body(client) + + {:error, + "Failed to upload the report to '#{endpoint}' (reason: status_code = #{status_code}, body = #{body})."} + + {:error, reason} when reason in [:timeout, :connect_timeout] -> + {:ok, + "Unable to upload the report to '#{endpoint}' due to a timeout. Not failing the build."} + + {:error, reason} -> + {:error, "Failed to upload the report to '#{endpoint}' (reason: #{inspect(reason)})."} + end + end + else + defp send_file(_, _) do + raise """ + missing :hackney dependency - {:error, - "Failed to upload the report to '#{endpoint}' (reason: status_code = #{status_code}, body = #{ - body - })."} + ExCoveralls requires :hackney to post to the coveralls.io service. - {:error, reason} when reason in [:timeout, :connect_timeout] -> - {:ok, "Unable to upload the report to '#{endpoint}' due to a timeout. Not failing the build."} + Please add :hackney to your mix.exs :deps list and run: - {:error, reason} -> - {:error, "Failed to upload the report to '#{endpoint}' (reason: #{inspect(reason)})."} + mix deps.get + mix deps.clean --build excoveralls + """ end end end diff --git a/mix.exs b/mix.exs index b337f661..718501f1 100644 --- a/mix.exs +++ b/mix.exs @@ -39,7 +39,7 @@ defmodule ExCoveralls.Mixfile do def deps do [ {:jason, "~> 1.0"}, - {:hackney, "~> 1.16"}, + {:hackney, "~> 1.16", optional: true}, {:ex_doc, ">= 0.0.0", only: :dev, runtime: false}, {:meck, "~> 0.8", only: :test}, {:mock, "~> 0.3.6", only: :test}