Skip to content

Commit

Permalink
make failed builds notifications slimmer
Browse files Browse the repository at this point in the history
  • Loading branch information
thatportugueseguy committed Oct 15, 2024
1 parent 4c48263 commit b844b4e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
match Util.Build.is_failed_build n, cfg.status_rules.failed_builds_channel with
| true, Some failed_builds_channel ->
(* if we have a failed build and a failed builds channel, we send one notification there too,
but we don't repeat notifications on the same channel*)
but we don't notify the same channel twice *)
let chans = failed_builds_channel :: chans |> List.sort_uniq String.compare in
Lwt.return (direct_message @ chans)
| _ -> Lwt.return (direct_message @ chans)
Expand Down Expand Up @@ -282,7 +282,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
log#warn "couldn't match commit email %s to slack profile: %s" email e;
Lwt.return_some email)
in
let notifs = List.map (generate_status_notification ~slack_user_id ~ctx cfg n) channels in
let notifs = List.map (generate_status_notification ?slack_user_id ~ctx cfg n) channels in
Lwt.return notifs

let send_notifications (ctx : Context.t) notifications =
Expand Down
44 changes: 17 additions & 27 deletions lib/slack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ let generate_push_notification notification channel =

let buildkite_description_re = Re2.create_exn {|^Build #(\d+)(.*)|}

let generate_status_notification ~(ctx : Context.t) ~slack_user_id (cfg : Config_t.config)
let generate_status_notification ~(ctx : Context.t) ?slack_user_id (cfg : Config_t.config)
(notification : status_notification) channel =
let { commit; state; description; target_url; context; repository; _ } = notification in
let ({ commit : inner_commit; sha; author; html_url; _ } : status_commit) = commit in
let ({ commit : inner_commit; sha; html_url; _ } : status_commit) = commit in
let ({ message; _ } : inner_commit) = commit in
let is_buildkite = String.starts_with context ~prefix:"buildkite" in
let color_info = if state = Success then "good" else "danger" in
Expand All @@ -267,17 +267,12 @@ let generate_status_notification ~(ctx : Context.t) ~slack_user_id (cfg : Config
in
let commit_info =
[
(* if we have a DM notification, we don't need to repeat the commit message and author because
the user receiving the message is already the author of that commit. Users handles start with U *)
(match Devkit.Stre.starts_with channel "U" with
| true -> sprintf "*Commit*: `<%s|%s>`" html_url (git_short_sha_hash sha)
| false ->
sprintf "*Commit*: `<%s|%s>` %s - %s" html_url (git_short_sha_hash sha) (first_line message)
((* If the author's email is not associated with a github account the author will be missing.
Using the information from the commit instead, which should be equivalent. *)
Option.map_default
(fun { login; _ } -> login)
commit.author.name author));
(let mention =
match slack_user_id with
| None -> ""
| Some id -> sprintf "<@%s>" id
in
sprintf "*Commit*: `<%s|%s>` %s" html_url (git_short_sha_hash sha) mention);
]
in
let branches_info =
Expand Down Expand Up @@ -330,22 +325,17 @@ let generate_status_notification ~(ctx : Context.t) ~slack_user_id (cfg : Config
Option.map_default (String.equal channel) false cfg.status_rules.failed_builds_channel
in
match Util.Build.is_failed_build notification && is_failed_builds_channel with
| false -> []
| true ->
let failed_steps =
let repo_state = State.find_or_add_repo ctx.state repository.url in
let pipeline = notification.context in
let slack_step_link (s, l) =
let step = Devkit.Stre.drop_prefix s (pipeline ^ "/") in
Printf.sprintf "<%s|%s> " l step
in
match Build.new_failed_steps notification repo_state pipeline with
| [] -> []
| steps -> [ sprintf "*Steps broken*: %s" (String.concat ", " (List.map slack_step_link steps)) ]
let repo_state = State.find_or_add_repo ctx.state repository.url in
let pipeline = notification.context in
let slack_step_link (s, l) =
let step = Devkit.Stre.drop_prefix s (pipeline ^ "/") in
Printf.sprintf "<%s|%s> " l step
in
(match slack_user_id with
| Some slack_user_id -> sprintf "*Commit author*: <@%s>" slack_user_id :: failed_steps
| None -> failed_steps)
| _ -> []
(match Build.new_failed_steps notification repo_state pipeline with
| [] -> []
| steps -> [ sprintf "*Steps broken*: %s" (String.concat ", " (List.map slack_step_link steps)) ])
in
let msg = String.concat "\n" @@ List.concat [ commit_info; branches_info; failed_builds_info ] in
let attachment =
Expand Down

0 comments on commit b844b4e

Please sign in to comment.