Skip to content

Commit

Permalink
Merge pull request #142 from tatchi/fix-pipeline-url
Browse files Browse the repository at this point in the history
fix link to pipeline when using buildkite
  • Loading branch information
Khady authored Mar 26, 2024
2 parents 6d67e34 + 7f51de2 commit 325ab73
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
48 changes: 32 additions & 16 deletions lib/slack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,7 @@ let generate_status_notification (cfg : Config_t.config) (notification : status_
let { commit; state; description; target_url; context; repository; _ } = notification in
let ({ commit : inner_commit; sha; author; html_url; _ } : status_commit) = commit in
let ({ message; _ } : inner_commit) = commit in
let state_info =
match state with
| Success -> "success"
| Failure -> "failure"
| Error -> "error"
| _ ->
invalid_arg
(sprintf "Monorobot doesn't know how to generate notification for the unexpected event %s of %s"
(string_of_status_state state) sha
)
in
let is_buildkite = String.starts_with context ~prefix:"buildkite" in
let color_info =
match state with
| Success -> "good"
Expand All @@ -249,6 +239,7 @@ let generate_status_notification (cfg : Config_t.config) (notification : status_
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
Expand Down Expand Up @@ -276,13 +267,38 @@ let generate_status_notification (cfg : Config_t.config) (notification : status_
[ sprintf "*%s*: %s" (pluralize ~suf:"es" ~len:(List.length branches) "Branch") (String.concat ", " branches) ]
in
let summary =
let state_info =
match state with
| Success -> "succeeded"
| Failure -> "failed"
| Error -> "error"
| _ ->
invalid_arg
(sprintf "Monorobot doesn't know how to generate notification for the unexpected event %s of %s"
(string_of_status_state state) sha
)
in
let commit_message = first_line message in
match target_url with
| None ->
sprintf "<%s|[%s]> CI Build Status notification: %s" repository.url repository.full_name state_info
(* in case the CI run is not using buildkite *)
| None -> sprintf "<%s|[%s]> CI Build Status notification: %s" repository.url repository.full_name state_info
| Some target_url ->
sprintf "<%s|[%s]> CI Build Status notification for <%s|%s>: %s" repository.url repository.full_name target_url
context state_info
let default_summary =
sprintf "<%s|[%s]> CI Build Status notification for <%s|%s>: %s" repository.url repository.full_name target_url
context state_info
in
if not is_buildkite then default_summary
else (
(* Keep only the portion of the url before /builds/... *)
let pipeline_url =
match String.split_on_char '/' target_url with
| "https:" :: "" :: "buildkite.com" :: "org" :: pipeline :: "builds" :: _ ->
Some (Printf.sprintf "https://buildkite.com/org/%s" pipeline)
| _ -> None
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
)
in
let msg = String.concat "\n" @@ List.concat [ commit_info; branches_info ] in
let attachment =
Expand Down
10 changes: 5 additions & 5 deletions test/slack_payloads.expected
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ will notify #longest-a1
will notify #id[[email protected]]
{
"channel": "id[[email protected]]",
"text": "<https://github.com/ahrefs/monorepo|[ahrefs/monorepo]> CI Build Status notification for <https://buildkite.com/org/pipeline2/builds/2|buildkite/pipeline2>: failure",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build failed for \"Update README.md\"",
"attachments": [
{
"fallback": null,
Expand All @@ -453,7 +453,7 @@ will notify #id[[email protected]]
will notify #default
{
"channel": "default",
"text": "<https://github.com/ahrefs/monorepo|[ahrefs/monorepo]> CI Build Status notification for <https://buildkite.com/org/pipeline2/builds/2|buildkite/pipeline2>: failure",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build failed for \"Update README.md\"",
"attachments": [
{
"fallback": null,
Expand All @@ -477,7 +477,7 @@ will notify #default
will notify #default
{
"channel": "default",
"text": "<https://github.com/Codertocat/Hello-World|[Codertocat/Hello-World]> CI Build Status notification: success",
"text": "<https://github.com/Codertocat/Hello-World|[Codertocat/Hello-World]> CI Build Status notification: succeeded",
"attachments": [
{
"fallback": null,
Expand All @@ -496,7 +496,7 @@ will notify #default
will notify #all-push-events
{
"channel": "all-push-events",
"text": "<https://github.com/ahrefs/monorepo|[ahrefs/monorepo]> CI Build Status notification for <https://buildkite.com/org/pipeline2/builds/2|buildkite/pipeline2>: success",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build succeeded for \"Update README.md\"",
"attachments": [
{
"fallback": null,
Expand All @@ -516,7 +516,7 @@ will notify #all-push-events
will notify #default
{
"channel": "default",
"text": "<https://github.com/ahrefs/monorepo|[ahrefs/monorepo]> CI Build Status notification for <https://buildkite.com/org/pipeline2/builds/2|buildkite/pipeline2>: success",
"text": "<https://buildkite.com/org/pipeline2|[buildkite/pipeline2]>: Build succeeded for \"Update README.md\"",
"attachments": [
{
"fallback": null,
Expand Down

0 comments on commit 325ab73

Please sign in to comment.