Skip to content

Instantly share code, notes, and snippets.

@elbosso
Created December 2, 2020 06:22
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 elbosso/adb994fe7eceb5018d093ba30c840a3d to your computer and use it in GitHub Desktop.
Save elbosso/adb994fe7eceb5018d093ba30c840a3d to your computer and use it in GitHub Desktop.
Bash script to automaticalley create issues in GitLab - use it for example in cron jobs
#!/bin/bash
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
}
usage()
{
echo "usage: openIssue.sh -h host -p projectid [-l labels ] -x token -t title [ -d description ] | [-?]"
}
#HOST=gitlab.fritz.box
#PRIVATETOKEN=SxUM-SNxc-DKeH5kLUcv
#PROJECTID=6
#LABELS=To%20Do
LABELS=""
#LABELS=$( rawurlencode "$LABELS,automatic" )
#TITLE="Issues with auth - encoding test"
#TITLE=$( rawurlencode "$TITLE" )
DESCRIPTION=""
#DESCRIPTION=$( rawurlencode "$DESCRIPTION" )
#http://linuxcommand.org/lc3_wss0120.php
while [ "$1" != "" ]; do
case $1 in
-h | --host ) shift
HOST="$1"
;;
-p | --projectid ) shift
PROJECTID="$1"
;;
-l | --labels ) shift
LABELS="$1"
;;
-x | --token ) shift
PRIVATETOKEN="$1"
;;
-t | --title ) shift
TITLE="$1"
;;
-d | --description ) shift
DESCRIPTION="$1"
;;
-? | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
if [ -z "${HOST+x}" ]; then echo "Error: host is unset" && exit 2; fi
if [ -z "${PRIVATETOKEN+x}" ]; then echo "Error: token is unset" && exit 2; fi
if [ -z "${TITLE+x}" ]; then echo "Error: title is unset" && exit 2; fi
if [ -z "${PROJECTID+x}" ]; then echo "Error: projectid is unset" && exit 2; fi
LABELS=$( rawurlencode "$LABELS,automatic" )
TITLE=$( rawurlencode "$TITLE" )
DESCRIPTION=$( rawurlencode "$DESCRIPTION" )
ISSUEID=$(curl --request POST --header "PRIVATE-TOKEN: $PRIVATETOKEN" "http://$HOST/api/v4/projects/$PROJECTID/issues?title=$TITLE&labels=$LABELS&description=$DESCRIPTION"|jq .iid)
ISSUEURL=$(curl --header "PRIVATE-TOKEN: $PRIVATETOKEN" "http://$HOST/api/v4/projects/$PROJECTID/issues/$ISSUEID"|jq .web_url)
COMMENT="Created automatically at "$(LC_ALL=de_DE.utf8 date)
COMMENT=$( rawurlencode "$COMMENT" )
curl --request POST --header "PRIVATE-TOKEN: $PRIVATETOKEN" "http://$HOST/api/v4/projects/$PROJECTID/issues/$ISSUEID/notes?body=$COMMENT" >/dev/null
echo "created $ISSUEURL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment