Last active
June 28, 2018 10:18
-
-
Save bviktor/ebf28ca21aa7b7a13d178ec1b455051b to your computer and use it in GitHub Desktop.
GitLab Avatar Updater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -Eeu | |
######################################################################## | |
############################ START SETTINGS ############################ | |
######################################################################## | |
# your GitLab instance' URL | |
export GITLAB_URL='https://gitlab.foobar.com' | |
# directory where you installed glavatar | |
export GLAVATAR_ROOT='/opt/glavatar' | |
# your netscape formatted cookie for the site you download the avatars from | |
# for firefox you can use https://addons.mozilla.org/en-US/firefox/addon/cookies-txt/ | |
export COOKIE_FILE="${GLAVATAR_ROOT}/cookie.txt" | |
export IMAGE_BASE='https://portal.foobar.com/images/members' | |
# database server host | |
export DB_HOST='127.0.0.1' | |
# database server port | |
export DB_PORT='5432' | |
# database name | |
export DB_NAME='gitlabhq_production' | |
# database user | |
export DB_USER='gitlab' | |
# its password | |
export DB_PASS='P@ssw0rd' | |
# needed scopes: api | |
export GITLAB_TOKEN='T0k3n' | |
######################################################################## | |
############################# END SETTINGS ############################# | |
######################################################################## | |
# work variables, you shouldn't change anything from this point | |
export USER_FILE="${GLAVATAR_ROOT}/users.txt" | |
export AVATAR_FILE="${GLAVATAR_ROOT}/avatar.jpg" | |
function pg_query () | |
{ | |
sudo -u postgres sh -c "cd && PGPASSWORD=\"${DB_PASS}\" psql --host=${DB_HOST} --port=${DB_PORT} --dbname=${DB_NAME} --username=${DB_USER} --tuples-only --no-align --field-separator=',' --command \"${1}\"" | |
} | |
# define the query, this assumes you use LDAP users | |
QUERY="SELECT id,username FROM users WHERE password_automatically_set IS TRUE" | |
# execute the query | |
pg_query "${QUERY}" > ${USER_FILE} | |
# loop through user list | |
test -e ${USER_FILE} || exit | |
while read -r line | |
do | |
ID=$(cut -d',' -f1 <<<${line}) | |
UNAME=$(cut -d',' -f2 <<<${line}) | |
# delete previous user's avatar | |
rm -f ${AVATAR_FILE} | |
# download current user's avatar | |
wget -nv --content-on-error --load-cookies=${COOKIE_FILE} "${IMAGE_BASE}/${UNAME}.jpg" -O ${AVATAR_FILE} || true | |
# continue if grep doesn't match, but preserve the match state anyway | |
RET=1 | |
file ${AVATAR_FILE} | grep -i jpeg && RET=$? || true | |
# only do API request if it's actually an image | |
if [ ${RET} -eq 0 ] | |
then | |
SIZE=$(stat --printf="%s" ${AVATAR_FILE}) | |
# gitlab rejects avatars bigger than 200k | |
if [ ${SIZE} -gt 200000 ] | |
then | |
# extent is not exact, use a slightly lower value | |
convert ${AVATAR_FILE} -define jpeg:extent=180kb ${AVATAR_FILE} | |
fi | |
# push it to GitLab via API | |
curl --request PUT --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --form "avatar=@/${AVATAR_FILE}" "${GITLAB_URL}/api/v4/users/${ID}" | |
# cosmetics | |
echo -e '\n' | |
fi | |
done < ${USER_FILE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment