Skip to content

Commit

Permalink
Make :hackney an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
aptinio committed Oct 24, 2021
1 parent 7114886 commit 5a8a39f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
75 changes: 45 additions & 30 deletions lib/excoveralls/poster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} ->
Expand All @@ -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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 5a8a39f

Please sign in to comment.