Skip to content

Commit 3ff0d77

Browse files
committed
github, slack: support pr review updates
1 parent 06c3852 commit 3ff0d77

11 files changed

+116
-23
lines changed

lib/action.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
7979
8080
in both cases, since pull_request_review_comment is already handled by another type of event, information in the pull_request_review payload
8181
does not provide any insightful information and will thus be ignored. *)
82-
| Submitted, _, _ -> partition_label cfg n.pull_request.labels
82+
| (Submitted | Edited), _, _ -> partition_label cfg n.pull_request.labels
8383
| _ -> []
8484

8585
let partition_commit (cfg : Config_t.config) files =
@@ -184,7 +184,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
184184
| Github.Push n ->
185185
partition_push cfg n |> List.map ~f:(fun (channel, n) -> generate_push_notification n channel) |> Lwt.return
186186
| Pull_request n -> partition_pr cfg n |> List.map ~f:(generate_pull_request_notification n) |> Lwt.return
187-
| PR_review n -> partition_pr_review cfg n |> List.map ~f:(generate_pr_review_notification n) |> Lwt.return
187+
| PR_review n -> partition_pr_review cfg n |> List.map ~f:(generate_pr_review_notification ctx n) |> Lwt.return
188188
| PR_review_comment n ->
189189
partition_pr_review_comment cfg n |> List.map ~f:(generate_pr_review_comment_notification ctx n) |> Lwt.return
190190
| Issue n -> partition_issue cfg n |> List.map ~f:(generate_issue_notification n) |> Lwt.return
@@ -206,6 +206,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
206206
| Github.PR_review_comment n -> State.add_comment_map ctx.state n.repository.url n.comment.id message
207207
| Issue_comment n -> State.add_comment_map ctx.state n.repository.url n.comment.id message
208208
| Commit_comment n -> State.add_comment_map ctx.state n.repository.url n.comment.id message
209+
| PR_review n -> State.add_review_map ctx.state n.repository.url n.review.id message
209210
| _ -> Lwt.return_unit
210211
in
211212
let notify = function

lib/github.atd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ type pr_notification = {
185185

186186
type review = {
187187
?body: string nullable;
188+
id: int;
188189
html_url: string;
189190
state: string;
190191
}

lib/slack.ml

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ let generate_pull_request_notification notification channel =
7676
blocks = None;
7777
}
7878

79-
let generate_pr_review_notification notification channel =
79+
let generate_pr_review_notification (ctx : Context.t) notification channel =
8080
let { action; sender; pull_request; review; repository } = notification in
8181
let ({ number; title; html_url; _ } : pull_request) = pull_request in
8282
let action_str =
8383
match action with
84-
| Submitted ->
84+
| Submitted | Edited ->
8585
( match review.state with
8686
| "commented" -> "commented on"
8787
| "approved" -> "approved"
@@ -98,22 +98,26 @@ let generate_pr_review_notification notification channel =
9898
sprintf "<%s|[%s]> *%s* <%s|%s> #%d %s" repository.url repository.full_name sender.login review.html_url action_str
9999
number (pp_link ~url:html_url title)
100100
in
101-
New_message
102-
{
103-
channel;
104-
text = Some summary;
105-
attachments =
106-
Some
107-
[
108-
{
109-
empty_attachments with
110-
mrkdwn_in = Some [ "text" ];
111-
color = Some "#ccc";
112-
text = mrkdwn_of_markdown_opt review.body;
113-
};
114-
];
115-
blocks = None;
116-
}
101+
let attachments =
102+
Some
103+
[
104+
{
105+
empty_attachments with
106+
mrkdwn_in = Some [ "text" ];
107+
color = Some "#ccc";
108+
text = mrkdwn_of_markdown_opt review.body;
109+
};
110+
]
111+
in
112+
match action with
113+
| Submitted -> New_message { channel; text = Some summary; attachments; blocks = None }
114+
| Edited ->
115+
( match State.get_review_map ctx.state repository.url review.id with
116+
| Some ({ channel; ts } : post_message_res) ->
117+
Update_message { channel; ts; text = Some summary; attachments; blocks = None }
118+
| None -> invalid_arg (sprintf "could not find comment %d in %s" review.id repository.url)
119+
)
120+
| _ -> invalid_arg "impossible"
117121

118122
let generate_pr_review_comment_notification (ctx : Context.t) notification channel =
119123
let { action; pull_request; sender; comment; repository } = notification in

lib/state.atd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ type branch_statuses = status_state map_as_object
1111
type pipeline_statuses = branch_statuses map_as_object
1212

1313
type post_message_res <ocaml from="Slack"> = abstract
14-
type slack_comment_map = post_message_res map_as_object
14+
type post_msg_map = post_message_res map_as_object
1515

1616
(* The runtime state of a given GitHub repository *)
1717
type repo_state = {
1818
pipeline_statuses <ocaml mutable>: pipeline_statuses;
19-
comments_map <ocaml mutable>: slack_comment_map;
19+
comments_map <ocaml mutable>: post_msg_map;
20+
reviews_map <ocaml mutable>: post_msg_map;
2021
}
2122

2223
(* The serializable runtime state of the bot *)

lib/state.ml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ type t = {
77
lock : Lwt_mutex.t; (** protect access to mutable string map `pipeline_statuses` *)
88
}
99

10-
let empty_repo_state () : State_t.repo_state = { pipeline_statuses = StringMap.empty; comments_map = StringMap.empty }
10+
let empty_repo_state () : State_t.repo_state =
11+
{ pipeline_statuses = StringMap.empty; comments_map = StringMap.empty; reviews_map = StringMap.empty }
1112

1213
let empty () : t =
1314
let state = State_t.{ repos = Stringtbl.empty (); bot_user_id = None } in
@@ -44,6 +45,16 @@ let get_comment_map { state; _ } repo_url comment_id =
4445
let repo_state = find_or_add_repo' state repo_url in
4546
Map.find repo_state.comments_map (Int.to_string comment_id)
4647

48+
let add_review_map { state; lock } repo_url review_id post_message_res =
49+
Lwt_mutex.with_lock lock @@ fun () ->
50+
let repo_state = find_or_add_repo' state repo_url in
51+
repo_state.reviews_map <- Map.set repo_state.reviews_map ~key:(Int.to_string review_id) ~data:post_message_res;
52+
Lwt.return_unit
53+
54+
let get_review_map { state; _ } repo_url review_id =
55+
let repo_state = find_or_add_repo' state repo_url in
56+
Map.find repo_state.reviews_map (Int.to_string review_id)
57+
4758
let set_bot_user_id { state; _ } user_id = state.State_t.bot_user_id <- Some user_id
4859
let get_bot_user_id { state; _ } = state.State_t.bot_user_id
4960
let log = Log.from "state"

mock_states/issue_comment.edited.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@
1313
"channel": "some channel",
1414
"ts": "some ts"
1515
}
16+
},
17+
"reviews_map": {
18+
"0": {
19+
"channel": "some channel",
20+
"ts": "some ts"
21+
}
1622
}
1723
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
{
3+
"pipeline_statuses": {
4+
"default": {
5+
"master": "failure"
6+
},
7+
"buildkite/pipeline2": {
8+
"master": "success"
9+
}
10+
},
11+
"comments_map": {
12+
"0": {
13+
"channel": "some channel",
14+
"ts": "some ts"
15+
}
16+
},
17+
"reviews_map": {
18+
"0": {
19+
"channel": "some channel",
20+
"ts": "some ts"
21+
}
22+
}
23+
}

mock_states/pull_request_review_comment.edited.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@
1313
"channel": "some channel",
1414
"ts": "some ts"
1515
}
16+
},
17+
"reviews_map": {
18+
"0": {
19+
"channel": "some channel",
20+
"ts": "some ts"
21+
}
1622
}
1723
}

mock_states/status.state_hide_success_test.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@
1313
"channel": "some channel",
1414
"ts": "some ts"
1515
}
16+
},
17+
"reviews_map": {
18+
"0": {
19+
"channel": "some channel",
20+
"ts": "some ts"
21+
}
1622
}
1723
}

mock_states/status.state_hide_success_test_disallowed_pipeline.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@
99
"channel": "some channel",
1010
"ts": "some ts"
1111
}
12+
},
13+
"reviews_map": {
14+
"0": {
15+
"channel": "some channel",
16+
"ts": "some ts"
17+
}
1218
}
1319
}

0 commit comments

Comments
 (0)