From 13c70942e3768f386176756dd08598e07d1009f1 Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Fri, 13 Feb 2015 18:53:27 -0300 Subject: [PATCH 1/3] [#25] Handle messages with position 0 as global issue comments. --- src/egithub.erl | 8 ++-- src/egithub_webhook.erl | 83 +++++++++++++++++++++++++++-------------- 2 files changed, 60 insertions(+), 31 deletions(-) diff --git a/src/egithub.erl b/src/egithub.erl index 18d5f97..8a63380 100644 --- a/src/egithub.erl +++ b/src/egithub.erl @@ -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) -> @@ -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). diff --git a/src/egithub_webhook.erl b/src/egithub_webhook.erl index f42d152..5f48c4a 100644 --- a/src/egithub_webhook.erl +++ b/src/egithub_webhook.erl @@ -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; @@ -73,30 +75,57 @@ write_comments(Cred, Repo, PR, Comments, Messages) -> position := Position, text := Text }) -> - write_comment(Cred, Repo, PR, CommitId, Path, Position, Text, Comments) + write_line_comment(Cred, Repo, PR, CommitId, + Path, Position, Text, Comments); + (#{text := Text, + position := 0 + }) -> + write_issue_comment(Cred, Repo, PR, 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. +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. -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_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. From d56488c5d2fe8d5cbafe8eb99e613f371b29e851 Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Fri, 13 Feb 2015 18:54:23 -0300 Subject: [PATCH 2/3] [#25] Fix elvis comments. --- src/egithub_webhook.erl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/egithub_webhook.erl b/src/egithub_webhook.erl index 5f48c4a..aafe5ea 100644 --- a/src/egithub_webhook.erl +++ b/src/egithub_webhook.erl @@ -97,7 +97,8 @@ 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); + lager:info("Comment '~s' for '~s' on position ~p is already there", + Args); not_exists -> {ok, _} = egithub:pull_req_comment_line( From 3f7ac3dc22520d6c807d4a94f0cd7ee1ccd2972a Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Wed, 18 Feb 2015 10:17:29 -0300 Subject: [PATCH 3/3] [#25] Fix clause order. --- src/egithub_webhook.erl | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/egithub_webhook.erl b/src/egithub_webhook.erl index aafe5ea..55cf133 100644 --- a/src/egithub_webhook.erl +++ b/src/egithub_webhook.erl @@ -69,20 +69,21 @@ 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_line_comment(Cred, Repo, PR, CommitId, - Path, Position, Text, Comments); - (#{text := Text, - position := 0 - }) -> - write_issue_comment(Cred, Repo, PR, Text, Comments) - end, - lists:foreach(Fun, Messages). + 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). write_issue_comment(Cred, Repo, PR, Text, Comments) -> case issue_comment_exists(Comments, Text) of