-
-
Save mgoellnitz/ba98c479ff1462cf12e0acc1a33298b6 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# | |
# Copyright 2016-2021 Martin Goellnitz | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
if [ `jq 2>&1|wc -l` = 0 ] ; then | |
echo "To use this tool, jq must be installed." | |
exit | |
fi | |
if [ "$1" == "-h" ]; then | |
echo "GitLab Project Code Snippet Command Line Tool 'snip.sh'" | |
echo "" | |
echo "$0 [ -u instance url] [-p project] [-t token] [-i] [-a] filename [Title]" | |
echo "" | |
echo "If 'filename' points to an existing file, it is uploaded to Gitlab with an optional title." | |
echo "If 'filename' describes a filename present in the given project, it is downloaded." | |
echo "If 'filename' describes a filename present in any of your projects and you don't mention one of them, the project is automatically discovered." | |
echo "If 'filename' is not present, the available snippets in the project are listed." | |
echo "" | |
echo " -u instance base url - default https://gitlab.com" | |
echo "" | |
echo " -p project name with user or group name" | |
echo "" | |
echo " -t gitlab api token - default \$GITLAB_COM_TOKEN" | |
echo "" | |
echo " -d delete snippet" | |
echo "" | |
echo " -i make internally visible for logged in users" | |
echo "" | |
echo " -a make publicly visible for all" | |
echo "" | |
echo " -h this help message" | |
echo "" | |
echo "So the default action is to list all projects available to the user identified with the access token." | |
echo "" | |
exit 1 | |
fi | |
# defaults | |
GITLAB="https://gitlab.com" | |
TOKEN=$GITLAB_COM_TOKEN | |
VISIBILITY="private" | |
PSTART=$(echo $1|sed -e 's/^\(.\).*/\1/g') | |
while [ "$PSTART" = "-" ] ; do | |
if [ "$1" = "-u" ] ; then | |
shift | |
GITLAB="$1" | |
fi | |
if [ "$1" = "-p" ] ; then | |
shift | |
PROJECT="$1" | |
fi | |
if [ "$1" = "-t" ] ; then | |
shift | |
TOKEN="$1" | |
fi | |
if [ "$1" = "-d" ] ; then | |
DELETE="delete" | |
fi | |
if [ "$1" = "-a" ] ; then | |
VISIBILITY="public" | |
fi | |
if [ "$1" = "-i" ] ; then | |
VISIBILITY="internal" | |
fi | |
shift | |
PSTART=$(echo $1|sed -e 's/^\(.\).*/\1/g') | |
done | |
API="${GITLAB}/api/v4" | |
FILEPATH=$1 | |
TITLE=$2 | |
HEADER="PRIVATE-TOKEN: $TOKEN" | |
# echo project: $PROJECT title: $TITLE file: $FILEPATH gitlab: $GITLAB with token $TOKEN | |
if [ ! -z "$PROJECT" ] ; then | |
PID=$(echo $PROJECT|sed -e 's/\//%2F/g') | |
# echo "project ID for $PROJECT is $PID" | |
URLADD="/projects/$PID" | |
CHECK=$(curl -k -H "$HEADER" ${API}$URLADD 2> /dev/null|grep "404"|wc -l) | |
if [ "$CHECK" -eq "1" ] ; then | |
echo "Unknown project $PROJECT on $GITLAB ($TOKEN)" | |
exit 1 | |
fi | |
else | |
if [ -z "$TOKEN" ] ; then | |
echo "Cannot work without the context of a project or user. Sorry... Use -h for help." | |
exit 1 | |
fi | |
fi | |
if [ -z "$FILEPATH" ] ; then | |
if [ -z "$PROJECT" ] ; then | |
echo "Listing snippet files available to the current user at $GITLAB" | |
else | |
echo "Listing snippet files in $GITLAB/$PROJECT" | |
fi | |
curl -k -H "$HEADER" ${API}$URLADD/snippets 2> /dev/null|jq '.[]|.file_name'|sed -e s/^\"//g|sed -e s/\"$//g | |
else | |
FILE=`basename $FILEPATH` | |
SNID=$(curl -k -H "$HEADER" ${API}$URLADD/snippets 2> /dev/null|jq '.[]|select(.file_name=="'$FILE'")|.id') | |
# echo $SNID | |
if [ ! -z "$SNID" ] ; then | |
PID=$(curl -k -H "$HEADER" ${API}$URLADD/snippets 2> /dev/null \ | |
| jq '.[]|select(.file_name=="'$FILE'").project_id') | |
# echo "Snippet $SNID project: $PID" | |
if [ "$PID" == "null" ] ; then | |
echo "Personal Snippet without Project Context." | |
SNURL=${API}/snippets/${SNID} | |
else | |
SNURL=${API}/projects/$PID/snippets/${SNID} | |
fi | |
fi | |
# echo "SNURL: $SNURL" | |
if [ ! -z "$DELETE" ] ; then | |
if [ -z "$SNURL" ] ; then | |
echo "No snippet '$FILE' found in $GITLAB/$PROJECT to delete" | |
else | |
echo "Deleting snippet '$FILE' from $GITLAB/$PROJECT" | |
# curl -k -X DELETE -H "$HEADER" ${SNURL} 2> /dev/null > /dev/null | |
curl -k -X DELETE -H "$HEADER" ${SNURL} 2> /dev/null | |
fi | |
else | |
if [ -f $FILEPATH ] ; then | |
if [ -z "$SNURL" ] ; then | |
if [ -z "$TITLE" ] ; then | |
TITLE=$FILE | |
fi | |
echo "Creating snippet '$FILE' with title '$TITLE' in $GITLAB/$PROJECT from '$FILEPATH'" | |
# -d "{ \"title\": \"$TITLE\", \"file_name\": \"$FILE\", \"visibility\": \"$VISIBILITY\", \"content\": \"$(cat $FILEPATH)\" }" \ | |
echo "Upload URL ${API}$URLADD/snippets" | |
curl -D - -X POST -H "$HEADER" -H "Content-Type: application/json" \ | |
-d "{ \"title\": \"$TITLE\", \"file_name\": \"$FILE\", \"visibility\": \"$VISIBILITY\", \"content\": \"$(cat $FILEPATH)\" }" \ | |
${API}$URLADD/snippets | |
else | |
if [ -z "$TITLE" ] ; then | |
echo "Updating snippet '$FILE' in $GITLAB/$PROJECT" | |
# curl -k -X PUT -H "$HEADER" -F "code=<$FILEPATH" -F "file_name=$FILE" -F "visibility_level=$VISIBILITY" ${SNURL} 2> /dev/null > /dev/null | |
curl -k -X PUT -H "$HEADER" -H "Content-Type: application/json" \ | |
-d "{ \"file_name\": \"$FILE\", \"visibility\": \"$VISIBILITY\", \"content\": \"$(cat $FILEPATH)\" }" \ | |
${SNURL} 2> /dev/null > /dev/null | |
else | |
echo "Updating snippet '$FILE' with title '$TITLE' in $GITLAB/$PROJECT" | |
# curl -k -X PUT -H "$HEADER" -F "code=<$FILEPATH" -F "file_name=$FILE" -F "title=$TITLE" -F "visibility_level=$VISIBILITY" ${SNURL} 2> /dev/null > /dev/null | |
curl -k -X PUT -H "$HEADER" -H "Content-Type: application/json" \ | |
-d "{ \"title\": \"$TITLE\", \"file_name\": \"$FILE\", \"visibility\": \"$VISIBILITY\", \"content\": \"$(cat $FILEPATH)\" }" \ | |
${SNURL} 2> /dev/null > /dev/null | |
fi | |
fi | |
else | |
if [ -z "$SNURL" ] ; then | |
echo "'$FILEPATH' not found in $GITLAB/$PROJECT" | |
exit 1 | |
fi | |
echo "Fetching snippet '$FILE' as '$FILEPATH'" | |
# TODO: Check is perhaps obsolete | |
CHECK="$(curl -k -H "$HEADER" ${SNURL} 2> /dev/null | grep ".*message.*404.*Not.Found" )" | |
if [ ! -z "$CHECK" ] ; then | |
echo "'$FILEPATH' not found in $GITLAB/$PROJECT" | |
exit 1 | |
fi | |
curl -k -H "$HEADER" ${SNURL}/raw 2> /dev/null > $FILEPATH | |
fi | |
fi | |
fi |
Thank you for the script. But does this really run for you? It certainly hangs for me and it does so in line 18.
I've published a variation of your gist. Have a look at it as well as the reason for the change if you like.
Just realized that I didn't commit some changes due to tests I did which didn't produce consistent code. So, have a look at the current version which seems still to be working for me for most situations (list, fetch, update).
Thank you for this update. Line 18 is unchanged (backticks have been deprecated for command substitution for more than a decade) and thus the script still breaks for me. Even after fixing that, I still cannot create a successful snippet. I believe the URLADD variable is wrong, paths to projects on gitlab do not have /projects/ in the URI. I tried to fiddle with that but eventually gave up.
Hi, so my V4 Update is now integrated but incomplete as you are saying. When trying to upload a snippet, do you use a project context or is it a user-based snippet?
thanks for the script !
iv have made some api v4 changes @ https://gist.github.com/dubiouscript/5553dce89497ffd9805dd0de16503e8d
...
tested appears working
-list
-update
-fetch
untested
-create
-delete