Last active 1 month ago

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

get_prj_name_by_id.sh Raw
1#!/bin/bash
2set -euo pipefail
3
4# ── Colors ───────────────────────────────────────────────────────────
5RED='\033[0;31m'
6YELLOW='\033[1;33m'
7NC='\033[0m'
8
9# ── Logging ──────────────────────────────────────────────────────────
10log_error() { printf "${RED}[ERR]${NC} %s\n" "$*" >&2; }
11log_warn() { printf "${YELLOW}[WARN]${NC} %s\n" "$*" >&2; }
12
13# ── Usage ────────────────────────────────────────────────────────────
14usage() {
15 cat <<EOF
16Usage: $(basename "$0") [OPTIONS] PROJECT_ID
17
18Get project name from GitLab by its numeric ID.
19
20Options:
21 -h, --help Show this help
22 -t, --token-file=FILE File containing the GitLab personal access token
23 -g, --gitlab-url=URL GitLab instance URL (default: https://git.mws-team.ru)
24
25Examples:
26 $(basename "$0") -t ~/.gitlab_token 42
27 $(basename "$0") --token-file=token.txt --gitlab-url=https://gitlab.com 123
28EOF
29 exit 1
30}
31
32# ── Dependency check ─────────────────────────────────────────────────
33for cmd in curl jq; do
34 if ! command -v "$cmd" &>/dev/null; then
35 log_error "Required command not found: ${cmd}"
36 exit 1
37 fi
38done
39
40# ── Parse arguments ──────────────────────────────────────────────────
41GITLAB_URL="https://git.mws-team.ru"
42TOKEN_FILE=""
43
44translated=()
45while (( $# )); do
46 case "$1" in
47 --help) translated+=("-h"); shift ;;
48 --token-file=*) translated+=("-t" "${1#*=}"); shift ;;
49 --gitlab-url=*) translated+=("-g" "${1#*=}"); shift ;;
50 --) translated+=("--"); shift; translated+=("$@"); break ;;
51 *) translated+=("$1"); shift ;;
52 esac
53done
54set -- "${translated[@]}"
55
56while getopts ":ht:g:" opt; do
57 case "$opt" in
58 h) usage ;;
59 t) TOKEN_FILE="$OPTARG" ;;
60 g) GITLAB_URL="$OPTARG" ;;
61 :) log_error "Missing argument for -${OPTARG}"; usage ;;
62 \?) log_error "Invalid option: -${OPTARG}"; usage ;;
63 esac
64done
65shift $((OPTIND - 1))
66
67# Positional argument: project ID
68PROJECT_ID="${1:-}"
69
70# ── Validate inputs ─────────────────────────────────────────────────
71[[ -z "$TOKEN_FILE" ]] && { log_error "Missing --token-file"; usage; }
72[[ ! -f "$TOKEN_FILE" ]] && { log_error "Token file not found: ${TOKEN_FILE}"; exit 1; }
73[[ -z "$PROJECT_ID" ]] && { log_error "Missing PROJECT_ID"; usage; }
74
75if ! [[ "$PROJECT_ID" =~ ^[0-9]+$ ]]; then
76 log_error "PROJECT_ID must be a positive integer, got '${PROJECT_ID}'"
77 exit 1
78fi
79
80GITLAB_URL="${GITLAB_URL%/}"
81
82TOKEN="$(<"$TOKEN_FILE")"
83readonly TOKEN GITLAB_URL PROJECT_ID
84
85# ── Fetch project info ──────────────────────────────────────────────
86response="$(curl --silent --fail \
87 --header "Authorization: Bearer ${TOKEN}" \
88 "${GITLAB_URL}/api/v4/projects/${PROJECT_ID}")" || {
89 log_error "API request failed for project ${PROJECT_ID}"
90 exit 1
91}
92
93# If the API returned an error message, show it; otherwise print project details.
94echo "$response" | jq '
95 if has("message") then
96 .message
97 else
98 {name, name_with_namespace, path_with_namespace}
99 end
100'
101