Skip to content

Commit

Permalink
api: inject list of manual mappings of github to slack emails
Browse files Browse the repository at this point in the history
  • Loading branch information
koonwen committed Sep 14, 2023
1 parent 70bd5cf commit e6b2544
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
let rules = cfg.status_rules.rules in
let action_on_match (branches : branch list) =
let%lwt default =
match%lwt Slack_api.lookup_user ~ctx ~email:n.commit.commit.author.email with
match%lwt Slack_api.lookup_user ~ctx ~cfg ~email:n.commit.commit.author.email with
(* To send a DM, channel parameter is set to the user id of the recipient *)
| Ok res -> Lwt.return [ res.user.id ]
| Error e ->
Expand Down
2 changes: 1 addition & 1 deletion lib/api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module type Github = sig
end

module type Slack = sig
val lookup_user : ctx:Context.t -> email:string -> lookup_user_res slack_response Lwt.t
val lookup_user : ctx:Context.t -> cfg:Config_t.config -> email:string -> lookup_user_res slack_response Lwt.t
val send_notification : ctx:Context.t -> msg:post_message_req -> unit slack_response Lwt.t

val send_chat_unfurl
Expand Down
5 changes: 3 additions & 2 deletions lib/api_local.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end

(** The base implementation for local check payload debugging and mocking tests *)
module Slack_base : Api.Slack = struct
let lookup_user ~ctx:_ ~email:_ = Lwt.return @@ Error "undefined for local setup"
let lookup_user ~ctx:_ ~cfg:_ ~email:_ = Lwt.return @@ Error "undefined for local setup"
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"
Expand All @@ -62,7 +62,8 @@ end
module Slack : Api.Slack = struct
include Slack_base

let lookup_user ~ctx:_ ~email =
let lookup_user ~ctx:_ ~(cfg : Config_t.config) ~email =
let email = List.Assoc.find cfg.user_mappings ~equal:String.equal email |> Option.value ~default:email in
let mock_user =
{
Slack_t.id = sprintf "id[%s]" email;
Expand Down
6 changes: 4 additions & 2 deletions lib/api_remote.ml
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ module Slack : Api.Slack = struct
(* must read whole response to update lexer state *)
ignore (Slack_j.read_ok_res s l)

(** [lookup_user ctx email] queries slack for a user profile with [email] *)
let lookup_user ~(ctx : Context.t) ~email =
(** [lookup_user cfg email] queries slack for a user profile with [email] *)
let lookup_user ~(ctx : Context.t) ~(cfg : Config_t.config) ~email =
(* Check if config holds the Github to Slack email mapping *)
let email = List.Assoc.find cfg.user_mappings ~equal:String.equal email |> Option.value ~default:email in
let data = Slack_j.string_of_lookup_user_req { Slack_t.email } in
request_token_auth ~name:"lookup user by email"
~body:(`Raw ("application/json", data))
Expand Down
1 change: 1 addition & 0 deletions lib/config.atd
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type config = {
~project_owners <ocaml default="{rules = []}"> : project_owners;
~ignored_users <ocaml default="[]">: string list; (* list of ignored users *)
?main_branch_name : string nullable; (* the name of the main branch; used to filter out notifications about merges of main branch into other branches *)
~user_mappings <ocaml default="[]">: (string * string) list <json repr="object"> (* list of github to slack profile mappings *)
}

(* This specifies the Slack webhook to query to post to the channel with the given name *)
Expand Down
3 changes: 3 additions & 0 deletions test/monorobot.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,8 @@
"channel": "frontend-bot"
}
]
},
"user_mappings": {
"[email protected]": "[email protected]"
}
}
8 changes: 4 additions & 4 deletions test/slack_payloads.expected
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,9 @@ will notify #longest-a1
}
===== file ../mock_payloads/status.cancelled_test.json =====
===== file ../mock_payloads/status.failure_test.json =====
will notify #id[mail@example.org]
will notify #id[slack_mail@example.com]
{
"channel": "id[mail@example.org]",
"channel": "id[slack_mail@example.com]",
"text": "<https://github.com/ahrefs/monorepo|[ahrefs/monorepo]> CI Build Status notification for <https://buildkite.com/org/pipeline2/builds/2|buildkite/pipeline2>: failure",
"attachments": [
{
Expand Down Expand Up @@ -557,9 +557,9 @@ will notify #all-push-events
]
}
===== file ../mock_payloads/status.success_test_non_main_branch.json =====
will notify #id[mail@example.org]
will notify #id[slack_mail@example.com]
{
"channel": "id[mail@example.org]",
"channel": "id[slack_mail@example.com]",
"text": "<https://github.com/ahrefs/monorepo|[ahrefs/monorepo]> CI Build Status notification for <https://buildkite.com/org/pipeline2/builds/2|buildkite/pipeline2>: success",
"attachments": [
{
Expand Down

0 comments on commit e6b2544

Please sign in to comment.