Skip to content

Commit

Permalink
Merge pull request #162 from ahrefs/make-notifications-minimal
Browse files Browse the repository at this point in the history
make status notifications minimal
  • Loading branch information
Khady authored Oct 9, 2024
2 parents 8913567 + 9dce6ec commit 38b0672
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 88 deletions.
62 changes: 28 additions & 34 deletions lib/slack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -245,37 +245,37 @@ let generate_status_notification (cfg : Config_t.config) (notification : status_
let ({ commit : inner_commit; sha; author; 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 =
match state with
| Success -> "good"
| _ -> "danger"
in
let description_info =
let color_info = if state = Success then "good" else "danger" in
let build_desc =
match description with
| None -> None
| None -> ""
| Some s ->
let text =
match target_url with
| None -> s
| Some _ when not is_buildkite -> s
| Some target_url ->
(* Specific to buildkite *)
match Re2.find_submatches_exn buildkite_description_re s with
| [| Some _; Some build_nr; Some rest |] ->
(* We use a zero-with space \u{200B} to prevent slack from interpreting #XXXXXX as a color *)
sprintf "Build <%s|#\u{200B}%s>%s" target_url build_nr rest
| _ -> s
in
Some (sprintf "*Description*: %s." text)
match target_url with
| None -> s
| Some _ when not is_buildkite -> s
| Some target_url ->
(* Specific to buildkite *)
match Re2.find_submatches_exn buildkite_description_re s with
| [| Some _; Some build_nr; Some rest |] ->
(* We use a zero-with space \u{200B} to prevent slack from interpreting #XXXXXX as a color *)
sprintf "Build <%s|#\u{200B}%s>%s" target_url build_nr rest
| _ | (exception _) ->
(* we either match on the first case or get an exception *)
s
in
let commit_info =
[
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);
(* 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));
]
in
let branches_info =
Expand Down Expand Up @@ -321,17 +321,11 @@ let generate_status_notification (cfg : Config_t.config) (notification : status_
in
match pipeline_url with
| None -> default_summary
| Some pipeline_url -> sprintf "<%s|[%s]>: Build %s for \"%s\"" pipeline_url context state_info commit_message)
| Some pipeline_url -> sprintf "<%s|[%s]>: %s for \"%s\"" pipeline_url context build_desc commit_message)
in
let msg = String.concat "\n" @@ List.concat [ commit_info; branches_info ] in
let attachment =
{
empty_attachments with
mrkdwn_in = Some [ "fields"; "text" ];
color = Some color_info;
text = description_info;
fields = Some [ { title = None; value = msg; short = false } ];
}
{ empty_attachments with mrkdwn_in = Some [ "fields"; "text" ]; color = Some color_info; text = Some msg }
in
make_message ~text:summary ~attachments:[ attachment ] ~channel ()

Expand Down
69 changes: 15 additions & 54 deletions test/slack_payloads.expected
Original file line number Diff line number Diff line change
Expand Up @@ -479,37 +479,27 @@ will notify #longest-a1
will notify #id[[email protected]]
{
"channel": "id[[email protected]]",
"text": "<https://buildkite.com/ahrefs/monorepo|[buildkite/pipeline2]>: Build failed for \"c1 message\"",
"text": "<https://buildkite.com/ahrefs/monorepo|[buildkite/pipeline2]>: Build <https://buildkite.com/ahrefs/monorepo/builds/181732|#​181732> is failing for \"c1 message\"",
"attachments": [
{
"fallback": null,
"mrkdwn_in": [ "fields", "text" ],
"color": "danger",
"text": "*Description*: Build <https://buildkite.com/ahrefs/monorepo/builds/181732|#​181732> is failing.",
"fields": [
{
"value": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/7e0a933e9c71b4ca107680ca958ca1888d5e479b|7e0a933e>` c1 message - author\n*Branch*: author/patches/js-storage"
}
]
"text": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/7e0a933e9c71b4ca107680ca958ca1888d5e479b|7e0a933e>` c1 message - author\n*Branch*: author/patches/js-storage"
}
],
"unfurl_links": false
}
will notify #default
{
"channel": "default",
"text": "<https://buildkite.com/ahrefs/monorepo|[buildkite/pipeline2]>: Build failed for \"c1 message\"",
"text": "<https://buildkite.com/ahrefs/monorepo|[buildkite/pipeline2]>: Build <https://buildkite.com/ahrefs/monorepo/builds/181732|#​181732> is failing for \"c1 message\"",
"attachments": [
{
"fallback": null,
"mrkdwn_in": [ "fields", "text" ],
"color": "danger",
"text": "*Description*: Build <https://buildkite.com/ahrefs/monorepo/builds/181732|#​181732> is failing.",
"fields": [
{
"value": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/7e0a933e9c71b4ca107680ca958ca1888d5e479b|7e0a933e>` c1 message - author\n*Branch*: author/patches/js-storage"
}
]
"text": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/7e0a933e9c71b4ca107680ca958ca1888d5e479b|7e0a933e>` c1 message - author\n*Branch*: author/patches/js-storage"
}
],
"unfurl_links": false
Expand All @@ -518,18 +508,13 @@ will notify #default
will notify #default
{
"channel": "default",
"text": "<https://buildkite.com/ahrefs/monorepo|[buildkite/pipeline2]>: Build failed for \"c1 message\"",
"text": "<https://buildkite.com/ahrefs/monorepo|[buildkite/pipeline2]>: Build <https://buildkite.com/ahrefs/monorepo/builds/181732|#​181732> failed (7 minutes, 29 seconds) for \"c1 message\"",
"attachments": [
{
"fallback": null,
"mrkdwn_in": [ "fields", "text" ],
"color": "danger",
"text": "*Description*: Build <https://buildkite.com/ahrefs/monorepo/builds/181732|#​181732> failed (7 minutes, 29 seconds).",
"fields": [
{
"value": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/7e0a933e9c71b4ca107680ca958ca1888d5e479b|7e0a933e>` c1 message - author\n*Branch*: author/patches/js-storage"
}
]
"text": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/7e0a933e9c71b4ca107680ca958ca1888d5e479b|7e0a933e>` c1 message - author\n*Branch*: author/patches/js-storage"
}
],
"unfurl_links": false
Expand All @@ -538,37 +523,27 @@ will notify #default
will notify #id[[email protected]]
{
"channel": "id[[email protected]]",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build failed for \"Update README.md\"",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build <https://buildkite.com/org/pipeline2/builds/2|#​2> failed (20 seconds) for \"Update README.md\"",
"attachments": [
{
"fallback": null,
"mrkdwn_in": [ "fields", "text" ],
"color": "danger",
"text": "*Description*: Build <https://buildkite.com/org/pipeline2/builds/2|#​2> failed (20 seconds).",
"fields": [
{
"value": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/0d95302addd66c1816bce1b1d495ed1c93ccd478|0d95302a>` Update README.md - Khady\n*Branch*: master"
}
]
"text": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/0d95302addd66c1816bce1b1d495ed1c93ccd478|0d95302a>` Update README.md - Khady\n*Branch*: master"
}
],
"unfurl_links": false
}
will notify #default
{
"channel": "default",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build failed for \"Update README.md\"",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build <https://buildkite.com/org/pipeline2/builds/2|#​2> failed (20 seconds) for \"Update README.md\"",
"attachments": [
{
"fallback": null,
"mrkdwn_in": [ "fields", "text" ],
"color": "danger",
"text": "*Description*: Build <https://buildkite.com/org/pipeline2/builds/2|#​2> failed (20 seconds).",
"fields": [
{
"value": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/0d95302addd66c1816bce1b1d495ed1c93ccd478|0d95302a>` Update README.md - Khady\n*Branch*: master"
}
]
"text": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/0d95302addd66c1816bce1b1d495ed1c93ccd478|0d95302a>` Update README.md - Khady\n*Branch*: master"
}
],
"unfurl_links": false
Expand All @@ -588,11 +563,7 @@ will notify #default
"fallback": null,
"mrkdwn_in": [ "fields", "text" ],
"color": "good",
"fields": [
{
"value": "*Commit*: `<https://github.com/Codertocat/Hello-World/commit/6113728f27ae82c7b1a177c8d03f9e96e0adf246|6113728f>` Initial commit - Codertocat\n*Branches*: master, changes, gh-pages"
}
]
"text": "*Commit*: `<https://github.com/Codertocat/Hello-World/commit/6113728f27ae82c7b1a177c8d03f9e96e0adf246|6113728f>` Initial commit - Codertocat\n*Branches*: master, changes, gh-pages"
}
],
"unfurl_links": false
Expand All @@ -601,18 +572,13 @@ will notify #default
will notify #all-push-events
{
"channel": "all-push-events",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build succeeded for \"Update README.md\"",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build <https://buildkite.com/org/pipeline2/builds/2|#​2> passed (5 minutes, 19 seconds) for \"Update README.md\"",
"attachments": [
{
"fallback": null,
"mrkdwn_in": [ "fields", "text" ],
"color": "good",
"text": "*Description*: Build <https://buildkite.com/org/pipeline2/builds/2|#​2> passed (5 minutes, 19 seconds).",
"fields": [
{
"value": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/0d95302addd66c1816bce1b1d495ed1c93ccd478|0d95302a>` Update README.md - Khady\n*Branch*: develop"
}
]
"text": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/0d95302addd66c1816bce1b1d495ed1c93ccd478|0d95302a>` Update README.md - Khady\n*Branch*: develop"
}
],
"unfurl_links": false
Expand All @@ -621,18 +587,13 @@ will notify #all-push-events
will notify #default
{
"channel": "default",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build succeeded for \"Update README.md\"",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build <https://buildkite.com/org/pipeline2/builds/2|#​2> passed (5 minutes, 19 seconds) for \"Update README.md\"",
"attachments": [
{
"fallback": null,
"mrkdwn_in": [ "fields", "text" ],
"color": "good",
"text": "*Description*: Build <https://buildkite.com/org/pipeline2/builds/2|#​2> passed (5 minutes, 19 seconds).",
"fields": [
{
"value": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/0d95302addd66c1816bce1b1d495ed1c93ccd478|0d95302a>` Update README.md - Khady\n*Branch*: master"
}
]
"text": "*Commit*: `<https://github.com/ahrefs/monorepo/commit/0d95302addd66c1816bce1b1d495ed1c93ccd478|0d95302a>` Update README.md - Khady\n*Branch*: master"
}
],
"unfurl_links": false
Expand Down

0 comments on commit 38b0672

Please sign in to comment.