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 May 13, 2022
1 parent a215645 commit ad4309d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 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
72 changes: 43 additions & 29 deletions lib/excoveralls/poster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,54 @@ defmodule ExCoveralls.Poster do
end
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 ad4309d

Please sign in to comment.