diff --git a/lib/mix/tasks/hex.publish.ex b/lib/mix/tasks/hex.publish.ex index 0f8766c8..56290843 100644 --- a/lib/mix/tasks/hex.publish.ex +++ b/lib/mix/tasks/hex.publish.ex @@ -487,6 +487,7 @@ defmodule Mix.Tasks.Hex.Publish do meta = build.meta %{tarball: tarball, outer_checksum: checksum} = Hex.Tar.create!(meta, meta.files, :memory) dry_run? = Keyword.get(opts, :dry_run, false) + opts = [{:name, Map.fetch!(build.meta, :name)} | opts] if dry_run? do :ok @@ -509,6 +510,24 @@ defmodule Mix.Tasks.Hex.Publish do Hex.Shell.info("Package published to #{location} (#{checksum})") :ok + {:ok, {403, _, _}} = result -> + Hex.Shell.info("") + Hex.Shell.error("Publishing failed") + package = Keyword.fetch!(opts, :name) + + case Hex.API.Package.get(organization, package, auth) do + {:ok, {code, _, _}} when code in 200..299 -> + Hex.Shell.error(""" + Package with name #{Keyword.fetch!(opts, :name)} already exists. \ + Make sure you are authenticated and have permissions to publish the package.\ + """) + + _ -> + Hex.Utils.print_error_result(result) + end + + :error + other -> Hex.Shell.info("") Hex.Shell.error("Publishing failed") diff --git a/test/mix/tasks/hex.publish_test.exs b/test/mix/tasks/hex.publish_test.exs index 74ae4002..34ac8876 100644 --- a/test/mix/tasks/hex.publish_test.exs +++ b/test/mix/tasks/hex.publish_test.exs @@ -147,6 +147,32 @@ defmodule Mix.Tasks.Hex.PublishTest do end) end + test "try create existing package without permissions" do + Process.put(:hex_test_app_name, :ex_doc) + Mix.Project.push(ReleaseSimple.MixProject) + + in_tmp(fn -> + set_home_tmp() + File.write!("mix.exs", "mix.exs") + File.write!("myfile.txt", "hello") + setup_auth("user2", "hunter42") + send(self(), {:mix_shell_input, :prompt, "hunter42"}) + + assert catch_throw( + Mix.Tasks.Hex.Publish.run(["package", "--no-progress", "--replace", "--yes"]) + ) == {:exit_code, 1} + + message = + "Package with name ex_doc already exists. " <> + "Make sure you are authenticated and have permissions to publish the package." + + assert_received {:mix_shell, :error, ["Publishing failed"]} + assert_received {:mix_shell, :error, [^message]} + end) + after + purge([ReleaseSimple.MixProject]) + end + test "publish docs with invalid filename" do Mix.Project.push(DocsFilenameError.MixProject)