botanikanet revised this gist 1 month ago. Go to revision
1 file changed, 74 insertions, 48 deletions
get_prj_name_by_id.sh
| @@ -1,18 +1,45 @@ | |||
| 1 | 1 | #!/bin/bash | |
| 2 | - | # | |
| 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 ──────────────────────────────────────────────────────────── | |
| 3 | 14 | 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" | |
| 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 | |
| 9 | 29 | exit 1 | |
| 10 | 30 | } | |
| 11 | 31 | ||
| 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="" | |
| 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="" | |
| 16 | 43 | ||
| 17 | 44 | translated=() | |
| 18 | 45 | while (( $# )); do | |
| @@ -20,55 +47,54 @@ while (( $# )); do | |||
| 20 | 47 | --help) translated+=("-h"); shift ;; | |
| 21 | 48 | --token-file=*) translated+=("-t" "${1#*=}"); shift ;; | |
| 22 | 49 | --gitlab-url=*) translated+=("-g" "${1#*=}"); shift ;; | |
| 23 | - | --) translated+=("--"); shift; translated+=("$@"); set -- ;; | |
| 50 | + | --) translated+=("--"); shift; translated+=("$@"); break ;; | |
| 24 | 51 | *) translated+=("$1"); shift ;; | |
| 25 | - | esac | |
| 52 | + | esac | |
| 26 | 53 | done | |
| 27 | 54 | set -- "${translated[@]}" | |
| 28 | 55 | ||
| 29 | 56 | while getopts ":ht:g:" opt; do | |
| 30 | 57 | 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 ;; | |
| 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 ;; | |
| 36 | 63 | esac | |
| 37 | 64 | done | |
| 38 | - | ||
| 39 | 65 | shift $((OPTIND - 1)) | |
| 40 | - | if [[ -n "$@" ]] | |
| 41 | - | then | |
| 42 | - | ID=$@ | |
| 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 | |
| 43 | 78 | fi | |
| 44 | 79 | ||
| 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 | |
| 80 | + | GITLAB_URL="${GITLAB_URL%/}" | |
| 63 | 81 | ||
| 64 | - | re='^[0-9]+$' | |
| 65 | - | if ! [[ ${ID} =~ ${re} ]] | |
| 66 | - | then | |
| 67 | - | echo "${ID}" | |
| 68 | - | echo "ID is not number" | |
| 69 | - | usage | |
| 70 | - | fi | |
| 71 | - | } | |
| 82 | + | TOKEN="$(<"$TOKEN_FILE")" | |
| 83 | + | readonly TOKEN GITLAB_URL PROJECT_ID | |
| 72 | 84 | ||
| 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}' | |
| 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 | + | } | |
| 74 | 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 | + | ' | |
botanikanet revised this gist 8 months ago. Go to revision
No changes
botanikanet revised this gist 8 months 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 | + | ||