-
-
Save dubiouscript/5553dce89497ffd9805dd0de16503e8d to your computer and use it in GitHub Desktop.
GitLab Snippet Command Line Tool
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 | |
# | |
# Copyright 2016-2017 Martin Goellnitz | |
# | |
# update gitlab api | |
# https://gist.github.com/dubiouscript/5553dce89497ffd9805dd0de16503e8d | |
# -dscript- | |
# | |
# 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 --help |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 listet." | |
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=0 | |
# api version update; | |
WURL="$GITLAB""/api/v4" ; | |
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=20 | |
fi | |
if [ "$1" = "-i" ] ; then | |
VISIBILITY=10 | |
fi | |
shift | |
PSTART=$( echo $1|sed -e 's/^\(.\).*/\1/g') | |
done | |
FILEPATH=$1 | |
TITLE=$2 | |
HEADER="PRIVATE-TOKEN: $TOKEN" | |
# echo project: $PROJECT title: $TITLE file: $FILEPATH gitlab: $GITLAB with token $TOKEN | |
if [ ! -z "$PROJECT" ] ; then | |
_usr=$( echo ${PROJECT%%/*} ) | |
PID=$(curl -k -H "$HEADER" ${WURL}/users/"$_usr"/projects?per_page=100 2> /dev/null|jq '.[]|select(.path_with_namespace=="'$PROJECT'")|.id') | |
# echo "project ID for $PROJECT is $PID" | |
if [ -z "$PID" ] ; then | |
echo "Unknown project $PROJECT on $GITLAB ($TOKEN)" | |
exit 1 | |
fi | |
URLADD="/projects/$PID" | |
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" ${WURL}$URLADD/snippets 2> /dev/null|jq '.[]|.file_name'|sed -e s/^\"//g|sed -e s/\"$//g | |
else | |
FILE=$(basename $FILEPATH) | |
api_tmp_snipets=$(curl -k -H "$HEADER" ${WURL}$URLADD/snippets 2> /dev/null); | |
SNID=$( echo "$api_tmp_snipets" |jq '.[]|select(.file_name=="'$FILE'")|.id') | |
if [ ! -z "$SNID" ] ; then | |
PROJECT=$( echo "$api_tmp_snipets"|jq '.[]|select(.file_name=="'$FILE'")|.web_url'| awk -F/ '{print $4"/"$5 }' ) | |
_usr=$( echo ${PROJECT%%/*} ) | |
# echo "Snippet project: $PROJECT" | |
PID=$(curl -k -H "$HEADER" ${WURL}/users/"$_usr"/projects?per_page=100 2> /dev/null|jq '.[]|select(.path_with_namespace=="'$PROJECT'")|.id') | |
if [ -z "$PID" ] ; then | |
echo "Unknown project $PROJECT on $GITLAB" | |
exit 1 | |
fi | |
SNURL=${WURL}/projects/$PID/snippets/${SNID} | |
fi | |
# echo "Snurl: $SNURL" | |
if [ ! -z "$DELETE" ] ; then | |
if [ -z "$SNURL" ] ; then | |
echo "No snippet '$FILE' found in ${WURL}/$PROJECT to delete" | |
else | |
echo "Deleting snippet '$FILE' from ${WURL}/$PROJECT" | |
curl -k -X DELETE -H "$HEADER" ${SNURL} 2> /dev/null > /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 ${WURL}/$PROJECT from '$FILEPATH'" | |
curl -k -X POST -H "$HEADER" -F "code=<$FILEPATH" -F "file_name=$FILE" -F "title=$TITLE" -F "visibility_level=$VISIBILITY" ${WURL}$URLADD/snippets 2> /dev/null > /dev/null | |
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 | |
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 | |
fi | |
fi | |
else | |
echo "Fetching snippet '$FILE' as '$FILEPATH' from $GITLAB/$PROJECT" | |
snippet_json=$( curl -k -H "$HEADER" ${SNURL} 2> /dev/null) | |
CHECK="$(echo "$snippet_json" | grep ".*message.*404.*Not.Found" )" | |
if [ ! -z "$CHECK" ] ; then | |
echo "'$FILEPATH' not found in $GITLAB/$PROJECT" | |
else | |
# save file!! | |
echo "Saveing snippet '$FILE' as '$FILEPATH'" | |
curl -k -H "$HEADER" ${SNURL}/raw 2> /dev/null > $FILEPATH; | |
fi | |
fi | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment