botanikanet revised this gist 6 days ago. Go to revision
No changes
botanikanet revised this gist 6 days ago. Go to revision
1 file changed, 74 insertions
get_prj_name_by_id.sh(file created)
| @@ -0,0 +1,74 @@ | |||
| 1 | + | #!/bin/bash | |
| 2 | + | # | |
| 3 | + | usage() { | |
| 4 | + | echo "$(basename ${0}) [-h|--help] [-t|--token-file=<file>] [-g|--gitlab-url=<url>] ID" | |
| 5 | + | echo "Get project name from Gitlab by id" | |
| 6 | + | echo " --help | -h Print this help" | |
| 7 | + | echo " --token-file= | -t <file> Set file with Gitlab token" | |
| 8 | + | echo " --gitlab-url=* | -g <url> Set Gtilab URL" | |
| 9 | + | exit 1 | |
| 10 | + | } | |
| 11 | + | ||
| 12 | + | # Map long options to short ones and rebuild argv | |
| 13 | + | declare GITLAB_URL="https://git.mws-team.ru" | |
| 14 | + | declare TOKEN_FILE="" | |
| 15 | + | declare ID="" | |
| 16 | + | ||
| 17 | + | translated=() | |
| 18 | + | while (( $# )); do | |
| 19 | + | case "$1" in | |
| 20 | + | --help) translated+=("-h"); shift ;; | |
| 21 | + | --token-file=*) translated+=("-t" "${1#*=}"); shift ;; | |
| 22 | + | --gitlab-url=*) translated+=("-g" "${1#*=}"); shift ;; | |
| 23 | + | --) translated+=("--"); shift; translated+=("$@"); set -- ;; | |
| 24 | + | *) translated+=("$1"); shift ;; | |
| 25 | + | esac | |
| 26 | + | done | |
| 27 | + | set -- "${translated[@]}" | |
| 28 | + | ||
| 29 | + | while getopts ":ht:g:" opt; do | |
| 30 | + | case "$opt" in | |
| 31 | + | h) usage ;; | |
| 32 | + | t) TOKEN_FILE=$OPTARG ;; | |
| 33 | + | g) GITLAB_URL=$OPTARG ;; | |
| 34 | + | :) echo "Missing arg for -$OPTARG"; usage ;; | |
| 35 | + | \?) echo "Invalid option: -$OPTARG"; usage ;; | |
| 36 | + | esac | |
| 37 | + | done | |
| 38 | + | ||
| 39 | + | shift $((OPTIND - 1)) | |
| 40 | + | if [[ -n "$@" ]] | |
| 41 | + | then | |
| 42 | + | ID=$@ | |
| 43 | + | fi | |
| 44 | + | ||
| 45 | + | function checks { | |
| 46 | + | if [ -z "${TOKEN_FILE}" ] | |
| 47 | + | then | |
| 48 | + | echo "Missing token file" | |
| 49 | + | usage | |
| 50 | + | fi | |
| 51 | + | ||
| 52 | + | if [ -z "${GITLAB_URL}" ] | |
| 53 | + | then | |
| 54 | + | echo "Missing gitlab url" | |
| 55 | + | usage | |
| 56 | + | fi | |
| 57 | + | ||
| 58 | + | if [ ! -f "${TOKEN_FILE}" ] | |
| 59 | + | then | |
| 60 | + | echo "Token file not found" | |
| 61 | + | usage | |
| 62 | + | fi | |
| 63 | + | ||
| 64 | + | re='^[0-9]+$' | |
| 65 | + | if ! [[ ${ID} =~ ${re} ]] | |
| 66 | + | then | |
| 67 | + | echo "${ID}" | |
| 68 | + | echo "ID is not number" | |
| 69 | + | usage | |
| 70 | + | fi | |
| 71 | + | } | |
| 72 | + | ||
| 73 | + | curl --silent --header "PRIVATE-TOKEN: $(cat ${TOKEN_FILE})" "${GITLAB_URL}/api/v4/projects/${ID}" | jq '. | {name: .name, name_with_namespace: .name_with_namespace, path_with_namespace: .path_with_namespace}' | |
| 74 | + | ||
Newer
Older