Skip to content

Commit

Permalink
Fixed bug when listing projects from gitlabs. Pagination was required.
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Falk committed Oct 23, 2014
1 parent 9965805 commit b9ecff9
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/gitlab_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@
-spec get_public_projects() -> map().
get_public_projects() ->
{ok, Token} = application:get_env(divapi, token),
Request = ?GITLAB_URL ++ "projects/search/%25" ++ "?private_token=" ++ Token,
Projects = get_public_projects(Token, 1, []),
lists:foldl(fun (Map, AccMap) ->
Name = maps:get(<<"name">>, Map),
Url = maps:get(?GIT_URL_KEY, Map),
maps:put(Name, Url, AccMap)
end, maps:new(), Projects).

get_public_projects(Token, Page, Acc) ->
Request = ?GITLAB_URL ++ "projects/search/%25" ++ "?private_token=" ++ Token ++ "&page=" ++ integer_to_list(Page),
case httpc:request(Request) of
{ok, {_, _, Res}} ->
Projects = jiffy:decode(Res, [return_maps]),
lists:foldl(fun(Map, AccMap) ->
Name = maps:get(<<"name">>, Map),
Url = maps:get(?GIT_URL_KEY, Map),
maps:put(Name, Url, AccMap)
end, maps:new(), Projects)
case jiffy:decode(Res, [return_maps]) of
[] -> lists:flatten(Acc);
Projects -> get_public_projects(Token, Page + 1, [Projects | Acc])
end;
Error ->
Error
end.

%% @doc Returns the url found by using the diversity gitlab api for a given project name
-spec get_public_project_url(binary()) -> binary().
get_public_project_url(ProjectName) ->
Projects = get_public_projects(),
maps:get(ProjectName, Projects).
maps:get(ProjectName, Projects).

0 comments on commit b9ecff9

Please sign in to comment.