-
Notifications
You must be signed in to change notification settings - Fork 6
/
gitlab-init-remote-v1.2.sh
executable file
·85 lines (69 loc) · 2.45 KB
/
gitlab-init-remote-v1.2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
username=$1
repo_name=$2
if [ "$username" = "-h" ]; then
echo "USAGE:"
echo "1 argument - your username in gitlab"
echo "2 argument - name of the repo you want to create"
exit 1
fi
if [ "$username" = "" ]; then
echo "Could not find username, please provide it."
exit 1
fi
dir_name=`basename $(pwd)`
if [ "$repo_name" = "" ]; then
read -p "Repo name (hit enter to use '$dir_name')? " repo_name
fi
if [ "$repo_name" = "" ]; then
repo_name=$dir_name
fi
# ask user for password
read -s -p "Enter Password: " password
request=`curl --request POST "https://gitlab.com/api/v3/session?login=$username&password=$password"`
if [ "$request" = '{"message":"401 Unauthorized"}' ]; then
echo "Username or password incorrect."
exit 1
fi
token=`echo $request | cut -d , -f 28 | cut -d : -f 2 | cut -d '"' -f 2`
echo -n "Creating GitLab repository '$repo_name' ..."
curl -H "Content-Type:application/json" https://gitlab.com/api/v3/projects?private_token=$token -d '{"name":"'$repo_name'"}' > /dev/null 2>&1
echo " done."
# 2>$1 means that we want redirect stderr to stdout
echo -n "Pushing local code to remote ..."
git init
echo "gitlab-init-remote.sh" > .gitignore
echo ".gitignore" >> .gitignore
git config --global core.excudefiles ~/.gitignore_global
git config --global user.name=$username
git add .
git commit -m "first commit"
git remote add origin https://gitlab.com/$username/$repo_name.git > /dev/null 2>&1
git push -u origin master > /dev/null 2>&1
echo " done."
read -r -p "Do you want to add a user to your repository? [y/n] " input
case $input in
[yY][eE][sS]|[yY])
read -p "Enter username: " newuser
echo "Enter access_level"
echo "10 => Guest access"
echo "20 => Reporter access"
echo "30 => Developer access"
echo "40 => Master access"
read -p "50 => Owner access # Only valid for groups - " access
# find out id of the project and user_id
id=`curl --header "PRIVATE-TOKEN: $token" https://gitlab.com/api/v3/projects | cut -d , -f1 | cut -d : -f2`
user_id=`curl --header "PRIVATE-TOKEN: $token" https://gitlab.com/api/v3/users?username=$newuser | cut -d , -f3 | cut -d : -f2`
# add user to gitlab project
curl --request POST --header "PRIVATE-TOKEN: $token" --data "user_id=$user_id&access_level=$access" https://gitlab.com/api/v3/projects/$id/members
;;
[nN][oO]|[nN])
echo "As you wish, master."
;;
*)
echo "Invalid input..."
;;
esac
echo ""
echo "The created repo is available at following link:"
echo "http://gitlab.com/$username/$repo_name"