Skip to content

Commit

Permalink
small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thatportugueseguy committed Oct 9, 2024
1 parent 6dfafc7 commit 7f264e2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
13 changes: 4 additions & 9 deletions lib/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
let repo = Github.repo_of_notification req in
let cfg = Context.find_repo_config_exn ctx repo.url in
let slack_match_func = match_github_login_to_slack_id cfg in
let get_thread_permalink = Slack_api.get_thread_permalink in
match ignore_notifications_from_user cfg req with
| true -> Lwt.return []
| false ->
Expand All @@ -261,17 +262,13 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
| Pull_request n ->
let%lwt notifications =
partition_pr cfg ctx n
|> Lwt_list.map_p
(generate_pull_request_notification ~ctx ~slack_match_func
~get_thread_permalink:Slack_api.get_thread_permalink n)
|> Lwt_list.map_p (generate_pull_request_notification ~ctx ~slack_match_func ~get_thread_permalink n)
in
Lwt.return @@ List.flatten notifications
| PR_review n ->
let%lwt notifications =
partition_pr_review cfg n
|> Lwt_list.map_p
(generate_pr_review_notification ~ctx ~slack_match_func
~get_thread_permalink:Slack_api.get_thread_permalink n)
|> Lwt_list.map_p (generate_pr_review_notification ~ctx ~slack_match_func ~get_thread_permalink n)
in
Lwt.return @@ List.flatten notifications
| PR_review_comment n ->
Expand All @@ -282,9 +279,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
| Issue_comment n ->
let%lwt notifications =
partition_issue_comment cfg n
|> Lwt_list.map_p
(generate_issue_comment_notification ~ctx ~slack_match_func
~get_thread_permalink:Slack_api.get_thread_permalink n)
|> Lwt_list.map_p (generate_issue_comment_notification ~ctx ~slack_match_func ~get_thread_permalink n)
in
Lwt.return @@ List.flatten notifications
| Commit_comment n ->
Expand Down
7 changes: 5 additions & 2 deletions lib/api_local.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ module Slack_base : Api.Slack = struct
let send_notification ~ctx:_ ~msg:_ = Lwt.return @@ Error "undefined for local setup"
let send_chat_unfurl ~ctx:_ ~channel:_ ~ts:_ ~unfurls:_ () = Lwt.return @@ Error "undefined for local setup"
let send_auth_test ~ctx:_ () = Lwt.return @@ Error "undefined for local setup"

let get_thread_permalink ~ctx:_ (_thread : State_t.slack_thread) = Lwt.return_none
end

Expand Down Expand Up @@ -91,7 +90,11 @@ module Slack : Api.Slack = struct
Lwt.return
@@ Ok ({ url = ""; team = ""; user = ""; team_id = ""; user_id = "test_slack_user" } : Slack_t.auth_test_res)

let get_thread_permalink ~ctx:_ (_thread : State_t.slack_thread) = Lwt.return_none
let get_thread_permalink ~ctx:_ (thread : State_t.slack_thread) =
Lwt.return_some
@@ Printf.sprintf "https://monorobot.slack.com/archives/%s/p%s?thread_ts=%s&cid=%s" thread.cid
(Stre.replace_all ~str:thread.ts ~sub:"." ~by:"")
thread.ts thread.cid
end

(** Simple messages (only the actual text messages that users see) output to log for checking payload commands *)
Expand Down
12 changes: 3 additions & 9 deletions lib/slack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ open Mrkdwn
open Github_j
open Slack_j

let log = Devkit.Log.from "slack"

let empty_attachments =
{
mrkdwn_in = None;
Expand Down Expand Up @@ -119,7 +117,7 @@ let generate_pull_request_notification ~slack_match_func ~(ctx : Context.t) ~get
let handler (res : Slack_t.post_message_res) =
match notification.action with
| Opened | Ready_for_review | Labeled ->
State.update_thread ctx.state ~repo_url:repository.url ~pr_url:html_url
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
| _ -> ()
Expand Down Expand Up @@ -153,21 +151,19 @@ let generate_pr_review_notification ~slack_match_func ~(ctx : Context.t) ~get_th
sprintf "<%s|[%s]> *%s* <%s|%s> #%d %s" repository.url repository.full_name sender.login review.html_url action_str
number (pp_link ~url:html_url title)
in
(* if the message is already in a thread, post to that thread *)
let thread = State.get_thread ctx.state ~repo_url:repository.url ~pr_url:html_url channel in

let%lwt pr_summary =
let summary =
sprintf "<%s|[%s]> *%s* <%s|%s> #%d %s" repository.url repository.full_name sender.login review.html_url
action_str number (pp_link ~url:html_url title)
in

let%lwt thread_mention = make_thread_mention ~ctx thread get_thread_permalink in

let body = Some (sprintf "**Comment**: %s\n%s" (Option.default "" review.body) thread_mention) in

Lwt.return
@@ make_message ~text:summary ?attachments:(format_attachments ~slack_match_func ~footer:None ~body) ~channel ()
in

let msg =
make_message ~text:summary ?thread
?attachments:(format_attachments ~slack_match_func ~footer:None ~body:review.body)
Expand Down Expand Up @@ -200,7 +196,6 @@ let generate_pr_review_comment_notification ~slack_match_func ~(ctx : Context.t)
| None -> None
| Some a -> Some (sprintf "Commented in file <%s|%s>" comment.html_url a)
in
(* if the message is already in a thread, post to that thread *)
let thread = State.get_thread ctx.state ~repo_url:repository.url ~pr_url:html_url channel in
make_message ~text:summary ?thread
?attachments:(format_attachments ~slack_match_func ~footer:file ~body:(Some comment.body))
Expand Down Expand Up @@ -244,7 +239,6 @@ let generate_issue_comment_notification ~(ctx : Context.t) ~slack_match_func ~ge
sprintf "<%s|[%s]> *%s* <%s|%s> on #%d %s" repository.url repository.full_name sender.login comment.html_url
action_str number (pp_link ~url:issue.html_url title)
in
(* if the message is already in a thread, post to that thread *)
let thread = State.get_thread ctx.state ~repo_url:repository.url ~pr_url:html_url channel in
let%lwt comment_summary =
let summary =
Expand Down
2 changes: 1 addition & 1 deletion lib/state.ml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ let get_thread { state } ~repo_url ~pr_url channel =
| true -> Some thread)
threads

let update_thread { state } ~repo_url ~pr_url (msg : State_t.slack_thread) =
let add_thread_if_new { state } ~repo_url ~pr_url (msg : State_t.slack_thread) =
let repo_state = find_or_add_repo' state repo_url in
let set_threads threads =
match threads with
Expand Down
17 changes: 17 additions & 0 deletions state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"repos": {
"https://github.com/thatportugueseguy/buildkite-starter": {
"pipeline_statuses": {},
"pipeline_commits": {},
"slack_threads": {
"https://github.com/thatportugueseguy/buildkite-starter/pull/9": [
{
"channel": "monorobot-tests",
"ts": "1728399554.482879",
"cid": "C07DEKSFP7Y"
}
]
}
}
}
}
8 changes: 4 additions & 4 deletions test/slack_payloads.expected
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ will notify #default
"fallback": null,
"mrkdwn_in": [ "text" ],
"color": "#ccc",
"text": "*Comment*: approve review"
"text": "*Comment*: approve review\n*Slack thread*: <https://monorobot.slack.com/archives/Cdefault/p1728399554482879?thread_ts=1728399554.482879&cid=Cdefault|comments and reviews>"
}
],
"unfurl_links": false
Expand Down Expand Up @@ -389,7 +389,7 @@ will notify #frontend-bot
"fallback": null,
"mrkdwn_in": [ "text" ],
"color": "#ccc",
"text": "*Comment*: submit a PR review"
"text": "*Comment*: submit a PR review\n*Slack thread*: <https://monorobot.slack.com/archives/Cfebot/p1728399554482879?thread_ts=1728399554.482879&cid=Cfebot|comments and reviews>"
}
],
"unfurl_links": false
Expand Down Expand Up @@ -421,7 +421,7 @@ will notify #default
"fallback": null,
"mrkdwn_in": [ "text" ],
"color": "#ccc",
"text": "*Comment*: request changes review"
"text": "*Comment*: request changes review\n*Slack thread*: <https://monorobot.slack.com/archives/Cdefault/p1728399554482879?thread_ts=1728399554.482879&cid=Cdefault|comments and reviews>"
}
],
"unfurl_links": false
Expand Down Expand Up @@ -451,7 +451,7 @@ will notify #frontend-bot
"fallback": null,
"mrkdwn_in": [ "text" ],
"color": "#ccc",
"text": "*Comment*: This is a great PR, thank you!"
"text": "*Comment*: This is a great PR, thank you!\n*Slack thread*: <https://monorobot.slack.com/archives/Cfebot/p1728399554482879?thread_ts=1728399554.482879&cid=Cfebot|comments and reviews>"
}
],
"unfurl_links": false
Expand Down

0 comments on commit 7f264e2

Please sign in to comment.