From f45e1378ebc4f05707f57f260d8b82497c6cb5ee Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 18:56:36 +0200 Subject: [PATCH 1/7] 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 2/7] 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 3/7] 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 4/7] 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 5/7] 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 6/7] 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 7/7] 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 +...