dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

gitlab-project (1738B)


      1 #!/bin/sh
      2 
      3 # https://docs.gitlab.com/ee/api/README.html
      4 glserver="https://gitlab.com"
      5 apiversion="v4"
      6 whoami="admesg"
      7 
      8 show_help() {
      9 	echo "${0##*/} { CREATE [VISIBILITY] | DELETE } PROJECTNAME[S]"
     10 	echo "where ACTION is 'create' or 'delete', where 'create' creates a "
     11 	echo "$glserver project with name PROJECTNAME."
     12 	echo "VISIBILITY can be 'private', 'internal', or 'public'."
     13 	echo "If VISIBILITY is not set, the project will be set as 'private'."
     14 }
     15 
     16 send_create_project() {
     17 	curl \
     18 		--header "PRIVATE-TOKEN: $(pass Online/gitlab.com-admesg-private-access-token)" \
     19 		--header "Accept: application/json" \
     20 		--header "Content-type: application/json" \
     21 		--request POST \
     22 		--data "{\"name\":\"$2\", \"visibility_level\":$1}" \
     23 		"$glserver/api/$apiversion/projects"
     24 }
     25 
     26 send_delete_project() {
     27 	curl \
     28 		--header "PRIVATE-TOKEN: $(pass Online/gitlab.com-admesg-private-access-token)" \
     29 		--request DELETE \
     30 		"$glserver/api/$apiversion/projects/$whoami%2F$1"
     31 }
     32 
     33 die() {
     34 	printf '%s\n' "$1" >&2
     35 	exit 1
     36 }
     37 
     38 [ $# -lt 2 ] && (show_help && exit 1)
     39 
     40 visibility=0
     41 delete=0
     42 while :; do
     43 	case "$1" in
     44 		-h|-\?|--help)
     45 			show_help
     46 			exit 0
     47 			;;
     48 		delete)
     49 			delete=1
     50 			;;
     51 		create) # default operation
     52 			;;
     53 		private)
     54 			visibility=0
     55 			;;
     56 		internal)
     57 			visibility=10
     58 			;;
     59 		public)
     60 			visibility=20
     61 			;;
     62 		--) # end all options
     63 			shift
     64 			break
     65 			;;
     66 		-?*)
     67 			die 'Error: Unknown option specified'
     68 			;;
     69 		*)  # no more options
     70 			break
     71 	esac
     72 	shift
     73 done
     74 
     75 [ $# -lt 1 ] && die 'Error: No project name[s] specified'
     76 
     77 # loop over PROJECTNAME[S]
     78 for projectname in "$@"; do
     79 	if [ "$delete" = 1 ]; then
     80 		echo "deleting $projectname"
     81 		send_delete_project "$projectname"
     82 	else
     83 		send_create_project "$visibility" "$projectname"
     84 	fi
     85 done