get_prj_name_by_id.sh
· 1.8 KiB · Bash
Raw
#!/bin/bash
#
usage() {
echo "$(basename ${0}) [-h|--help] [-t|--token-file=<file>] [-g|--gitlab-url=<url>] ID"
echo "Get project name from Gitlab by id"
echo " --help | -h Print this help"
echo " --token-file= | -t <file> Set file with Gitlab token"
echo " --gitlab-url=* | -g <url> Set Gtilab URL"
exit 1
}
# Map long options to short ones and rebuild argv
declare GITLAB_URL="https://git.mws-team.ru"
declare TOKEN_FILE=""
declare ID=""
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+=("$@"); set -- ;;
*) 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 ;;
:) echo "Missing arg for -$OPTARG"; usage ;;
\?) echo "Invalid option: -$OPTARG"; usage ;;
esac
done
shift $((OPTIND - 1))
if [[ -n "$@" ]]
then
ID=$@
fi
function checks {
if [ -z "${TOKEN_FILE}" ]
then
echo "Missing token file"
usage
fi
if [ -z "${GITLAB_URL}" ]
then
echo "Missing gitlab url"
usage
fi
if [ ! -f "${TOKEN_FILE}" ]
then
echo "Token file not found"
usage
fi
re='^[0-9]+$'
if ! [[ ${ID} =~ ${re} ]]
then
echo "${ID}"
echo "ID is not number"
usage
fi
}
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}'
| 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 | |
| 75 |