From 758e4599ed336208c524c5ba4cb7af5f57dd9c1a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 16 Jun 2021 20:42:17 +0200 Subject: [PATCH 1/5] matrix-notifier: add markdown rendering w/ pandoc --- flake.nix | 1 + matrix-notifier | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/flake.nix b/flake.nix index 841883f..df25640 100644 --- a/flake.nix +++ b/flake.nix @@ -99,6 +99,7 @@ wrapperPath = with pkgs; lib.makeBinPath [ curl jq + pandoc ]; fixupPhase = '' diff --git a/matrix-notifier b/matrix-notifier index 22fca65..34a46b2 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -71,6 +71,15 @@ get_message_type() { printf '%s' "m.$MSG_TYPE" } +make_message_json() { + jq -s '.[0] * .[1]' \ + <(printf '%s' "$1" | + pandoc | + jq --raw-input --slurp "{format: \"org.matrix.custom.html\", formatted_body: .}") \ + <(printf '%s' "$1" | + jq --raw-input --slurp "{msgtype: \"$(get_message_type)\", body: .}") +} + send_message() { local login_json local token @@ -94,8 +103,7 @@ send_message() { exit 1 fi - message_json="$(printf '%s' "$MESSAGE" | - jq --raw-input --slurp "{msgtype: \"$(get_message_type)\", body: .}")" + message_json="$(make_message_json "$MESSAGE")" curl -XPOST \ -d "$message_json" \ "$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$token")" 2>/dev/null From fe0de738b070e7275b9f6c476d8a36eac595568f Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 16 Jun 2021 20:57:44 +0200 Subject: [PATCH 2/5] matrix-notifier: add option to disable mardown --- matrix-notifier | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/matrix-notifier b/matrix-notifier index 34a46b2..ab9a6e4 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -9,6 +9,9 @@ usage() { print_err "" print_err " -h, --help" print_err " print this usage screen and exit" + print_err " -f, --format" + print_err " should the message be formatted using 'pandoc'." + print_err " Must be either 'true' or 'false'." print_err " -t, --type" print_err " which message type should be sent must be one of" print_err " 'text' or 'notice', or 'auto'." @@ -72,12 +75,13 @@ get_message_type() { } make_message_json() { - jq -s '.[0] * .[1]' \ - <(printf '%s' "$1" | + { + printf '%s' "$1" | + jq --raw-input --slurp "{msgtype: \"$(get_message_type)\", body: .}" + [ "$FORMAT" == "true" ] && printf '%s' "$1" | pandoc | - jq --raw-input --slurp "{format: \"org.matrix.custom.html\", formatted_body: .}") \ - <(printf '%s' "$1" | - jq --raw-input --slurp "{msgtype: \"$(get_message_type)\", body: .}") + jq --raw-input --slurp "{format: \"org.matrix.custom.html\", formatted_body: .}" + } | jq -s 'add' } send_message() { @@ -110,12 +114,25 @@ send_message() { } MSG_TYPE='auto' +FORMAT='true' while [ $# -gt 0 ]; do opt="$1" shift case "$opt" in + -f|--format) + arg="$1" + shift + + if [ "$arg" == "true" ] || [ "$arg" == "false" ]; then + FORMAT="$arg" + continue + fi + + print_err "Must give value 'true' or 'false' with '-t|--type'" + exit 1 + ;; -t|--type) arg="$1" shift From d2b019f29db59f96d22110808c7d8490829817b4 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 16 Jun 2021 20:42:56 +0200 Subject: [PATCH 3/5] matrix-notifier: use link formatting when enabled Make the link formatted to provide a shorter, more good-looking message. --- matrix-notifier | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/matrix-notifier b/matrix-notifier index ab9a6e4..af9cdb8 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -40,7 +40,12 @@ rawurlencode() { default_drone_message() { local msg="Build ${DRONE_BUILD_STATUS}" - msg="$msg ${DRONE_SYSTEM_PROTO}://${DRONE_SYSTEM_HOST}/${DRONE_REPO}/${DRONE_BUILD_NUMBER}" + local drone_url="${DRONE_SYSTEM_PROTO}://${DRONE_SYSTEM_HOST}/${DRONE_REPO}/${DRONE_BUILD_NUMBER}" + if [ "$FORMAT" == "true" ]; then + msg="$msg [${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}#${DRONE_COMMIT:0:8}]($drone_url)" + else + msg="$msg $drone_url" + fi msg="$msg (${DRONE_TAG:-$DRONE_BRANCH})" printf '%s' "$msg" } From 05ab9425ddcce5ce8295409c1af5798c4456a08a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 16 Jun 2021 21:14:14 +0200 Subject: [PATCH 4/5] doc: mention pandoc formatting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9d05f48..420c711 100644 --- a/README.md +++ b/README.md @@ -31,4 +31,4 @@ export MESSAGE='This is my test message' This script is packaged with `Nix`, you can just use `nix run .` to run it. The only dependencies are `bash`, `curl`, and `jq`, install those and you should -be ready to go! +be ready to go! Format is needed when using formatting (enabled by default). From 5f1fb1c1a460f77370c2c2a79fa697cb31f84b09 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 16 Jun 2021 21:11:39 +0200 Subject: [PATCH 5/5] flake: bump version to v0.2.0 --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index df25640..7c2bc1c 100644 --- a/flake.nix +++ b/flake.nix @@ -75,7 +75,7 @@ packages = { matrix-notifier = pkgs.stdenvNoCC.mkDerivation rec { pname = "matrix-notifier"; - version = "0.1.2"; + version = "0.2.0"; src = ./matrix-notifier;