From 50948b9ffe31854ad10d8713fd1fc069b116916a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 19:04:17 +0200 Subject: [PATCH 1/3] 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 8f4062256e4ae92062c95141b0b67bfe15fb7956 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 19:13:56 +0200 Subject: [PATCH 2/3] 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 9dc5006663a1326902316eea1d309744cd6ad9f4 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 19:14:29 +0200 Subject: [PATCH 3/3] 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"