Created
May 9, 2020 14:51
-
-
Save elbosso/a5cfd424ece7ea79db5abeffc223b9d9 to your computer and use it in GitHub Desktop.
Shell script for using with telegraf exec input - writing number of open gitlab issues per project
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 | |
#https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command | |
rawurlencode() { | |
local string="${1}" | |
local strlen=${#string} | |
local encoded="" | |
local pos c o | |
for (( pos=0 ; pos<strlen ; pos++ )); do | |
c=${string:$pos:1} | |
case "$c" in | |
[-_.~a-zA-Z0-9] ) o="${c}" ;; | |
* ) printf -v o '%%%02x' "'$c" | |
esac | |
encoded+="${o}" | |
done | |
echo "${encoded}" # You can either set a return variable (FASTER) | |
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p | |
} | |
lineformatencode() { | |
local string="${1}" | |
local strlen=${#string} | |
local encoded="" | |
local pos c o | |
for (( pos=0 ; pos<strlen ; pos++ )); do | |
c=${string:$pos:1} | |
case "$c" in | |
[+\Q/\E-_.~a-zA-Z0-9] ) o="$c" ;; | |
* ) o="\\$c" | |
esac | |
encoded+="${o}" | |
done | |
echo "${encoded}" # You can either set a return variable (FASTER) | |
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p | |
} | |
components=$(curl -i --header 'Private-Token: SxUM-SNxc-DKeH5kLUcv' "http://gitlab.fritz.box/api/v4/projects/" 2>/dev/null|grep name|jq .[].id |cut -d '"' -f 2) | |
while read -r component; do | |
encoded=$( rawurlencode "$component" ) | |
projectname=$(curl -i --header 'Private-Token: SxUM-SNxc-DKeH5kLUcv' "http://gitlab.fritz.box/api/v4/projects/$encoded" 2>/dev/null|grep name|jq .name |cut -d '"' -f 2) | |
count=$(curl -i --header 'Private-Token: SxUM-SNxc-DKeH5kLUcv' "http://gitlab.fritz.box/api/v4/projects/$encoded/issues?state=opened&scope=all&per_page=1" 2>/dev/null|grep "X-Total:"|cut -d ' ' -f 2) | |
encoded=$( lineformatencode "$component" ) | |
echo "openIssues,projectId=$encoded,projectName=$projectname count=$count" | |
done <<< "$components" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment