Skip to content

Commit

Permalink
Merge pull request #26 from inaka/jfacorro.25.handle.issue.comments
Browse files Browse the repository at this point in the history
[Closes #25] Handle messages with position 0 as global issue comments.
  • Loading branch information
Brujo Benavides committed Feb 18, 2015
2 parents 7dcecdb + 3f7ac3d commit 3cd1905
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 40 deletions.
8 changes: 4 additions & 4 deletions src/egithub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pull_req_files(Credentials, Repo, PR) ->
{ok, Files}.

-spec pull_req_comment_line(credentials(), repository(), integer(),
string(), string(), integer(), string()) ->
string(), binary(), integer(), binary()) ->
result().
pull_req_comment_line(Credentials, Repo, PR,
CommitId, Filename, Line, Text) ->
Expand All @@ -127,11 +127,11 @@ pull_req_comments(Cred, Repo, PR) ->

%% Issues

-spec issue_comment(credentials(), repository(), integer(), string()) ->
result().
-spec issue_comment(credentials(), repository(), integer(), binary()) ->
result().
issue_comment(Cred, Repo, PR, Text) ->
Url = make_url({issue, comments}, {Repo, PR}),
Body = #{<<"body">> => list_to_binary(Text)},
Body = #{<<"body">> => Text},
JsonBody = egithub_json:encode(Body),
auth_req(Cred, Url, post, JsonBody).

Expand Down
103 changes: 67 additions & 36 deletions src/egithub_webhook.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ event(Module, Cred, <<"pull_request">>,
{ok, GithubFiles} = egithub:pull_req_files(Cred, Repo, PR),
case Module:handle_pull_request(Cred, Data, GithubFiles) of
{ok, Messages} ->
{ok, Comments} = egithub:pull_req_comments(Cred, Repo, PR),
write_comments(Cred, Repo, PR, Comments, Messages);
{error, Reason} ->
{error, Reason}
{ok, LineComments} = egithub:pull_req_comments(Cred, Repo, PR),
{ok, IssueComments} = egithub:issue_comments(Cred, Repo, PR),
Comments = LineComments ++ IssueComments,
write_comments(Cred, Repo, PR, Comments, Messages);
{error, Reason} ->
{error, Reason}
end;
event(_Config, _Cred, <<"ping">>, _Data) ->
ok;
Expand All @@ -67,36 +69,65 @@ event(_Config, _Cred, Event, _Data) ->

%% @doc Comment files that failed rules.
write_comments(Cred, Repo, PR, Comments, Messages) ->
Fun =
fun(#{commit_id := CommitId,
path := Path,
position := Position,
text := Text
}) ->
write_comment(Cred, Repo, PR, CommitId, Path, Position, Text, Comments)
end,
lists:foreach(Fun, Messages).

write_comment(Cred, Repo, PR, CommitId, Path, Position, Text, Comments) ->
case comment_exists(Comments, Path, Position, Text) of
exists ->
Args = [Text, Path, Position],
lager:info("Comment '~p' for ~p on position ~p is already there", Args);
not_exists ->
{ok, _} =
egithub:pull_req_comment_line(
Cred, Repo, PR, CommitId, Path, Position, Text
)
end.
Fun =
fun
(#{text := Text,
position := 0
}) ->
write_issue_comment(Cred, Repo, PR, Text, Comments);
(#{commit_id := CommitId,
path := Path,
position := Position,
text := Text
}) ->
write_line_comment(Cred, Repo, PR, CommitId,
Path, Position, Text, Comments)
end,
lists:foreach(Fun, Messages).

comment_exists(Comments, Path, Position, Body) ->
MatchingComments =
[Comment
|| #{<<"path">> := CPath,
<<"position">> := CPosition,
<<"body">> := CBody} = Comment <- Comments,
CPath == Path, CPosition == Position, CBody == Body],
case MatchingComments of
[] -> not_exists;
[_|_] -> exists
end.
write_issue_comment(Cred, Repo, PR, Text, Comments) ->
case issue_comment_exists(Comments, Text) of
exists ->
Args = [Text, PR],
lager:info("Comment '~s' for issue ~p is already there", Args);
not_exists ->
{ok, _} = egithub:issue_comment(Cred, Repo, PR, Text)
end.

write_line_comment(Cred, Repo, PR, CommitId, Path, Position, Text, Comments) ->
case line_comment_exists(Comments, Path, Position, Text) of
exists ->
Args = [Text, Path, Position],
lager:info("Comment '~s' for '~s' on position ~p is already there",
Args);
not_exists ->
{ok, _} =
egithub:pull_req_comment_line(
Cred, Repo, PR, CommitId, Path, Position, Text
)
end.

issue_comment_exists(Comments, Body) ->
MatchingComments =
[Comment
|| #{<<"issue_url">> := _,
<<"body">> := CBody} = Comment <- Comments,
CBody == Body],

case MatchingComments of
[] -> not_exists;
[_|_] -> exists
end.

line_comment_exists(Comments, Path, Position, Body) ->
MatchingComments =
[Comment
|| #{<<"path">> := CPath,
<<"position">> := CPosition,
<<"body">> := CBody} = Comment <- Comments,
CPath == Path, CPosition == Position, CBody == Body],

case MatchingComments of
[] -> not_exists;
[_|_] -> exists
end.

0 comments on commit 3cd1905

Please sign in to comment.