Skip to content

Instantly share code, notes, and snippets.

@royalth royalth/comment-added
Last active Aug 24, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.