Created
August 2, 2019 16:05
-
-
Save colorchestra/b87d4c057237fbbb62df3a8b333aae47 to your computer and use it in GitHub Desktop.
Uses the Gitlab API and jq to get the Gitlab User ID by supplying the username.
This file contains hidden or 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/bash | |
# requires curl and jq | |
# assumes usage of Gitlab API v4 | |
# hella hacky | |
url="https://git.example.net" | |
token="abcdefghijklmnop" | |
username="$1" | |
# output is always paginated - get total number of pages from http header | |
numpages=$(curl -sSl -D - "$url/api/v4/users?order_by=id&per_page=50" --header "PRIVATE-TOKEN: $token" | grep -i 'x-total-pages' | awk -F ' ' '{print $2}' | cut -c1 ) | |
# iterate to all pages (each requires one API call) | |
for p in $(seq $numpages); do | |
# create temporary file to store json in and get data | |
now=$(date +%Y%m%d_%H%M%S%z)-$p | |
curl -s "$url/api/v4/users?per_page=50&page=$p" --header "PRIVATE-TOKEN: $token" >> $now-gitlab-users.json | |
# get number of entries on page using jq | |
entries=$(cat $now-gitlab-users.json | jq '. | length') | |
# off by one, dirty as all heck. frick you | |
entries=$(($entries-1)) | |
# iterate through entries on page | |
for e in $(seq 0 $entries); do | |
if [[ $(cat $now-gitlab-users.json | jq .[$e].username | sed 's/\"//g') == "$username" ]]; then | |
# echo ID from json if username matches | |
cat $now-gitlab-users.json | jq .[$e].id | |
rm $now-gitlab-users.json | |
exit 0 | |
fi | |
done | |
rm $now-gitlab-users.json | |
done | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment