From 0ef8da98ee59b887ed670f98f64349c39b0b8f85 Mon Sep 17 00:00:00 2001 From: Filipe Marques Date: Wed, 31 Jul 2024 19:22:18 +0200 Subject: [PATCH] Testcomp script zulip webhook integration --- bench/README.md | 11 ++++++++ bench/testcomp/dune | 2 +- bench/testcomp/testcomp.ml | 53 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/bench/README.md b/bench/README.md index e86b781ee..8743258e0 100644 --- a/bench/README.md +++ b/bench/README.md @@ -26,6 +26,17 @@ $ dune exec -- testcomp/testcomp.exe 5 # timeout of 5 seconds A folder `testcomp-results-YYYY-MM-DD_HHhMMhSSs` has been created with a lot of output. It contains `results-report/index.html` which is the recommended way to visualize the results. +### Zulip notification: + +You can set up the script to notify you or a stream on Zulip using the Zulip Slack incoming webhook integration. +For information on creating webhooks, see [this](https://zulip.com/integrations/doc/slack_incoming#zulip-slack-incoming-webhook-integration). +Next, just set the `ZULIP_WEBHOOK` environment variable with the generated webhook and launch the script: + +```shell-session +export ZULIP_WEBHOOK="https://saussice.zulipchat.com/api/v1/external/slack_incoming?api_key=...&stream=germany&topic=bratwurst" +dune exec ./testcomp/testcomp.exe +``` + ## Generate the report by hand ```shell-session diff --git a/bench/testcomp/dune b/bench/testcomp/dune index 1fd39b72a..1bba28303 100644 --- a/bench/testcomp/dune +++ b/bench/testcomp/dune @@ -3,7 +3,7 @@ (executable (name testcomp) (modules testcomp whitelist) - (libraries bos fpath report tool yaml unix smtml)) + (libraries bos fpath report tool yaml unix smtml cohttp cohttp-lwt-unix lwt)) (rule (deps whitelist.txt) diff --git a/bench/testcomp/testcomp.ml b/bench/testcomp/testcomp.ml index 9016ebcbd..f27a8bfcb 100644 --- a/bench/testcomp/testcomp.ml +++ b/bench/testcomp/testcomp.ml @@ -146,6 +146,59 @@ let runs = files; !results +let notify_finished runs = + let open Cohttp in + let open Cohttp_lwt_unix in + let headers = + let headers = Header.init () in + Header.add_list headers + [ ("Content-type", "application/json"); ("User-Agent", "Owibot/1.0") ] + in + let send url body = + let body = Cohttp_lwt.Body.of_string (Yojson.to_string body) in + Client.post ~body ~headers url + in + let text = + Format.asprintf "Using tool=*%s* and timeout=*%F*@\n@\n%a@." + (Tool.to_reference_name tool) + timeout Report.Runs.pp_quick_results runs + in + (* Notify on `ZULIP_WEBHOOK` *) + match Bos.OS.Env.var "ZULIP_WEBHOOK" with + | None -> Format.eprintf "%s" text + | Some url -> + let url = Uri.of_string url in + let body = + (* Using Yojson just to ensure we're sending correct json *) + `Assoc + [ ( "blocks" + , `List + [ `Assoc + [ ("type", `String "header") + ; ( "text" + , `Assoc + [ ("type", `String "plain_text") + ; ( "text" + , `String + "Benchmark results (commit hash=...) :octopus:" ) + ; ("emoji", `Bool true) + ] ) + ] + ; `Assoc + [ ("type", `String "section") + ; ( "text" + , `Assoc + [ ("type", `String "mrkdwn"); ("text", `String text) ] + ) + ] + ] ) + ] + in + let result, _ = Lwt_main.run @@ send url body in + let status = Response.status result in + Format.eprintf "Server responded: %s@." (Code.string_of_status status) + let () = let runs = ok_or_fail runs in + notify_finished runs; Report.Gen.full_report runs output_dir reference_name |> ok_or_fail