Skip to content

Instantly share code, notes, and snippets.

@royalth
Last active August 24, 2017 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royalth/5c2de927953c47115553a52107765427 to your computer and use it in GitHub Desktop.
Save royalth/5c2de927953c47115553a52107765427 to your computer and use it in GitHub Desktop.
Gerrit comment-added hook sending Rocket.Chat messages to change owner
#!/bin/bash
logFile=/tmp/comment-added.log
CHANGEURL=
PROJECT=
AUTHOR=
COMMENT=
SCORE=
OWNER=
changeOwners=<list of change owners that we allow to send messages to>
username=<username>
password=<password>
rocketServerUrl=<Rocket.Chat server address>
sendMessage() {
msg=`echo "$AUTHOR at $CHANGEURL: $SCORE, $COMMENT" | tr '\n' ' '` >> $logFile 2>&1
authData=`curl $rocketServerUrl/api/v1/login -d "username=$username&password=$password"`
userId=`echo $authData | jq -r '.data.userId'`
authToken=`echo $authData | jq -r '.data.authToken'`
curl -H "X-Auth-Token: $authToken" \
-H "X-User-Id: $userId" \
-H "Content-Type: application/json" \
-d "{\"channel\": \"@$recipient\", \"text\": \"$msg\"}" \
$rocketServerUrl/api/v1/chat.postMessage >> $logFile 2>&1
}
echo "$@" >> $logFile 2>&1
while [ $# -gt 0 ]
do
case "$1" in
(--change-url) CHANGEURL="$2"; shift ;;
(--author) AUTHOR="$2"; shift ;;
(--comment) COMMENT="$2"; shift ;;
(--change-owner) OWNER="$2"; shift ;;
(--Code-Review) SCORE="$2"; shift ;;
(--Verified) SCORE="$2"; shift ;;
esac
shift
done
email=${OWNER#*\(}
recipient=${email%@*}
authorEmail=${AUTHOR#*\(}
reviewer=${authorEmail%@*}
if [[ " ${changeOwners[*]} " == *"$recipient"* ]]; then
if [ "$reviewer" != "$recipient" ] ; then
sendMessage
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment