Compare commits
7 commits
ca3b5a053c
...
bad1c90d39
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | bad1c90d39 | ||
Bruno BELANYI | dfbc17b518 | ||
Bruno BELANYI | 0f701ddbe2 | ||
Bruno BELANYI | 7b71469624 | ||
Bruno BELANYI | abf66aa479 | ||
Bruno BELANYI | e017deeaa7 | ||
Bruno BELANYI | f45e1378eb |
27
.drone.yml
Normal file
27
.drone.yml
Normal file
|
@ -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
|
||||||
|
...
|
|
@ -4,6 +4,18 @@ print_err() {
|
||||||
printf "%s\n" "$1" >&2
|
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"
|
||||||
|
print_err " -t, --type"
|
||||||
|
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
|
# Blessed Stack Overflow
|
||||||
rawurlencode() {
|
rawurlencode() {
|
||||||
(set +x
|
(set +x
|
||||||
|
@ -30,6 +42,7 @@ default_drone_message() {
|
||||||
printf '%s' "$msg"
|
printf '%s' "$msg"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ensure_variables() {
|
||||||
if [ "$DRONE" == "true" ] && [ -z "$MESSAGE" ]; then
|
if [ "$DRONE" == "true" ] && [ -z "$MESSAGE" ]; then
|
||||||
MESSAGE="$(default_drone_message)"
|
MESSAGE="$(default_drone_message)"
|
||||||
fi
|
fi
|
||||||
|
@ -44,6 +57,24 @@ elif [ -z "$MESSAGE" ]; then
|
||||||
print_err "You must provide MESSAGE"
|
print_err "You must provide MESSAGE"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
||||||
|
local message_json
|
||||||
|
|
||||||
if [ "$(curl -XGET "$ADDRESS/_matrix/client/r0/login" 2>/dev/null |
|
if [ "$(curl -XGET "$ADDRESS/_matrix/client/r0/login" 2>/dev/null |
|
||||||
jq 'any(.flows[].type; .== "m.login.password")')" != "true" ]; then
|
jq 'any(.flows[].type; .== "m.login.password")')" != "true" ]; then
|
||||||
|
@ -52,19 +83,59 @@ if [ "$(curl -XGET "$ADDRESS/_matrix/client/r0/login" 2>/dev/null |
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
LOGIN_JSON="$(printf '%s\n%s' "$USER" "$PASS" |
|
login_json="$(printf '%s\n%s' "$USER" "$PASS" |
|
||||||
jq -Rn '[inputs] | {type: "m.login.password", user: .[0], password: .[1]}')" &>/dev/null
|
jq -Rn '[inputs] | {type: "m.login.password", user: .[0], password: .[1]}')" &>/dev/null
|
||||||
TOKEN="$(curl -XPOST \
|
token="$(curl -XPOST \
|
||||||
-d "$LOGIN_JSON" \
|
-d "$login_json" \
|
||||||
"$ADDRESS/_matrix/client/r0/login" 2>/dev/null| jq .access_token --raw-output)"
|
"$ADDRESS/_matrix/client/r0/login" 2>/dev/null| jq .access_token --raw-output)"
|
||||||
|
|
||||||
if [ -z "$TOKEN" ] || [ "$TOKEN" == "null" ]; then
|
if [ -z "$token" ] || [ "$token" == "null" ]; then
|
||||||
print_err "Error during login"
|
print_err "Error during login"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
MESSAGE_JSON="$(printf '%s' "$MESSAGE" |
|
message_json="$(printf '%s' "$MESSAGE" |
|
||||||
jq --raw-input --slurp '{msgtype: "m.text", body: .}')"
|
jq --raw-input --slurp "{msgtype: \"$(get_message_type)\", body: .}")"
|
||||||
curl -XPOST \
|
curl -XPOST \
|
||||||
-d "$MESSAGE_JSON" \
|
-d "$message_json" \
|
||||||
"$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$TOKEN")" 2>/dev/null
|
"$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$token")" 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
MSG_TYPE='auto'
|
||||||
|
|
||||||
|
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
|
||||||
|
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
|
||||||
|
exit
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
print_err "Unknown argument '$opt'"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
ensure_variables
|
||||||
|
send_message
|
||||||
|
|
Loading…
Reference in a new issue