Last active 6 days ago

Получение имени проекта по его id в Gitlab

get_prj_name_by_id.sh Raw
1#!/bin/bash
2#
3usage() {
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
13declare GITLAB_URL="https://git.mws-team.ru"
14declare TOKEN_FILE=""
15declare ID=""
16
17translated=()
18while (( $# )); 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
26done
27set -- "${translated[@]}"
28
29while 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
37done
38
39shift $((OPTIND - 1))
40if [[ -n "$@" ]]
41then
42 ID=$@
43fi
44
45function 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
73curl --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
75