|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +function getHelp { |
| 4 | + echo "Setup repository labels |
| 5 | +
|
| 6 | + --help, -h Show this help |
| 7 | + --user, -u GitHub username |
| 8 | + --password, -p GitHub password |
| 9 | + --verbose, -v Details process |
| 10 | +
|
| 11 | +Usage: |
| 12 | + $0 -u githubUser -p githubPassword owner/repo |
| 13 | + " |
| 14 | +} |
| 15 | + |
| 16 | +function github_api { |
| 17 | + curl -u "$GITHUB_USERNAME:$GITHUB_PASSWORD" -sL "https://api.github.com/repos/$GITHUB_REPO/$1" -X "$2" -d "$3" |
| 18 | +} |
| 19 | + |
| 20 | +VERBOSE=0 |
| 21 | +args=("$@") |
| 22 | +for i in "$@" |
| 23 | +do |
| 24 | + if [ ! -z "$counter" ] && [[ "$counter" != "$readed_counter" ]]; then |
| 25 | + counter=$[$counter + 1] |
| 26 | + continue |
| 27 | + fi |
| 28 | + |
| 29 | + if [[ "$i" = "--help" ]] || [[ "$i" = "-h" ]]; then |
| 30 | + printf "$(getHelp)" |
| 31 | + exit 0 |
| 32 | + elif [[ $i == '--user' ]] || [[ $i == '-u' ]]; then |
| 33 | + GITHUB_USERNAME=${args[$counter + 1]} |
| 34 | + readed_counter=$[$readed_counter + 1] |
| 35 | + elif [[ $i == '--password' ]] || [[ $i == '-p' ]]; then |
| 36 | + GITHUB_PASSWORD=${args[$counter + 1]} |
| 37 | + readed_counter=$[$readed_counter + 1] |
| 38 | + elif [[ $i == '--verbose' ]] || [[ $i == '-v' ]]; then |
| 39 | + VERBOSE=1 |
| 40 | + else |
| 41 | + GITHUB_REPO=$i |
| 42 | + fi |
| 43 | + |
| 44 | + readed_counter=$[$readed_counter + 1] |
| 45 | + counter=$[$counter + 1] |
| 46 | +done |
| 47 | + |
| 48 | +if [ -z "$GITHUB_REPO" ]; then |
| 49 | + read -p "Type your Github repository name (owner/repo_name): " GITHUB_REPO |
| 50 | +fi |
| 51 | + |
| 52 | +if [ -z "$GITHUB_USERNAME" ]; then |
| 53 | + read -p "Type your Github username: " GITHUB_USERNAME |
| 54 | +fi |
| 55 | + |
| 56 | +if [ -z "$GITHUB_PASSWORD" ]; then |
| 57 | + read -p "Type your Github password (won't be shown): " -s GITHUB_PASSWORD |
| 58 | + echo; |
| 59 | +fi |
| 60 | + |
| 61 | +if [ -z "$GITHUB_USERNAME" ] || [ -z "$GITHUB_REPO" ]; then |
| 62 | + >&2 echo "There are missing parameters !" |
| 63 | + >&2 printf "$(getHelp)" |
| 64 | + exit 1 |
| 65 | +fi |
| 66 | + |
| 67 | +REMOVE_DEFAULT_LABELS='bug |
| 68 | +duplicate |
| 69 | +enhancement |
| 70 | +help%20wanted |
| 71 | +invalid |
| 72 | +question |
| 73 | +wontfix' |
| 74 | + |
| 75 | +LABELS='Category: Backend,c2e0c6 |
| 76 | +Category: Business/Meetings,0e8a16 |
| 77 | +Category: DevOps,fef2c0 |
| 78 | +Category: Frontend,bfdadc |
| 79 | +Category: Unit test,ededed |
| 80 | +Priority: High,fef2c0 |
| 81 | +Priority: Highest,b60205 |
| 82 | +Priority: Low,d4c5f9 |
| 83 | +Priority: Lowest,ededed |
| 84 | +Priority: Medium,d4c5f9 |
| 85 | +Stage: Analysis,e6e6e6 |
| 86 | +Stage: Backlog,ededed |
| 87 | +Stage: In progress,fbca04 |
| 88 | +Stage: Review,0052cc |
| 89 | +Stage: Testing,e616e6 |
| 90 | +Status: Blocked,d93f0b |
| 91 | +Status: Duplicated,c5def5 |
| 92 | +Status: Impediment,b60205 |
| 93 | +Type: Bug,fc2929 |
| 94 | +Type: Improvement,84b6eb |
| 95 | +Type: New feature,0052cc |
| 96 | +Type: Sub-task,ededed' |
| 97 | + |
| 98 | +if [[ "$VERBOSE" == 1 ]]; then |
| 99 | + echo "Removing default labels" |
| 100 | +fi |
| 101 | + |
| 102 | +while read -r label; do |
| 103 | + response=$(github_api "labels/$label" DELETE) |
| 104 | + if [[ "$response" == *"message"* ]]; then |
| 105 | + if [[ ! "$response" == *"Not Found"* ]]; then |
| 106 | + echo "Error removing \"$label\": $response" |
| 107 | + fi |
| 108 | + elif [[ "$VERBOSE" == 1 ]]; then |
| 109 | + echo "Label \"$label\" removed" |
| 110 | + fi |
| 111 | +done <<< "$REMOVE_DEFAULT_LABELS" |
| 112 | + |
| 113 | +if [[ "$VERBOSE" == 1 ]]; then |
| 114 | + echo "Creating new labels" |
| 115 | +fi |
| 116 | + |
| 117 | +while read -r label; do |
| 118 | + label_name=$(echo $label | cut -d , -f 1) |
| 119 | + label_color=$(echo $label | cut -d , -f 2) |
| 120 | + response=$(github_api labels POST "{\"name\": \"$label_name\", \"color\":\"$label_color\"}") |
| 121 | + |
| 122 | + if [[ "$response" == *"errors"* ]]; then |
| 123 | + if [[ ! "$response" == *"already_exists"* ]]; then |
| 124 | + >&2 echo "Error on creating: $label_name, response: $response" |
| 125 | + fi |
| 126 | + elif [[ "$VERBOSE" == 1 ]]; then |
| 127 | + echo "Label \"$label_name\" created" |
| 128 | + fi |
| 129 | +done <<< "$LABELS" |
| 130 | + |
0 commit comments