From b9ecff91a92557e16ec1bacbd5716a87071b63e4 Mon Sep 17 00:00:00 2001 From: Emil Falk Date: Thu, 23 Oct 2014 09:17:57 +0200 Subject: [PATCH] Fixed bug when listing projects from gitlabs. Pagination was required. --- src/gitlab_utils.erl | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/gitlab_utils.erl b/src/gitlab_utils.erl index 2edb3bd..228ca95 100644 --- a/src/gitlab_utils.erl +++ b/src/gitlab_utils.erl @@ -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). \ No newline at end of file + maps:get(ProjectName, Projects).