Skip to content

Instantly share code, notes, and snippets.

@peterwwillis
Created April 2, 2019 14:49
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 peterwwillis/290b969a158fc468044952a7c6aac279 to your computer and use it in GitHub Desktop.
Save peterwwillis/290b969a158fc468044952a7c6aac279 to your computer and use it in GitHub Desktop.
Get all GitLab repositories using the GitLab API
#!/usr/bin/env bash
# Get all GitLab repositories from the GitLab API.
GITLAB_HOST="https://gitlab.com"
export TOKEN=$(cat ~/.gitlab-token)
API_VER="v4"
PER_PAGE=100
set -eo pipefail
set -x
tempfile() {
tempprefix=$(basename "$0")
mktemp /tmp/${tempprefix}.XXXXXX
}
function _getcurl () {
HDF="$1"
JSF="$2"
if [ -n "$3" ] ;
then URL="$3"
else URL="$GITLAB_HOST/api/$API_VER/projects?per_page=$PER_PAGE" ;
fi
echo "Getting URL '$URL' ..." 1>&2
curl -s -D $HDF -H "Private-Token: $TOKEN" "$URL" > $JSF
}
function _get_next_link () {
HDF="$1"
grep '^Link:' $HDF | sed -e 's/^Link: //' | sed -e 's/,/\n/g' | grep 'rel="next"' | cut -d ';' -f 1 | sed -e's/^[[:space:]]*<//; s/>$//'
}
function _list_repos () {
FILE="$1"
if [ -n "$2" ] ;
then TYPE="$2" ;
#else TYPE="ssh\|http" ;
else TYPE="ssh" ;
fi
grep -o "\"\($TYPE\)_url_to_repo\":\"[^\"]\+\"" "$FILE" | cut -d : -f 2- | tr -d \"
}
function _on_exit () {
rm -f $GLH $GLO
}
function _main () {
FN="$1"
C=0
GCARG=
if [ -e "$FN" ] ; then
read -p REMOVE "Do you want to remove existing file '$FN'? [y/N] "
if [ "$REMOVE" = "y" -o "$REMOVE" = "Y" ] ; then
rm -f "$FN"
else
echo "Not removing file; exiting"
exit 1
fi
fi
trap _on_exit EXIT
trap _on_exit HUP INT QUIT TERM STOP PWR # 1 2 3 15 30
while [ true ] ; do
if [ $C -eq 9999 ] ; then
echo "Error: there can't be 999,900 repos... detecting infinite loop, exiting"
exit 1
fi
GLH=$(tempfile)
GLO=$(tempfile)
_getcurl $GLH $GLO $GCARG
REPOS=$(_list_repos $GLO)
if [ ! -n "$REPOS" ] ; then
return 0
fi
echo "$REPOS" | tee -a "$FN"
GCARG=$(_get_next_link $GLH)
if [ ! -n "$GCARG" ] ; then
return 0
fi
C=$(($C+1))
done
}
function _usage () {
cat <<EOF
Usage: $0 FILENAME
Queries the GitLab API for all repositories and stores them in FILENAME.
EOF
exit 1
}
if [ "$1" = "-h" -o "$1" = "--help" -o $# -lt 1 ] ; then
_usage
fi
_main "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment