Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 125 lines (105 sloc) 3.386 kb
3d5cf18 @yrashk Made agner_github a parametrized module
yrashk authored
1 -module(agner_github, [Account]).
61c97b0 @yrashk Added modular indeces support
yrashk authored
2 -behaviour(agner_index).
3 -include_lib("agner.hrl").
4 -include_lib("agner_index.hrl").
5
6 -export([repositories/0,
7 repository/1,
8 tags/1,
9 branches/1,
10 spec/2
1aabca9 @yrashk Initial WIP, nothing to see yet
yrashk authored
11 ]).
12
61c97b0 @yrashk Added modular indeces support
yrashk authored
13 repositories() ->
14 case request("https://github.com/api/v2/json/repos/show/" ++ Account) of
15 {error, _Reason} = Error ->
16 Error;
17 {struct, Object} ->
18 Repositories = proplists:get_value(<<"repositories">>, Object),
19 lists:all(fun (invalid) ->
20 false;
21 (_) ->
22 true
23 end,
24 lists:map(fun ({struct, RepObject}) ->
25 repo_name(proplists:get_value(<<"name">>, RepObject))
26 end, Repositories))
27 end.
28
29 repository(Name) ->
1539dde @yrashk Allow using github accounts other than 'agner'
yrashk authored
30 case request("https://github.com/api/v2/json/repos/show/" ++ proper_repo_name(Name)) of
61c97b0 @yrashk Added modular indeces support
yrashk authored
31 {error, _Reason} = Error ->
32 Error;
33 {struct, Object} ->
34 {struct, Repo} = proplists:get_value(<<"repository">>, Object),
35 Repo
36 end.
37
1aabca9 @yrashk Initial WIP, nothing to see yet
yrashk authored
38
61c97b0 @yrashk Added modular indeces support
yrashk authored
39 tags(Name) ->
1539dde @yrashk Allow using github accounts other than 'agner'
yrashk authored
40 case request("http://github.com/api/v2/json/repos/show/" ++ proper_repo_name(Name) ++ "/tags") of
61c97b0 @yrashk Added modular indeces support
yrashk authored
41 {struct, Object} ->
42 {struct, Tags} = proplists:get_value(<<"tags">>, Object),
43 lists:map(fun({Tag, SHA1}) ->
44 {binary_to_list(Tag),
45 binary_to_list(SHA1)}
46 end, Tags)
47 end.
48
49
50 branches(Name) ->
1539dde @yrashk Allow using github accounts other than 'agner'
yrashk authored
51 case request("http://github.com/api/v2/json/repos/show/" ++ proper_repo_name(Name) ++ "/branches") of
61c97b0 @yrashk Added modular indeces support
yrashk authored
52 {error, _Reason} = Error ->
53 Error;
54 {struct, Object} ->
55 {struct, Branches} = proplists:get_value(<<"branches">>, Object),
56 lists:map(fun({Branch, SHA1}) ->
57 {binary_to_list(Branch),
58 binary_to_list(SHA1)}
59 end, Branches)
60 end.
1aabca9 @yrashk Initial WIP, nothing to see yet
yrashk authored
61
61c97b0 @yrashk Added modular indeces support
yrashk authored
62 spec(Name, SHA1) ->
1539dde @yrashk Allow using github accounts other than 'agner'
yrashk authored
63 case request("http://github.com/api/v2/json/blob/show/" ++ proper_repo_name(Name) ++ "/" ++ SHA1 ++ "/agner.config") of
61c97b0 @yrashk Added modular indeces support
yrashk authored
64 {error, _Reason} = Error ->
65 Error;
66 {struct, Object} ->
67 {struct, Blob} = proplists:get_value(<<"blob">>, Object),
ba81e59 @yrashk agner_github:spec/2 finally parses specification
yrashk authored
68 S = binary_to_list(proplists:get_value(<<"data">>, Blob)),
69 {ok, Tokens, _ } = erl_scan:string(S),
70 lists:map(fun (Term) ->
71 {ok, T} = erl_parse:parse_term(lists:reverse(Term)),
72 T
73 end,
74 tl(lists:foldl(fun ({dot, _}=Dot,[Term|Terms]) ->
75 [[]|[[Dot|Term]|Terms]];
76 (Token, [Term|Terms]) ->
77 [[Token|Term]|Terms];
78 (Token, []) ->
79 [[Token]]
80 end, [], Tokens)))
61c97b0 @yrashk Added modular indeces support
yrashk authored
81 end.
1aabca9 @yrashk Initial WIP, nothing to see yet
yrashk authored
82
83
84 %%%
85
1539dde @yrashk Allow using github accounts other than 'agner'
yrashk authored
86 proper_repo_name(Name) ->
87 case string:tokens(Name,"/") of
88 [_, _]=L ->
89 string:join(L,"/") ++ ".agner";
90 [Name] ->
91 string:join([Account, Name],"/") ++ ".agner"
92 end.
93
94 %%%
95
1aabca9 @yrashk Initial WIP, nothing to see yet
yrashk authored
96 request(URL) ->
97 parse_response(httpc_request(URL)).
98
99 httpc_request(URL) ->
100 httpc_request_1(URL,[]).
101
102 httpc_request_1(URL, Opts) ->
103 httpc:request(get,{URL,
104 []},
105 [{timeout, 60000}],
106 Opts,
107 agner).
61c97b0 @yrashk Added modular indeces support
yrashk authored
108
109 parse_response({ok, {{"HTTP/1.1",200,_},_Headers,Body}}) ->
110 mochijson2:decode(Body);
111 parse_response({ok, {{"HTTP/1.1",404,_},_Headers,_Body}}) ->
112 {error, not_found}.
113
114 %%%
115
116 repo_name(B) when is_binary(B) ->
117 S = binary_to_list(B),
118 case string:tokens(S,".") of
119 [Name,"agner"] ->
120 Name;
121 _ ->
122 invalid
123 end.
124
Something went wrong with that request. Please try again.