From 8ae3594c6869ca7359fe4f6ec071406c2125ba04 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 01:25:04 +0200 Subject: [PATCH 01/31] doc: add README --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..9d05f48 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# matrix-notifier + +This is a simple to send a message to a Matrix room. It automatically logs in +and sends the message when invoked. This was written to be used a notification +script in my CI/CD pipelines. + +## How to use + +You need to define the following environment variables for the script to be +executed correctly: + +* `USER`: the user to login as. +* `PASS`: the password to login with. +* `ADDRESS`: the address of the homeserver to connect to. +* `ROOM`: the room id, as can be found in the room parameters. +* `MESSAGE`: the message you wish to send to the room. + +### Example + +```sh +export ADDRESS='https://matrix.org' +export USER='username' +export PASS='password' +export ROOM='!aaaaaaaaaaaaaaaaaa:matrix.org' +export MESSAGE='This is my test message' +./matrix-notifier +``` + +## How to run/install + +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! From ca3b5a053c8f38a9f8c8d8b26fe319d7adfda37c Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 18:22:04 +0200 Subject: [PATCH 02/31] matrix-notifier: add default Drone message --- matrix-notifier | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/matrix-notifier b/matrix-notifier index 9bf6b4c..ebc2f67 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -23,6 +23,17 @@ rawurlencode() { echo "$encoded") } +default_drone_message() { + local msg="Build ${DRONE_BUILD_STATUS}" + msg="$msg ${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/${DRONE_BUILD_NUMBER}" + msg="$msg (${DRONE_BRANCH})" + printf '%s' "$msg" +} + +if [ "$DRONE" == "true" ] && [ -z "$MESSAGE" ]; then + MESSAGE="$(default_drone_message)" +fi + if [ -z "$USER" ] || [ -z "$PASS" ]; then print_err "You must provide USER and PASS" exit 1 From f45e1378ebc4f05707f57f260d8b82497c6cb5ee Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 18:56:36 +0200 Subject: [PATCH 03/31] matrix-notifier: add usage And split into functions to make it more manageable. --- matrix-notifier | 99 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 32 deletions(-) diff --git a/matrix-notifier b/matrix-notifier index ebc2f67..2dfb61c 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -4,6 +4,13 @@ print_err() { printf "%s\n" "$1" >&2 } +usage() { + print_err "Usage: $0 [option]..." + print_err "" + print_err " -h, --help" + print_err " print this usage screen and exit" +} + # Blessed Stack Overflow rawurlencode() { (set +x @@ -30,41 +37,69 @@ default_drone_message() { printf '%s' "$msg" } -if [ "$DRONE" == "true" ] && [ -z "$MESSAGE" ]; then - MESSAGE="$(default_drone_message)" -fi +ensure_variables() { + if [ "$DRONE" == "true" ] && [ -z "$MESSAGE" ]; then + MESSAGE="$(default_drone_message)" + fi -if [ -z "$USER" ] || [ -z "$PASS" ]; then - print_err "You must provide USER and PASS" - exit 1 -elif [ -z "$ADDRESS" ] || [ -z "$ROOM" ]; then - print_err "You must provide ADDRESS and ROOM" - exit 1 -elif [ -z "$MESSAGE" ]; then - print_err "You must provide MESSAGE" - exit 1 -fi + if [ -z "$USER" ] || [ -z "$PASS" ]; then + print_err "You must provide USER and PASS" + exit 1 + elif [ -z "$ADDRESS" ] || [ -z "$ROOM" ]; then + print_err "You must provide ADDRESS and ROOM" + exit 1 + elif [ -z "$MESSAGE" ]; then + print_err "You must provide MESSAGE" + exit 1 + fi +} -if [ "$(curl -XGET "$ADDRESS/_matrix/client/r0/login" 2>/dev/null | - jq 'any(.flows[].type; .== "m.login.password")')" != "true" ]; then - print_err "Login method not supported" - exit 1 -fi +send_message() { + local login_json + local token + local message_json + + if [ "$(curl -XGET "$ADDRESS/_matrix/client/r0/login" 2>/dev/null | + jq 'any(.flows[].type; .== "m.login.password")')" != "true" ]; then + print_err "Login method not supported" + exit 1 + fi -LOGIN_JSON="$(printf '%s\n%s' "$USER" "$PASS" | - jq -Rn '[inputs] | {type: "m.login.password", user: .[0], password: .[1]}')" &>/dev/null -TOKEN="$(curl -XPOST \ - -d "$LOGIN_JSON" \ - "$ADDRESS/_matrix/client/r0/login" 2>/dev/null| jq .access_token --raw-output)" + login_json="$(printf '%s\n%s' "$USER" "$PASS" | + jq -Rn '[inputs] | {type: "m.login.password", user: .[0], password: .[1]}')" &>/dev/null + token="$(curl -XPOST \ + -d "$login_json" \ + "$ADDRESS/_matrix/client/r0/login" 2>/dev/null| jq .access_token --raw-output)" -if [ -z "$TOKEN" ] || [ "$TOKEN" == "null" ]; then - print_err "Error during login" - exit 1 -fi + if [ -z "$token" ] || [ "$token" == "null" ]; then + print_err "Error during login" + exit 1 + fi -MESSAGE_JSON="$(printf '%s' "$MESSAGE" | - jq --raw-input --slurp '{msgtype: "m.text", body: .}')" -curl -XPOST \ - -d "$MESSAGE_JSON" \ - "$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$TOKEN")" 2>/dev/null + message_json="$(printf '%s' "$MESSAGE" | + jq --raw-input --slurp "{msgtype: \"m.text\", body: .}")" + curl -XPOST \ + -d "$message_json" \ + "$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$token")" 2>/dev/null +} + +while [ $# -gt 0 ]; do + opt="$1" + shift + + case "$opt" in + -h|--help) + usage + exit + ;; + *) + print_err "Unknown argument '$opt'" + usage + exit 1 + ;; + esac +done + +ensure_variables +send_message From e017deeaa7b3250b2fcd5b6a04ea7199da34fcc2 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 18:56:58 +0200 Subject: [PATCH 04/31] matrix-notifier: allow specifying message type --- matrix-notifier | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/matrix-notifier b/matrix-notifier index 2dfb61c..4ad553b 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 " -t, --type" + print_err " which message type should be sent" + print_err " must be one of 'text' or 'notice'" } # Blessed Stack Overflow @@ -78,17 +81,31 @@ send_message() { fi message_json="$(printf '%s' "$MESSAGE" | - jq --raw-input --slurp "{msgtype: \"m.text\", body: .}")" + jq --raw-input --slurp "{msgtype: \"$MSG_TYPE\", body: .}")" curl -XPOST \ -d "$message_json" \ "$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$token")" 2>/dev/null } +MSG_TYPE='m.text' + while [ $# -gt 0 ]; do opt="$1" shift case "$opt" in + -t|--type) + arg="$1" + shift + if [ -z "$arg" ]; then + print_err "Must give a message type when using '-t|--type'" + exit 1 + elif [ "$arg" != "text" ] && [ "$arg" != "notice" ]; then + print_err "Invalid message type '$arg'" + exit 1 + fi + MSG_TYPE="m.$arg" + ;; -h|--help) usage exit From abf66aa479e28c152f4de43538ae8be14ad8d107 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 18:57:27 +0200 Subject: [PATCH 05/31] matrix-notifier: use 'notice' default message type --- matrix-notifier | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix-notifier b/matrix-notifier index 4ad553b..0309e46 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -87,7 +87,7 @@ send_message() { "$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$token")" 2>/dev/null } -MSG_TYPE='m.text' +MSG_TYPE='m.notice' while [ $# -gt 0 ]; do opt="$1" From 7b714696244d3dbfd54c06e774134489fe4d36a4 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 19:04:17 +0200 Subject: [PATCH 06/31] matrix-notifier: add 'auto' message type This is useful to only ping me when the build is failing. --- matrix-notifier | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/matrix-notifier b/matrix-notifier index 0309e46..9948c55 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -10,8 +10,10 @@ usage() { print_err " -h, --help" print_err " print this usage screen and exit" print_err " -t, --type" - print_err " which message type should be sent" - print_err " must be one of 'text' or 'notice'" + print_err " which message type should be sent must be one of" + print_err " 'text' or 'notice', or 'auto'." + print_err " The special value 'auto' defaults to 'notice', unless" + print_err " 'DRONE_BUILD_STATUS' indicates a failure." } # Blessed Stack Overflow @@ -100,11 +102,17 @@ while [ $# -gt 0 ]; do if [ -z "$arg" ]; then print_err "Must give a message type when using '-t|--type'" exit 1 - elif [ "$arg" != "text" ] && [ "$arg" != "notice" ]; then + elif [ "$arg" == "text" ] || [ "$arg" == "notice" ]; then + MSG_TYPE="m.$arg" + elif [ "$arg" == "auto" ]; then + MSG_TYPE="m.notice" + if [ "$DRONE_BUILD_STATUS" == "failure" ]; then + MSG_TYPE="m.text" + fi + else print_err "Invalid message type '$arg'" exit 1 fi - MSG_TYPE="m.$arg" ;; -h|--help) usage From 0f701ddbe291a80430fa1c811e942f8b08c4eb32 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 19:13:56 +0200 Subject: [PATCH 07/31] matrix-notifier: refactor handling of message type --- matrix-notifier | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/matrix-notifier b/matrix-notifier index 9948c55..bb52cb0 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -59,6 +59,18 @@ ensure_variables() { fi } +get_message_type() { + if [ "$MSG_TYPE" == "auto" ]; then + if [ "$DRONE_BUILD_STATUS" == "failure" ]; then + MSG_TYPE="text" + else + MSG_TYPE="notice" + fi + fi + + printf '%s' "m.$MSG_TYPE" +} + send_message() { local login_json local token @@ -83,13 +95,13 @@ send_message() { fi message_json="$(printf '%s' "$MESSAGE" | - jq --raw-input --slurp "{msgtype: \"$MSG_TYPE\", body: .}")" + jq --raw-input --slurp "{msgtype: \"$(get_message_type)\", body: .}")" curl -XPOST \ -d "$message_json" \ "$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$token")" 2>/dev/null } -MSG_TYPE='m.notice' +MSG_TYPE='notice' while [ $# -gt 0 ]; do opt="$1" @@ -99,20 +111,19 @@ while [ $# -gt 0 ]; do -t|--type) arg="$1" shift + if [ -z "$arg" ]; then print_err "Must give a message type when using '-t|--type'" exit 1 - elif [ "$arg" == "text" ] || [ "$arg" == "notice" ]; then - MSG_TYPE="m.$arg" - elif [ "$arg" == "auto" ]; then - MSG_TYPE="m.notice" - if [ "$DRONE_BUILD_STATUS" == "failure" ]; then - MSG_TYPE="m.text" - fi - else - print_err "Invalid message type '$arg'" - exit 1 fi + + for type in text notice auto; do + if [ "$arg" == "$type" ]; then + continue 2 # Go to next argument + fi + done + print_err "Invalid message type '$arg'" + exit 1 ;; -h|--help) usage From dfbc17b518c91b00f7ad4000208b79fe71067474 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 19:14:29 +0200 Subject: [PATCH 08/31] matrix-notifier: use 'auto' default message type This is the one that makes most sense in CI. --- matrix-notifier | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix-notifier b/matrix-notifier index bb52cb0..b92fb3e 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -101,7 +101,7 @@ send_message() { "$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$token")" 2>/dev/null } -MSG_TYPE='notice' +MSG_TYPE='auto' while [ $# -gt 0 ]; do opt="$1" From bad1c90d39b49578e948f7d192cfbb942749d178 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 14:34:44 +0200 Subject: [PATCH 09/31] ci: add Drone CI This is my first experiment to make sure I can actually use this in my CI. --- .drone.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..cd4c77d --- /dev/null +++ b/.drone.yml @@ -0,0 +1,27 @@ +--- +kind: pipeline +type: exec +name: Matrix Notifier check + +steps: +- name: Pre-commit checks + commands: + - nix flake check + +- name: Notifiy + commands: + - nix run . + environment: + ADDRESS: + from_secret: matrix_homeserver + ROOM: + from_secret: matrix_roomid + USER: + from_secret: matrix_username + PASS: + from_secret: matrix_password + when: + status: + - failure + - success +... From feb8639213329e0756304bb4b838c6b37895350e Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 21:44:09 +0200 Subject: [PATCH 10/31] flake: add meta to package derivation --- flake.nix | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/flake.nix b/flake.nix index 5388bdc..9a3dbba 100644 --- a/flake.nix +++ b/flake.nix @@ -105,6 +105,17 @@ patchShebangs $out/bin/${pname} wrapProgram $out/bin/${pname} --prefix PATH : "${wrapperPath}" ''; + + meta = with pkgs.lib; { + description = '' + A very simple bash script that can be used to send a message to + a Matrix room + ''; + homepage = "https://gitea.belanyi.fr/ambroisie/${pname}"; + license = licenses.mit; + platforms = platforms.unix; + maintainers = with maintainers; [ ambroisie ]; + }; }; }; } From 74b5d9b48cb1e2f7c45bdd9cef7455b98b2147a5 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 15 Jun 2021 18:28:12 +0200 Subject: [PATCH 11/31] matrix-notifier: use full URL to Drone build --- matrix-notifier | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix-notifier b/matrix-notifier index b92fb3e..e9b7f1b 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -37,7 +37,7 @@ rawurlencode() { default_drone_message() { local msg="Build ${DRONE_BUILD_STATUS}" - msg="$msg ${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/${DRONE_BUILD_NUMBER}" + msg="$msg ${DRONE_SYSTEM_PROTO}://${DRONE_SYSTEM_HOST}/${DRONE_REPO}/${DRONE_BUILD_NUMBER}" msg="$msg (${DRONE_BRANCH})" printf '%s' "$msg" } From 11ed9e71d062a2e863dda2b896abb8763ea3db47 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 15 Jun 2021 18:29:07 +0200 Subject: [PATCH 12/31] flake: bump version to v0.1.1 --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 9a3dbba..b2bcaa9 100644 --- a/flake.nix +++ b/flake.nix @@ -75,7 +75,7 @@ packages = { matrix-notifier = pkgs.stdenvNoCC.mkDerivation rec { pname = "matrix-notifier"; - version = "0.1.0"; + version = "0.1.1"; src = ./matrix-notifier; From 261f3b27ec0c441e9e1c39ac7b4f00c7c0dd1ce7 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 15 Jun 2021 18:38:26 +0200 Subject: [PATCH 13/31] matrix-notifier: prefer tag in Drone message --- matrix-notifier | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix-notifier b/matrix-notifier index e9b7f1b..22fca65 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -38,7 +38,7 @@ rawurlencode() { default_drone_message() { local msg="Build ${DRONE_BUILD_STATUS}" msg="$msg ${DRONE_SYSTEM_PROTO}://${DRONE_SYSTEM_HOST}/${DRONE_REPO}/${DRONE_BUILD_NUMBER}" - msg="$msg (${DRONE_BRANCH})" + msg="$msg (${DRONE_TAG:-$DRONE_BRANCH})" printf '%s' "$msg" } From 75d482cd6b0045fa463f0375d2bdf4b2ea4a3d1d Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 15 Jun 2021 18:39:19 +0200 Subject: [PATCH 14/31] flake: bump version to v0.1.2 --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index b2bcaa9..841883f 100644 --- a/flake.nix +++ b/flake.nix @@ -75,7 +75,7 @@ packages = { matrix-notifier = pkgs.stdenvNoCC.mkDerivation rec { pname = "matrix-notifier"; - version = "0.1.1"; + version = "0.1.2"; src = ./matrix-notifier; From 758e4599ed336208c524c5ba4cb7af5f57dd9c1a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 16 Jun 2021 20:42:17 +0200 Subject: [PATCH 15/31] 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 16/31] 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 17/31] 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 18/31] 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 19/31] 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; From b6a9c7e6a4194c85bdd0d4133879c0b63230167b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 8 Oct 2021 15:49:05 +0200 Subject: [PATCH 20/31] nix: use 'inputsFrom' --- flake.nix | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/flake.nix b/flake.nix index 7c2bc1c..1437d34 100644 --- a/flake.nix +++ b/flake.nix @@ -63,10 +63,8 @@ devShell = pkgs.mkShell { name = "matrix-notifier"; - buildInputs = with pkgs; [ - curl - jq - shellcheck + inputsFrom = with self.packages.${system}; [ + matrix-notifier ]; inherit (self.checks.${system}.pre-commit) shellHook; From 007994e0a2cadc9503457f04751baf896c45a50b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 3 Jun 2022 16:30:12 +0200 Subject: [PATCH 21/31] nix: migrate from deprecated attributes --- flake.nix | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/flake.nix b/flake.nix index 1437d34..5ddc848 100644 --- a/flake.nix +++ b/flake.nix @@ -36,6 +36,8 @@ in rec { apps = { + default = apps.matrix-notifier; + matrix-notifier = futils.lib.mkApp { drv = packages.matrix-notifier; }; }; @@ -56,21 +58,21 @@ }; }; - defaultApp = apps.matrix-notifier; + devShells = { + default = pkgs.mkShell { + name = "matrix-notifier"; - defaultPackage = packages.matrix-notifier; + inputsFrom = with self.packages.${system}; [ + matrix-notifier + ]; - devShell = pkgs.mkShell { - name = "matrix-notifier"; - - inputsFrom = with self.packages.${system}; [ - matrix-notifier - ]; - - inherit (self.checks.${system}.pre-commit) shellHook; + inherit (self.checks.${system}.pre-commit) shellHook; + }; }; packages = { + default = packages.matrix-notifier; + matrix-notifier = pkgs.stdenvNoCC.mkDerivation rec { pname = "matrix-notifier"; version = "0.2.0"; From 0bc11d85d57dd83bdcbf7b5cf6fe3a1d9d81bcbf Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 18 Mar 2023 12:42:02 +0000 Subject: [PATCH 22/31] nix: fix flake description --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 5ddc848..00e1a27 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,5 @@ { - description = "Ambroisie's blog"; + description = "A simple Matrix notifier for CI purposes"; inputs = { futils = { From eebe509b02983819855deeeb65a78a6cab13acd8 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 18 Mar 2023 12:46:36 +0000 Subject: [PATCH 23/31] matrix-notifier: add woodpecker integration --- matrix-notifier | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/matrix-notifier b/matrix-notifier index af9cdb8..0b99989 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -16,7 +16,7 @@ usage() { print_err " which message type should be sent must be one of" print_err " 'text' or 'notice', or 'auto'." print_err " The special value 'auto' defaults to 'notice', unless" - print_err " 'DRONE_BUILD_STATUS' indicates a failure." + print_err " 'CI_PIPELINE_STATUS'/'DRONE_BUILD_STATUS' indicates a failure." } # Blessed Stack Overflow @@ -38,6 +38,18 @@ rawurlencode() { echo "$encoded") } +default_woodpecker_message() { + local msg="Build ${CI_PIPELINE_STATUS}" + local woodpecker_url="${CI_SYSTEM_LINK}/${CI_REPO}/${CI_PIPELINE_NUMBER}" + if [ "$FORMAT" == "true" ]; then + msg="$msg [${CI_REPO}#${CI_COMMIT_SHA:0:8}]($woodpecker_url)" + else + msg="$msg $woodpecker_url" + fi + msg="$msg (${CI_COMMIT_TAG:-$CI_COMMIT_BRANCH})" + printf '%s' "$msg" +} + default_drone_message() { local msg="Build ${DRONE_BUILD_STATUS}" local drone_url="${DRONE_SYSTEM_PROTO}://${DRONE_SYSTEM_HOST}/${DRONE_REPO}/${DRONE_BUILD_NUMBER}" @@ -51,7 +63,9 @@ default_drone_message() { } ensure_variables() { - if [ "$DRONE" == "true" ] && [ -z "$MESSAGE" ]; then + if [ "$CI" == "woodpecker" ] && [ -z "$MESSAGE" ]; then + MESSAGE="$(default_woodpecker_message)" + elif [ "$DRONE" == "true" ] && [ -z "$MESSAGE" ]; then MESSAGE="$(default_drone_message)" fi @@ -69,7 +83,9 @@ ensure_variables() { get_message_type() { if [ "$MSG_TYPE" == "auto" ]; then - if [ "$DRONE_BUILD_STATUS" == "failure" ]; then + if [ "$CI_PIPELINE_STATUS" == "failure" ]; then + MSG_TYPE="text" + elif [ "$DRONE_BUILD_STATUS" == "failure" ]; then MSG_TYPE="text" else MSG_TYPE="notice" From 04758f81f23c693cca00f8eeb9e12eaa910353c4 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 18 Mar 2023 12:48:28 +0000 Subject: [PATCH 24/31] flake: bump version to v0.3.0 --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 00e1a27..7bda786 100644 --- a/flake.nix +++ b/flake.nix @@ -75,7 +75,7 @@ matrix-notifier = pkgs.stdenvNoCC.mkDerivation rec { pname = "matrix-notifier"; - version = "0.2.0"; + version = "0.3.0"; src = ./matrix-notifier; From cb62f179a348a2dbd99041c93e293fe9b14e6de9 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 1 Apr 2023 16:41:16 +0100 Subject: [PATCH 25/31] ci: add Woodpecker CI workflow --- .woodpecker/check.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 .woodpecker/check.yml diff --git a/.woodpecker/check.yml b/.woodpecker/check.yml new file mode 100644 index 0000000..9de6915 --- /dev/null +++ b/.woodpecker/check.yml @@ -0,0 +1,26 @@ +labels: + type: exec + +pipeline: +- name: pre-commit checks + image: bash + commands: + - nix flake check + +- name: notifiy + image: bash + secrets: + - source: matrix_password + target: pass + - source: matrix_homeserver + target: address + - source: matrix_roomid + target: room + - source: matrix_username + target: user + commands: + - nix run . + when: + status: + - failure + - success From 436483aac7455fe7c157d8f247761bcd7c0abc62 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 1 Apr 2023 17:12:44 +0100 Subject: [PATCH 26/31] ci: remove Drone CI --- .drone.yml | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index cd4c77d..0000000 --- a/.drone.yml +++ /dev/null @@ -1,27 +0,0 @@ ---- -kind: pipeline -type: exec -name: Matrix Notifier check - -steps: -- name: Pre-commit checks - commands: - - nix flake check - -- name: Notifiy - commands: - - nix run . - environment: - ADDRESS: - from_secret: matrix_homeserver - ROOM: - from_secret: matrix_roomid - USER: - from_secret: matrix_username - PASS: - from_secret: matrix_password - when: - status: - - failure - - success -... From d30a2d73667db3cfbcee7b65bdb9da1f98b0705e Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 18 Nov 2023 20:20:45 +0000 Subject: [PATCH 27/31] ci: remove deprecated syntax --- .woodpecker/check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.woodpecker/check.yml b/.woodpecker/check.yml index 9de6915..39bd018 100644 --- a/.woodpecker/check.yml +++ b/.woodpecker/check.yml @@ -1,7 +1,7 @@ labels: type: exec -pipeline: +steps: - name: pre-commit checks image: bash commands: From a57f1cfa71e861ac4eb63b30a35ccc6c9db12370 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 18 Nov 2023 20:21:05 +0000 Subject: [PATCH 28/31] ci: add package check --- .woodpecker/check.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.woodpecker/check.yml b/.woodpecker/check.yml index 39bd018..14aabfe 100644 --- a/.woodpecker/check.yml +++ b/.woodpecker/check.yml @@ -7,6 +7,11 @@ steps: commands: - nix flake check +- name: package check + image: bash + commands: + - nix build + - name: notifiy image: bash secrets: From 48dad0c96254421a77cee4969a36e401f659f0fc Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 18 Nov 2023 20:21:24 +0000 Subject: [PATCH 29/31] ci: use more explicit step name --- .woodpecker/check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.woodpecker/check.yml b/.woodpecker/check.yml index 14aabfe..baa5da1 100644 --- a/.woodpecker/check.yml +++ b/.woodpecker/check.yml @@ -2,7 +2,7 @@ labels: type: exec steps: -- name: pre-commit checks +- name: flake check image: bash commands: - nix flake check From 37491e91e6599eec6b577243ea5c2e0bf3a0272b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 3 Jan 2024 14:33:39 +0000 Subject: [PATCH 30/31] matrix-notifier: fix link to pipeline --- matrix-notifier | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix-notifier b/matrix-notifier index 0b99989..838396a 100755 --- a/matrix-notifier +++ b/matrix-notifier @@ -40,7 +40,7 @@ rawurlencode() { default_woodpecker_message() { local msg="Build ${CI_PIPELINE_STATUS}" - local woodpecker_url="${CI_SYSTEM_LINK}/${CI_REPO}/${CI_PIPELINE_NUMBER}" + local woodpecker_url="${CI_PIPELINE_URL}" if [ "$FORMAT" == "true" ]; then msg="$msg [${CI_REPO}#${CI_COMMIT_SHA:0:8}]($woodpecker_url)" else From 31b3b9fd2482a0704b5cfadb9ab440977d529d4d Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 3 Jan 2024 14:34:55 +0000 Subject: [PATCH 31/31] flake: bump version to v0.4.0 --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 7bda786..e02328a 100644 --- a/flake.nix +++ b/flake.nix @@ -75,7 +75,7 @@ matrix-notifier = pkgs.stdenvNoCC.mkDerivation rec { pname = "matrix-notifier"; - version = "0.3.0"; + version = "0.4.0"; src = ./matrix-notifier;