Skip to content

Commit

Permalink
Merge pull request #139 from starbelly/support-api-key-override
Browse files Browse the repository at this point in the history
add support for CI friendly publishing
  • Loading branch information
tsloughter authored Dec 30, 2019
2 parents b7155d5 + d42a415 commit 892c5e6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 19 deletions.
50 changes: 45 additions & 5 deletions src/rebar3_hex_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,53 @@ repo(State, RepoName) ->
MaybeFound2 = get_repo(<<MaybeParentRepo/binary, BinName/binary>>, Repos),
case {MaybeFound1, MaybeFound2} of
{{ok, Repo1}, undefined} ->
{ok, Repo1};
{ok, merge_with_env(Repo1)};
{undefined, {ok, Repo2}} ->
{ok, Repo2};
{ok, merge_with_env(Repo2)};
{undefined, undefined} ->
{error, {not_valid_repo, RepoName}}
end.


-define(ENV_VARS, [
{"HEX_API_KEY", {api_key, {string, undefined}}},
{"HEX_API_URL", {api_url, {string, undefined}}},
{"HEX_UNSAFE_REGISTRY", {repo_verify, {boolean, false}}},
{"HEX_NO_VERIFY_REPO_ORIGIN", {repo_verify_origin, {boolean, true}}}
]).

merge_with_env(Repo) ->
lists:foldl(fun({EnvName, {Key, _} = Default}, Acc) ->
Val = maybe_env_val(EnvName, Default),
maybe_put_key(Key, Val, Acc)
end, Repo, ?ENV_VARS).

maybe_put_key(_Key, undefined, Repo) ->
Repo;
maybe_put_key(Key, Val, Repo) ->
case maps:get(Key, Repo, undefined) of
Val ->
Repo;
_ ->
Repo#{Key => Val}
end.

maybe_env_val(K, {_, {Type, Default}}) ->
case {os:getenv(K), {Type, Default}} of
{false, {_, Default}} ->
Default;
{"", {_, Default}} ->
Default;
{Val, {boolean, _}} ->
to_bool(string:to_lower(Val));
{Val, {string, _}} ->
rebar_utils:to_binary(Val)
end.

to_bool("0") -> false;
to_bool("false") -> false;
to_bool(_) -> true.

parent_repos(State) ->
Resources = rebar_state:resources(State),
#{repos := Repos} = rebar_resource_v2:find_resource_state(pkg, Resources),
Expand All @@ -75,11 +115,11 @@ get_repo(BinaryName, Repos) ->
{error,{rebar_hex_repos,{repo_not_found,BinaryName}}} -> undefined
end.

hex_config_write(#{api_key := Key} = HexConfig) when is_binary(Key) ->
{ok, HexConfig};
hex_config_write(#{write_key := undefined}) ->
{error, no_write_key};
hex_config_write(#{api_key := <<_ApiKey/binary>>} = HexConfig) ->
{ok, HexConfig};
hex_config_write(#{write_key := WriteKey, username := Username} = HexConfig) ->
hex_config_write(#{api_key := undefined, write_key := WriteKey, username := Username} = HexConfig) ->
DecryptedWriteKey = rebar3_hex_user:decrypt_write_key(Username, WriteKey),
{ok, HexConfig#{api_key => DecryptedWriteKey}}.

Expand Down
39 changes: 25 additions & 14 deletions src/rebar3_hex_publish.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ init(State) ->
{short_desc, "Publish a new version of your package and update the package"},
{desc, ""},
{opts, [rebar3_hex:repo_opt(),
{"yes", $y, "yes", {boolean, false}, "Publishes the package without any confirmation
prompts"},
{"--revert", undefined, "--revert", string, "Revert given version."}]}]),

State1 = rebar_state:add_provider(State, Provider),
Expand Down Expand Up @@ -186,25 +188,34 @@ publish(App, Name, Version, Deps, [], AppDetails, HexConfig, State) ->
rebar3_hex_io:say(" Links:~n ~ts", [format_links(Links)]),
rebar3_hex_io:say(" Build tools: ~ts", [format_build_tools(BuildTools)]),
maybe_say_coc(HexConfig),
case rebar3_hex_io:ask("Proceed?", boolean, "Y") of
{Args, _} = rebar_state:command_parsed_args(State),
case proplists:get_bool("yes", Args) of
true ->
%% hex_config_write should mutate and return state as well
{ok, HexConfig1} = rebar3_hex_config:hex_config_write(HexConfig),
case create_and_publish(Metadata, PackageFiles, HexConfig1) of
ok ->
rebar_api:info("Published ~s ~s", [Name, Version]),
rebar3_hex_docs:publish(App, State, HexConfig1),
{ok, State};
Error={error, _} ->
Error
end;
_ ->
rebar3_hex_io:say("Goodbye..."),
{ok, State}
publish_package_and_docs(Name, Version, Metadata, PackageFiles, HexConfig, App, State);
false ->
case rebar3_hex_io:ask("Proceed?", boolean, "Y") of
true ->
publish_package_and_docs(Name, Version, Metadata, PackageFiles, HexConfig, App, State);
_ ->
rebar3_hex_io:say("Goodbye..."),
{ok, State}
end
end;

publish(_AppDir, _Name, _Version, _Deps, Excluded, _AppDetails, _, _) ->
?PRV_ERROR({non_hex_deps, Excluded}).

publish_package_and_docs(Name, Version, Metadata, PackageFiles, HexConfig, App, State) ->
{ok, HexConfig1} = rebar3_hex_config:hex_config_write(HexConfig),
case create_and_publish(Metadata, PackageFiles, HexConfig1) of
ok ->
rebar_api:info("Published ~s ~s", [Name, Version]),
rebar3_hex_docs:publish(App, State, HexConfig1),
{ok, State};
Error={error, _} ->
Error
end.

%% Internal functions

%% if publishing to the public repo or to a private organization link to the code of conduct
Expand Down

0 comments on commit 892c5e6

Please sign in to comment.