Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ on:
jobs:
test:
name: Test
runs-on: ubuntu-20.04
runs-on: ${{ matrix.pair.os }}
strategy:
fail-fast: false
matrix:
pair:
- erlang: master
rebar3: 3.23.0
rebar3: 3.25.0
os: ubuntu-24.04

- erlang: 28
rebar3: 3.25.0
os: ubuntu-24.04

- erlang: 27
rebar3: 3.23.0
os: ubuntu-22.04

- erlang: 26
rebar3: 3.21.0
os: ubuntu-22.04

- erlang: 25
rebar3: 3.21.0
os: ubuntu-22.04

- erlang: 24
rebar3: 3.21.0

- erlang: 23
rebar3: 3.20.0

- erlang: 22
rebar3: 3.18.0

- erlang: 21
rebar3: 3.15.2
os: ubuntu-22.04
steps:
- uses: actions/checkout@v4

Expand Down
19 changes: 19 additions & 0 deletions src/hex_api_short_url.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%% @doc
%% Hex HTTP API - Short URLs.
-module(hex_api_short_url).
-export([create/2]).

%% @doc
%% Creates a short URL.
%%
%% Examples:
%%
%% ```
%% > hex_api_short_url:create(hex_core:default_config(), <<"https://hex.pm/packages/example">>).
%% {ok, {201, ..., #{<<"url">> => <<"https://hex.pm/l/XXXXX">>}}}
%% '''
%% @end
-spec create(hex_core:config(), binary()) -> hex_api:response().
create(Config, URL) when is_map(Config) and is_binary(URL) ->
Body = #{<<"url">> => URL},
hex_api:post(Config, ["short_url"], Body).
24 changes: 23 additions & 1 deletion src/hex_repo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
get_package/2,
get_tarball/3,
get_docs/3,
get_public_key/1
get_public_key/1,
get_hex_installs/1
]).

%%====================================================================
Expand Down Expand Up @@ -143,6 +144,27 @@ get_public_key(Config) ->
Other
end.

%% @doc
%% Gets Hex installation versions CSV from repository.
%%
%% Examples:
%%
%% ```
%% > hex_repo:get_hex_installs(hex_core:default_config()).
%% {ok, {200, ..., <<"1.0.0,abc123,1.13.0\n1.1.0,def456,1.14.0\n...">>}}
%% '''
%% @end
get_hex_installs(Config) ->
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't strictly be in hex_core since it's specific to Mix's Hex, but it simplifies our implementation in MixHex.

ReqHeaders = make_headers(Config),
URI = build_url(Config, <<"installs/hex-1.x.csv">>),

case get(Config, URI, ReqHeaders) of
{ok, {200, RespHeaders, CSV}} ->
{ok, {200, RespHeaders, CSV}};
Other ->
Other
end.

%%====================================================================
%% Internal functions
%%====================================================================
Expand Down
10 changes: 9 additions & 1 deletion test/hex_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ suite() ->
[{require, {ssl_certs, [test_pub, test_priv]}}].

all() ->
[package_test, release_test, replace_test, user_test, owner_test, keys_test, auth_test].
[package_test, release_test, replace_test, user_test, owner_test, keys_test, auth_test, short_url_test].

package_test(_Config) ->
{ok, {200, _, Package}} = hex_api_package:get(?CONFIG, <<"ecto">>),
Expand Down Expand Up @@ -92,3 +92,11 @@ keys_test(_Config) ->

{ok, {200, _, #{<<"name">> := Name2}}} = hex_api_key:delete(?CONFIG, Name2),
ok.

short_url_test(_Config) ->
LongURL = <<"https://hex.pm/packages/ecto">>,
{ok, {201, _, Response}} = hex_api_short_url:create(?CONFIG, LongURL),
#{<<"url">> := ShortURL} = Response,
?assert(is_binary(ShortURL)),
?assert(binary:match(ShortURL, <<"https://hex.pm/l/">>) =/= nomatch),
ok.
7 changes: 7 additions & 0 deletions test/hex_repo_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ all() ->
get_package_test,
get_tarball_test,
get_docs_test,
get_hex_installs_test,
get_public_key_test,
repo_org_not_set
].
Expand Down Expand Up @@ -79,6 +80,12 @@ get_docs_test(_Config) ->
{ok, {403, _, _}} = hex_repo:get_docs(?CONFIG, <<"ecto">>, <<"9.9.9">>),
ok.

get_hex_installs_test(_Config) ->
{ok, {200, _, CSV}} = hex_repo:get_hex_installs(?CONFIG),
?assert(is_binary(CSV)),
?assert(binary:match(CSV, <<"1.0.0,abc123,1.13.0\n">>) =/= nomatch),
ok.

get_public_key_test(_Config) ->
{ok, {200, #{<<"etag">> := ETag}, PublicKey}} = hex_repo:get_public_key(?CONFIG),
[{'SubjectPublicKeyInfo', _, not_encrypted}] = public_key:pem_decode(PublicKey),
Expand Down
16 changes: 16 additions & 0 deletions test/support/hex_http_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ fixture(get, <<?TEST_REPO_URL, "/docs/ecto-1.0.0.tar.gz">>, _, _) ->
{ok, Docs} = hex_tarball:create_docs([]),
{ok, {200, Headers, Docs}};

fixture(get, <<?TEST_REPO_URL, "/installs/hex-1.x.csv">>, _, _) ->
Headers = #{
<<"etag">> => <<"\"dummy\"">>
},
CSV = <<"1.0.0,abc123,1.13.0\n1.1.0,def456,1.14.0\n">>,
{ok, {200, Headers, CSV}};

fixture(get, <<?TEST_REPO_URL, "/public_key">>, _, _) ->
Headers = #{
<<"etag">> => <<"\"dummy\"">>
Expand Down Expand Up @@ -233,6 +240,15 @@ fixture(delete, <<?TEST_API_URL, "/keys/", Name/binary>>, #{<<"authorization">>
},
{ok, {200, api_headers(), term_to_binary(Payload)}};

fixture(post, <<?TEST_API_URL, "/short_url">>, _, {_, Body}) ->
DecodedBody = binary_to_term(Body),
#{<<"url">> := URL} = DecodedBody,
ShortURL = <<"https://hex.pm/l/", (integer_to_binary(erlang:phash2(URL)))/binary>>,
Payload = #{
<<"url">> => ShortURL
},
{ok, {201, api_headers(), term_to_binary(Payload)}};

%% Other

fixture(Method, URI, _, _) ->
Expand Down