Back to home

Email notice when Gitlab commit

Gitlab's user of project relationship, It's save into the users_projects table.
We can simple get those.

SELECT u.email FROM users u RIGHT JOIN users_projects up ON up.user_id = u.id RIGHT JOIN projects p ON p.id = up.project_id WHERE p.path = 'PROJECT_PATH';

PROJECT_PATH is your Gitlab's path of git repositories. Normally, It's named end of “PROJECT_PATH.git”

We can add this code to the git repository hook.
Before that, we need to adjust the format.

python2.6 -c "import sys; print ','.join(i.strip() for i in sys.stdin.readlines()[1:])"

These code could to conversion the mysql output to out custom format. like:

+---------------------+
| email               |
+---------------------+
| xxxxxx@xxxx.com  |
| yyyyyy@xxxx.com   |
| zzzzzz@xxxx.com  |
+---------------------+

To

xxxxxx@xxxx.com,yyyyyy@xxxx.com,zzzzzz@xxxx.com

Finally, we combine these logic to our shell script.

mysql -ureadonly -p -hlocalhost -e "SELECT u.email FROM users u RIGHT JOIN users_projects up ON up.user_id = 

u.id RIGHT JOIN projects p ON p.id = up.project_id WHERE p.path = 'PROJECT_PATH';“ gitlab | python2.6 -c "import sys; print ','.join(i.strip() for i in sys.stdin.readlines()[1:])”

Then you could continue on the last post, add these instructions.

recipients=$(git config hooks.mailinglist)
if [ -z "$recipients" ]; then
        recipients=$(mysql -ureadonly -p -hlocalhost -e "SELECT u.email FROM users u RIGHT JOIN users_projects up ON up.user_id = u.id RIGHT JOIN projects p ON p.id = up.project_id WHERE p.path = '$PROJECT_PATH';" gitlab | python2.6 -c "import sys; print ','.join(i.strip() for i in sys.stdin.readlines()[1:])")
fi