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