From 599b703d1e1da8c6753dd65d6f8f81327f34a2c8 Mon Sep 17 00:00:00 2001
From: Lucas dos Santos Abreu <lucas.s.abreu@gmail.com>
Date: Wed, 7 Feb 2018 09:13:25 -0200
Subject: [PATCH 1/4] gitlab setup

---
 coderockr-way-gitlab-setup.bash | 153 ++++++++++++++++++++++++++++++++
 1 file changed, 153 insertions(+)
 create mode 100755 coderockr-way-gitlab-setup.bash

diff --git a/coderockr-way-gitlab-setup.bash b/coderockr-way-gitlab-setup.bash
new file mode 100755
index 0000000..c6f9c0f
--- /dev/null
+++ b/coderockr-way-gitlab-setup.bash
@@ -0,0 +1,153 @@
+#!/bin/bash
+
+function getHelp {
+    echo "Setup repository labels
+
+    --help, -h    Show this help
+    --token, -t   GitLab Private-Token (https://docs.gitlab.com/ee/api/README.html#personal-access-tokens)
+    --url, -u     GitLab base URL (default: https://gitlab.com)
+    --verbose, -v Details process
+
+Usage:
+    $0 --token gitlab-private-token [-u http://gitlab.example.com] owner/repo
+    "
+}
+
+function gitlab_labels_api {
+    QUERY=
+    [[ ! -z "$3" ]] && QUERY="?$(urlencode $3)"
+    [[ -z "$3" ]] && [[ "$1" = "DELETE" ]] && QUERY="?$(urlencode $2)"
+    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"
+}
+
+urlencode() {
+    # urlencode <string>
+    old_lc_collate=$LC_COLLATE
+    LC_COLLATE=C
+
+    local length="${#1}"
+    for (( i = 0; i < length; i++ )); do
+        local c="${1:i:1}"
+        case $c in
+            [a-zA-Z0-9.~_-]) printf "$c" ;;
+            *) printf '%%%02X' "'$c" ;;
+        esac
+    done
+
+    LC_COLLATE=$old_lc_collate
+}
+
+VERBOSE=0
+GITLAB_URL=https://gitlab.com/
+args=("$@")
+for i in "$@"
+do
+    if [ ! -z "$counter" ] && [[ "$counter" != "$readed_counter" ]]; then
+        counter=$[$counter + 1]
+        continue
+    fi
+
+    if [[ "$i" = "--help" ]] || [[ "$i" = "-h" ]]; then
+       printf "$(getHelp)"
+       exit 0
+    elif [[ $i == '--token' ]] || [[ $i == '-t' ]]; then
+        GITLAB_TOKEN=${args[$counter + 1]}
+        readed_counter=$[$readed_counter + 1]
+    elif [[ $i == '--url' ]] || [[ $i == '-u' ]]; then
+        GITLAB_URL=${args[$counter + 1]}
+        readed_counter=$[$readed_counter + 1]
+    elif [[ $i == '--verbose' ]] || [[ $i == '-v' ]]; then
+        VERBOSE=1
+    else
+        GITLAB_REPO=$i
+    fi
+
+    readed_counter=$[$readed_counter + 1]
+    counter=$[$counter + 1]
+done
+
+if [ -z "$GITLAB_REPO" ]; then
+    read -p "Type your GitLab repository name (owner/repo_name): " GITLAB_REPO
+fi
+
+if [ -z "$GITLAB_TOKEN" ]; then
+    read -p "Type your GitLab Private-token: " GITLAB_TOKEN
+fi
+
+if [ -z "$GITLAB_TOKEN" ] || [ -z "$GITLAB_REPO" ]; then
+    >&2 echo "There are missing parameters !"
+    >&2 printf "$(getHelp)"
+    exit 1
+fi
+
+GITLAB_REPO=$(urlencode $GITLAB_REPO)
+
+REMOVE_DEFAULT_LABELS='bug
+confirmed
+critical
+discussion
+documentation
+enhancement
+suggestion
+support'
+
+LABELS='Category: Backend,c2e0c6,0
+Category: Business/Meetings,0e8a16,0
+Category: DevOps,fef2c0,0
+Category: Frontend,bfdadc,0
+Category: Infrastructure,f0e68c,0
+Category: Report,40e0d0,0
+Category: Unit test,ededed,0
+Level: Easy,48d1cc,0
+Level: Medium,20b2aa,0
+Level: Hard,008b8b,0
+Priority: Highest,b60205,1
+Priority: High,fef2c0,2
+Priority: Medium,d4c5f9,3
+Priority: Low,d4c5f9,4
+Priority: Lowest,ededed,5
+Stage: Analysis,e6e6e6,0
+Stage: Backlog,ededed,0
+Stage: Cancelled,000000,0
+Stage: In progress,fbca04,0
+Stage: Review,0052cc,0
+Stage: Testing,e616e6,0
+Status: Blocked,d93f0b,0
+Status: Duplicated,c5def5,0
+Status: Impediment,b60205,0
+Status: Needs Fixing,ff8c00,0
+Type: Bug,fc2929,0
+Type: Improvement,84b6eb,0
+Type: New feature,0052cc,0
+Type: Sub-task,ededed,0'
+
+if [[ "$VERBOSE" == 1 ]]; then
+   echo "Removing default labels"
+fi
+
+while read -r label; do
+    response=$(gitlab_labels_api DELETE "name=$label")
+    if [[ "$response" != *"Status: 204"* ]]; then
+        [[ "$response" != *"Status: 404"* ]] && >&2 echo "Error removing \"$label\": $response"
+    elif  [[ "$VERBOSE" == 1 ]]; then
+        echo "Label \"$label\" removed"
+    fi
+done <<< "$REMOVE_DEFAULT_LABELS"
+
+if [[ "$VERBOSE" == 1 ]]; then
+    echo "Creating new labels"
+fi
+
+while read -r label; do
+    label_name=$(echo $label | cut -d , -f 1)
+    label_color=$(echo $label | cut -d , -f 2)
+    label_priority=$(echo $label | cut -d , -f 3)
+    response=$(gitlab_labels_api POST "name=$label_name&color=#$label_color&priority=$label_priority")
+
+    if [[ "$response" != *"Status: 201"* ]]; then
+        [[ "$response" != *"Status: 409"* ]] && >&2 echo "Error on creating: $label_name, response: $response"
+    elif [[ "$VERBOSE" == 1 ]]; then
+         echo "Label \"$label_name\" created"
+    fi
+done <<< "$LABELS"
+

From f3d1a54523727c1a9b0e82bab58f81cfab7e6960 Mon Sep 17 00:00:00 2001
From: Lucas dos Santos Abreu <lucas.s.abreu@gmail.com>
Date: Fri, 9 Feb 2018 08:43:32 -0200
Subject: [PATCH 2/4] no priorization

---
 coderockr-way-gitlab-setup.bash | 48 ++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/coderockr-way-gitlab-setup.bash b/coderockr-way-gitlab-setup.bash
index c6f9c0f..2e9ba2b 100755
--- a/coderockr-way-gitlab-setup.bash
+++ b/coderockr-way-gitlab-setup.bash
@@ -91,35 +91,35 @@ enhancement
 suggestion
 support'
 
-LABELS='Category: Backend,c2e0c6,0
-Category: Business/Meetings,0e8a16,0
-Category: DevOps,fef2c0,0
-Category: Frontend,bfdadc,0
-Category: Infrastructure,f0e68c,0
-Category: Report,40e0d0,0
-Category: Unit test,ededed,0
-Level: Easy,48d1cc,0
-Level: Medium,20b2aa,0
-Level: Hard,008b8b,0
+LABELS='Category: Backend,c2e0c6,
+Category: Business/Meetings,0e8a16,
+Category: DevOps,fef2c0,
+Category: Frontend,bfdadc,
+Category: Infrastructure,f0e68c,
+Category: Report,40e0d0,
+Category: Unit test,ededed,
+Level: Easy,48d1cc,
+Level: Medium,20b2aa,
+Level: Hard,008b8b,
 Priority: Highest,b60205,1
 Priority: High,fef2c0,2
 Priority: Medium,d4c5f9,3
 Priority: Low,d4c5f9,4
 Priority: Lowest,ededed,5
-Stage: Analysis,e6e6e6,0
-Stage: Backlog,ededed,0
-Stage: Cancelled,000000,0
-Stage: In progress,fbca04,0
-Stage: Review,0052cc,0
-Stage: Testing,e616e6,0
-Status: Blocked,d93f0b,0
-Status: Duplicated,c5def5,0
-Status: Impediment,b60205,0
-Status: Needs Fixing,ff8c00,0
-Type: Bug,fc2929,0
-Type: Improvement,84b6eb,0
-Type: New feature,0052cc,0
-Type: Sub-task,ededed,0'
+Stage: Analysis,e6e6e6,
+Stage: Backlog,ededed,
+Stage: Cancelled,000000,
+Stage: In progress,fbca04,
+Stage: Review,0052cc,
+Stage: Testing,e616e6,
+Status: Blocked,d93f0b,
+Status: Duplicated,c5def5,
+Status: Impediment,b60205,
+Status: Needs Fixing,ff8c00,
+Type: Bug,fc2929,
+Type: Improvement,84b6eb,
+Type: New feature,052cc,
+Type: Sub-task,ededed,'
 
 if [[ "$VERBOSE" == 1 ]]; then
    echo "Removing default labels"

From 1c622bd3a3816644e6a1ed5e3352121f9d82b8ff Mon Sep 17 00:00:00 2001
From: Lucas dos Santos Abreu <lucas.s.abreu@gmail.com>
Date: Fri, 9 Feb 2018 08:55:49 -0200
Subject: [PATCH 3/4] improv help

---
 coderockr-way-github-setup.bash |  3 +--
 coderockr-way-gitlab-setup.bash | 11 ++++++-----
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/coderockr-way-github-setup.bash b/coderockr-way-github-setup.bash
index db9acfc..946e076 100755
--- a/coderockr-way-github-setup.bash
+++ b/coderockr-way-github-setup.bash
@@ -9,8 +9,7 @@ function getHelp {
     --verbose, -v   Details process
 
 Usage:
-    $0 -u githubUser -p githubPassword owner/repo
-    "
+    $0 -u githubUser -p githubPassword owner/repo\n"
 }
 
 function github_api {
diff --git a/coderockr-way-gitlab-setup.bash b/coderockr-way-gitlab-setup.bash
index 2e9ba2b..bcdaa81 100755
--- a/coderockr-way-gitlab-setup.bash
+++ b/coderockr-way-gitlab-setup.bash
@@ -4,13 +4,14 @@ function getHelp {
     echo "Setup repository labels
 
     --help, -h    Show this help
-    --token, -t   GitLab Private-Token (https://docs.gitlab.com/ee/api/README.html#personal-access-tokens)
-    --url, -u     GitLab base URL (default: https://gitlab.com)
+    --token, -t   GitLab Private-Token (defaults to \$GITLAB_TOKEN)
+    --url, -u     GitLab base URL (defaults to \$GITLAB_URL or https://gitlab.com)
     --verbose, -v Details process
 
+To generate a Private-Token see: https://docs.gitlab.com/ee/api/README.html#personal-access-tokens
+
 Usage:
-    $0 --token gitlab-private-token [-u http://gitlab.example.com] owner/repo
-    "
+    $0 --token gitlab-private-token [-u http://gitlab.example.com] owner/repo\n"
 }
 
 function gitlab_labels_api {
@@ -38,7 +39,7 @@ urlencode() {
 }
 
 VERBOSE=0
-GITLAB_URL=https://gitlab.com/
+[[ -z "$GITLAB_URL" ]] && GITLAB_URL=https://gitlab.com/
 args=("$@")
 for i in "$@"
 do

From 904773754db559dfdb4ae8496903d6fa92d29831 Mon Sep 17 00:00:00 2001
From: Lucas dos Santos Abreu <lucas.s.abreu@gmail.com>
Date: Fri, 9 Feb 2018 08:55:53 -0200
Subject: [PATCH 4/4] readme

---
 README.md | 49 +++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 43 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md
index 04d2a8a..a479c87 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,19 @@
 Coderockr Way Github Setup
 --------------------------
-Using this script the GitHub project will setup it to add the basic labels needed to control your issues according with the *Coderockr Way* methodology
+Using this script your project will be bootstraped with the basic labels needed to control your issues according with the *Coderockr Way* methodology
 
 You can take a look here: https://github.com/coderockr/coderockr-way-github-setup/labels
 
+We now support setting up projects on [GitHub](#github) and [GitLab](#gitlab), click in the links to see more.
+
 How to use
 ----------
+
+### GitHub
+
 This repository has a script named [`coderockr-way-github-setup.bash`](coderockr-way-github-setup.bash) there are two ways to use it.
 
-You could just call it direct from GitHub using cURL, and it will ask you the info to updated your repository:
+ 1) You could just call it direct from GitHub using cURL, and it will ask you the info to updated your repository:
 
 ```bash
 $ bash -c "$(curl -sL https://raw.githubusercontent.com/coderockr/coderockr-way-github-setup/master/coderockr-way-github-setup.bash)"
@@ -17,7 +22,7 @@ Type your Github username: lucassabreu
 Type your Github password (won't be shown):
 ```
 
-Or you [install into your machine](#install) and run it directaly setting parameters (or not and it will be asked as shown before):
+ 2) Or you [install into your machine](#install) and run it directly setting parameters (or not and it will be asked as shown before):
 
 Usage: `coderockr-way-github-setup -u githubUser -p githubPassword owner/repo`
 
@@ -27,16 +32,48 @@ Usage: `coderockr-way-github-setup -u githubUser -p githubPassword owner/repo`
     --verbose, -v   Details process
 ```
 
-Using Github Token
-------------------
+#### Using Github Token
+
 If you're using github token you must set the environment variable `GITHUB_TOKEN`, then the script will ignore and won't ask for your username and password
 
+### GitLab
+
+Much like the GitHub script, this repository has a script named [`coderockr-way-gitlab-setup.bash`](coderockr-way-gitlab-setup.bash) and there are two ways to use it.
+
+ 1) Just run it directly from GitHub using cURL, a GitLab [personal token](https://docs.gitlab.com/ee/api/README.html#personal-access-tokens) and projects name (owner/project) will be asked:
+
+```bash
+$ bash -c "$(curl -sL https://raw.githubusercontent.com/coderockr/coderockr-way-gitlab-setup/master/coderockr-way-gitlab-setup.bash)"
+Type your GitLab repository name (owner/repo_name): coderockr/awesome-project
+Type your GitLab Private-token: <private-token>
+```
+
+ 2) Or, after [installing it](#gitlab-setup), you can run it directly with parameters (or not and they will be asked as if you were running from GitHub)
+
+Usage: `./coderockr-way-gitlab-setup.bash --token gitlab-private-token [-u http://gitlab.example.com] owner/repo`
+
+```
+    --help, -h    Show this help
+    --token, -t   GitLab Private-Token (defaults to $GITLAB_TOKEN)
+    --url, -u     GitLab base URL (defaults to $GITLAB_URL or https://gitlab.com)
+    --verbose, -v Details process
+```
+
 Install
 -------
 
-To install into your machine just run the commands bellow, and then use the command `coderockr-way-github-setup`.
+To install into your machine just run the commands bellow, and then use the command `coderockr-way-github-setup` or `coderockr-way-gitlab-setup`.
+
+#### GitHub Setup
 
 ```sh
 curl -sL "https://raw.githubusercontent.com/coderockr/coderockr-way-github-setup/master/coderockr-way-github-setup.bash" -o "/usr/local/bin/coderockr-way-github-setup"
 chmod a+x /usr/local/bin/coderockr-way-github-setup
 ```
+
+#### GitLab Setup
+
+```sh
+curl -sL "https://raw.githubusercontent.com/coderockr/coderockr-way-github-setup/master/coderockr-way-gitlab-setup.bash" -o "/usr/local/bin/coderockr-way-gitlab-setup"
+chmod a+x /usr/local/bin/coderockr-way-gitlab-setup
+```
