Last active
August 18, 2019 06:37
-
-
Save elbosso/40cb21b275f8ee9a155b47954b376b1f to your computer and use it in GitHub Desktop.
Shell script for using with telegraf exec input - writing number of open gitlab issues per label
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/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 -s -i --header 'Private-Token: xxxx-xxxx-xxxxxxxxxx' "http://<gitlab-host>/api/v4/projects/<projectid>/labels" |grep name| sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}'|grep name|cut -d ':' -f 2|cut -d '"' -f 2) | |
while read -r component; do | |
encoded=$( rawurlencode "$component" ) | |
count=$(curl -i --header 'Private-Token: xxxx-xxxx-xxxxxxxxxx' "http://<gitlab-host>/api/v4/projects/<projectid>/issues?state=opened&scope=all&per_page=1&labels=$encoded" 2>/dev/null|grep "X-Total:"|cut -d ' ' -f 2) | |
encoded=$( lineformatencode "$component" ) | |
echo "openIssues,component=$encoded count=$count" | |
done <<< "$components" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment