|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +function getHelp { |
| 4 | + echo "Setup repository labels |
| 5 | +
|
| 6 | + --help, -h Show this help |
| 7 | + --token, -t GitLab Private-Token (defaults to \$GITLAB_TOKEN) |
| 8 | + --url, -u GitLab base URL (defaults to \$GITLAB_URL or https://gitlab.com) |
| 9 | + --verbose, -v Details process |
| 10 | +
|
| 11 | +To generate a Private-Token see: https://docs.gitlab.com/ee/api/README.html#personal-access-tokens |
| 12 | +
|
| 13 | +Usage: |
| 14 | + $0 --token gitlab-private-token [-u http://gitlab.example.com] owner/repo\n" |
| 15 | +} |
| 16 | + |
| 17 | +function gitlab_labels_api { |
| 18 | + QUERY= |
| 19 | + [[ ! -z "$3" ]] && QUERY="?$(urlencode $3)" |
| 20 | + [[ -z "$3" ]] && [[ "$1" = "DELETE" ]] && QUERY="?$(urlencode $2)" |
| 21 | + curl -w "\nStatus: %{http_code}\n" -H "Private-Token: $GITLAB_TOKEN" -sL "$GITLAB_URL/api/v4/projects/$GITLAB_REPO/labels$QUERY" -X "$1" -d "$2" |
| 22 | +} |
| 23 | + |
| 24 | +urlencode() { |
| 25 | + # urlencode <string> |
| 26 | + old_lc_collate=$LC_COLLATE |
| 27 | + LC_COLLATE=C |
| 28 | + |
| 29 | + local length="${#1}" |
| 30 | + for (( i = 0; i < length; i++ )); do |
| 31 | + local c="${1:i:1}" |
| 32 | + case $c in |
| 33 | + [a-zA-Z0-9.~_-]) printf "$c" ;; |
| 34 | + *) printf '%%%02X' "'$c" ;; |
| 35 | + esac |
| 36 | + done |
| 37 | + |
| 38 | + LC_COLLATE=$old_lc_collate |
| 39 | +} |
| 40 | + |
| 41 | +VERBOSE=0 |
| 42 | +[[ -z "$GITLAB_URL" ]] && GITLAB_URL=https://gitlab.com/ |
| 43 | +args=("$@") |
| 44 | +for i in "$@" |
| 45 | +do |
| 46 | + if [ ! -z "$counter" ] && [[ "$counter" != "$readed_counter" ]]; then |
| 47 | + counter=$[$counter + 1] |
| 48 | + continue |
| 49 | + fi |
| 50 | + |
| 51 | + if [[ "$i" = "--help" ]] || [[ "$i" = "-h" ]]; then |
| 52 | + printf "$(getHelp)" |
| 53 | + exit 0 |
| 54 | + elif [[ $i == '--token' ]] || [[ $i == '-t' ]]; then |
| 55 | + GITLAB_TOKEN=${args[$counter + 1]} |
| 56 | + readed_counter=$[$readed_counter + 1] |
| 57 | + elif [[ $i == '--url' ]] || [[ $i == '-u' ]]; then |
| 58 | + GITLAB_URL=${args[$counter + 1]} |
| 59 | + readed_counter=$[$readed_counter + 1] |
| 60 | + elif [[ $i == '--verbose' ]] || [[ $i == '-v' ]]; then |
| 61 | + VERBOSE=1 |
| 62 | + else |
| 63 | + GITLAB_REPO=$i |
| 64 | + fi |
| 65 | + |
| 66 | + readed_counter=$[$readed_counter + 1] |
| 67 | + counter=$[$counter + 1] |
| 68 | +done |
| 69 | + |
| 70 | +if [ -z "$GITLAB_REPO" ]; then |
| 71 | + read -p "Type your GitLab repository name (owner/repo_name): " GITLAB_REPO |
| 72 | +fi |
| 73 | + |
| 74 | +if [ -z "$GITLAB_TOKEN" ]; then |
| 75 | + read -p "Type your GitLab Private-token: " GITLAB_TOKEN |
| 76 | +fi |
| 77 | + |
| 78 | +if [ -z "$GITLAB_TOKEN" ] || [ -z "$GITLAB_REPO" ]; then |
| 79 | + >&2 echo "There are missing parameters !" |
| 80 | + >&2 printf "$(getHelp)" |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | + |
| 84 | +GITLAB_REPO=$(urlencode $GITLAB_REPO) |
| 85 | + |
| 86 | +REMOVE_DEFAULT_LABELS='bug |
| 87 | +confirmed |
| 88 | +critical |
| 89 | +discussion |
| 90 | +documentation |
| 91 | +enhancement |
| 92 | +suggestion |
| 93 | +support' |
| 94 | + |
| 95 | +LABELS='Category: Backend,c2e0c6, |
| 96 | +Category: Business/Meetings,0e8a16, |
| 97 | +Category: DevOps,fef2c0, |
| 98 | +Category: Frontend,bfdadc, |
| 99 | +Category: Infrastructure,f0e68c, |
| 100 | +Category: Report,40e0d0, |
| 101 | +Category: Unit test,ededed, |
| 102 | +Level: Easy,48d1cc, |
| 103 | +Level: Medium,20b2aa, |
| 104 | +Level: Hard,008b8b, |
| 105 | +Priority: Highest,b60205,1 |
| 106 | +Priority: High,fef2c0,2 |
| 107 | +Priority: Medium,d4c5f9,3 |
| 108 | +Priority: Low,d4c5f9,4 |
| 109 | +Priority: Lowest,ededed,5 |
| 110 | +Stage: Analysis,e6e6e6, |
| 111 | +Stage: Backlog,ededed, |
| 112 | +Stage: Cancelled,000000, |
| 113 | +Stage: In progress,fbca04, |
| 114 | +Stage: Review,0052cc, |
| 115 | +Stage: Testing,e616e6, |
| 116 | +Status: Blocked,d93f0b, |
| 117 | +Status: Duplicated,c5def5, |
| 118 | +Status: Impediment,b60205, |
| 119 | +Status: Needs Fixing,ff8c00, |
| 120 | +Type: Bug,fc2929, |
| 121 | +Type: Improvement,84b6eb, |
| 122 | +Type: New feature,052cc, |
| 123 | +Type: Sub-task,ededed,' |
| 124 | + |
| 125 | +if [[ "$VERBOSE" == 1 ]]; then |
| 126 | + echo "Removing default labels" |
| 127 | +fi |
| 128 | + |
| 129 | +while read -r label; do |
| 130 | + response=$(gitlab_labels_api DELETE "name=$label") |
| 131 | + if [[ "$response" != *"Status: 204"* ]]; then |
| 132 | + [[ "$response" != *"Status: 404"* ]] && >&2 echo "Error removing \"$label\": $response" |
| 133 | + elif [[ "$VERBOSE" == 1 ]]; then |
| 134 | + echo "Label \"$label\" removed" |
| 135 | + fi |
| 136 | +done <<< "$REMOVE_DEFAULT_LABELS" |
| 137 | + |
| 138 | +if [[ "$VERBOSE" == 1 ]]; then |
| 139 | + echo "Creating new labels" |
| 140 | +fi |
| 141 | + |
| 142 | +while read -r label; do |
| 143 | + label_name=$(echo $label | cut -d , -f 1) |
| 144 | + label_color=$(echo $label | cut -d , -f 2) |
| 145 | + label_priority=$(echo $label | cut -d , -f 3) |
| 146 | + response=$(gitlab_labels_api POST "name=$label_name&color=#$label_color&priority=$label_priority") |
| 147 | + |
| 148 | + if [[ "$response" != *"Status: 201"* ]]; then |
| 149 | + [[ "$response" != *"Status: 409"* ]] && >&2 echo "Error on creating: $label_name, response: $response" |
| 150 | + elif [[ "$VERBOSE" == 1 ]]; then |
| 151 | + echo "Label \"$label_name\" created" |
| 152 | + fi |
| 153 | +done <<< "$LABELS" |
| 154 | + |
0 commit comments