Skip to content

Commit

Permalink
slack: handle thread state on issue open/close
Browse files Browse the repository at this point in the history
  • Loading branch information
yasunariw committed Nov 15, 2024
1 parent 7ad3a21 commit 567851d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
partition_pr_review_comment cfg n
|> List.map (generate_pr_review_comment_notification ~ctx ~slack_match_func n)
|> Lwt.return
| Issue n -> partition_issue cfg n |> List.map (generate_issue_notification ~slack_match_func n) |> Lwt.return
| Issue n -> partition_issue cfg n |> List.map (generate_issue_notification ~ctx ~slack_match_func n) |> Lwt.return
| Issue_comment n ->
partition_issue_comment cfg n
|> List.map (generate_issue_comment_notification ~ctx ~slack_match_func n)
Expand Down
48 changes: 37 additions & 11 deletions lib/slack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ let format_attachments ~slack_match_func ~footer ~body =
in
Option.map (fun t -> markdown_text_attachment ~footer t |> List.map format_mention_in_markdown) body

let thread_state_handler ~ctx ~channel ~repo_url ~html_url action (response : Slack_t.post_message_res) =
match action with
| `Add ->
State.add_thread_if_new ctx.Context.state ~repo_url ~pr_url:html_url
{ cid = response.channel; channel; ts = response.ts }
| `Delete -> State.delete_thread ctx.state ~repo_url ~pr_url:html_url
| `Noop -> ()

let thread_state_action_of_pr_action : pr_action -> _ = function
| Opened | Ready_for_review | Labeled
| Reopened (* thread state deleted when PR closed, so need to re-add on reopen *) ->
`Add
| Closed -> `Delete
| _ -> `Noop

let thread_state_action_of_issue_action : issue_action -> _ = function
| Opened | Labeled | Reopened (* thread state deleted when PR closed, so need to re-add on reopen *) -> `Add
| Closed -> `Delete
| _ -> `Noop

let generate_pull_request_notification ~slack_match_func ~(ctx : Context.t) notification channel =
let { action; number; sender; pull_request; repository } = notification in
let ({ body; title; html_url; labels; merged; _ } : pull_request) = pull_request in
Expand All @@ -103,13 +123,8 @@ let generate_pull_request_notification ~slack_match_func ~(ctx : Context.t) noti
(pp_link ~url:html_url title) action_label sender.login
in
let attachments = format_attachments ~slack_match_func ~footer:None ~body in
let handler (res : Slack_t.post_message_res) =
match notification.action with
| Opened | Ready_for_review | Labeled | Reopened ->
State.add_thread_if_new ctx.state ~repo_url:repository.url ~pr_url:html_url
{ cid = res.channel; channel; ts = res.ts }
| Closed -> State.delete_thread ctx.state ~repo_url:repository.url ~pr_url:html_url
| _ -> ()
let handler =
thread_state_handler ~ctx ~channel ~repo_url:repository.url ~html_url (thread_state_action_of_pr_action action)
in
let reply_broadcast =
(* for closed/merged notifications, we want to notify the channel *)
Expand Down Expand Up @@ -169,10 +184,10 @@ let generate_pr_review_comment_notification ~slack_match_func ~(ctx : Context.t)
?attachments:(format_attachments ~slack_match_func ~footer:file ~body:(Some comment.body))
~channel:(Slack_channel.to_any channel) ()

let generate_issue_notification ~slack_match_func notification channel =
let generate_issue_notification ~slack_match_func ~ctx notification channel =
let ({ action; sender; issue; repository } : issue_notification) = notification in
let { number; body; title; html_url; labels; _ } = issue in
let action, body =
let action_label, body =
match action with
| Opened -> "opened", body
| Closed -> "closed", None
Expand All @@ -185,9 +200,20 @@ let generate_issue_notification ~slack_match_func notification channel =
in
let summary =
sprintf "<%s|[%s]> Issue #%d %s %s by *%s*" repository.url repository.full_name number (pp_link ~url:html_url title)
action sender.login
action_label sender.login
in
let handler =
thread_state_handler ~ctx ~channel ~repo_url:repository.url ~html_url (thread_state_action_of_issue_action action)
in
make_message ~text:summary ?attachments:(format_attachments ~slack_match_func ~footer:None ~body) ~channel ()
let reply_broadcast =
(* for closed notifications, we want to notify the channel *)
match action with
| Closed -> true
| _ -> false
in
make_message ~text:summary
?attachments:(format_attachments ~slack_match_func ~footer:None ~body)
~handler ~reply_broadcast ~channel ()

let generate_issue_comment_notification ~(ctx : Context.t) ~slack_match_func notification channel =
let { action; issue; sender; comment; repository } = notification in
Expand Down
3 changes: 2 additions & 1 deletion test/slack_payloads.expected
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ will notify #backend
{
"channel": "backend",
"text": "<https://github.com/xinyuluo/monorepo|[xinyuluo/monorepo]> Issue #6 <https://github.com/xinyuluo/monorepo/issues/6|create a test for issue_notification> closed by *xinyuluo*",
"unfurl_links": false
"unfurl_links": false,
"reply_broadcast": true
}
===== file ../mock_payloads/issues.labeled.json =====
will notify #backend
Expand Down

0 comments on commit 567851d

Please sign in to comment.