-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitlab-functions.sh
170 lines (142 loc) · 8.26 KB
/
gitlab-functions.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/bash
function create_or_update_gitlab_group() {
GITLAB_GROUP_NAME=$GITLAB_GROUP_PREFIX$GITHUB_ORGANIZATION
GITLAB_GROUP_DESC=$GITHUB_ORGA_DESC
GITLAB_GROUP_AVATAR_URL=$GITHUB_ORGA_AVATAR_URL # gitlab API does not support it yet
# check the $GITLAB_GROUP_NAME exists or not
# create it if necessary and update its description
# https://docs.gitlab.com/ee/api/groups.html#details-of-a-group
GITLAB_GROUP_EXISTS=$(curl -s -S -H "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -o /dev/null -s -w "%{http_code}\n" -X GET $GITLAB_HTTP_BASEURL/api/v4/groups/$GITLAB_GROUP_NAME)
if [ "$GITLAB_GROUP_EXISTS" == "404" ]; then
echo "-> Gitlab $GITLAB_GROUP_NAME group does not exists, create it !"
# https://docs.gitlab.com/ee/api/groups.html#new-group
curl -s -S --header "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X POST \
--form "name=$GITLAB_GROUP_NAME" \
--form "path=$GITLAB_GROUP_NAME" \
--form "description=$GITLAB_GROUP_DESC (backup de $GITHUB_ORGA_URL)" \
--form "visibility=public" \
--form "avatar_url=$GITLAB_GROUP_AVATAR_URL" \
$GITLAB_HTTP_BASEURL/api/v4/groups
fi
# update the gitlab group properties (name, description, visibility...)
# https://docs.gitlab.com/ee/api/groups.html#update-group
curl -s -S --header "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X PUT \
--form "name=$GITLAB_GROUP_NAME" \
--form "description=$GITLAB_GROUP_DESC (backup de $GITHUB_ORGA_URL)" \
--form "visibility=public" \
--form "avatar_url=$GITLAB_GROUP_AVATAR_URL" \
$GITLAB_HTTP_BASEURLapi/v4/groups/$GITLAB_GROUP_NAME
# retrieve the gitlab group id
curl -s -S -H "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X GET \
$GITLAB_HTTP_BASEURL/api/v4/groups/$GITLAB_GROUP_NAME \
>/usr/local/apache2/htdocs/$GITHUB_ORGANIZATION/GITLAB_GROUP_INFO.json
GITLAB_GROUP_ID=$(jq -r .id /usr/local/apache2/htdocs/$GITHUB_ORGANIZATION/GITLAB_GROUP_INFO.json)
}
function create_or_update_gitlab_projects() {
for GITLAB_PROJECT_NAME in $GITHUB_REPOS_NAMES
do
GITHUB_CU=$(cat /usr/local/apache2/htdocs/$GITHUB_ORGANIZATION/$GITLAB_PROJECT_NAME.cu.txt)
GITHUB_DESC=$(cat /usr/local/apache2/htdocs/$GITHUB_ORGANIZATION/$GITLAB_PROJECT_NAME.desc.txt)
GITHUB_DESC=$([ "$GITHUB_DESC" = "null" ] && echo "" || echo $GITHUB_DESC)
GITHUB_HOME=$(cat /usr/local/apache2/htdocs/$GITHUB_ORGANIZATION/$GITLAB_PROJECT_NAME.home.txt)
GITHUB_HOME=$([ "$GITHUB_HOME" = "null" ] && echo "" || echo $GITHUB_HOME)
GITHUB_URL=$(cat /usr/local/apache2/htdocs/$GITHUB_ORGANIZATION/$GITLAB_PROJECT_NAME.url.txt)
GITHUB_URL=$([ "$GITHUB_URL" = "null" ] && echo "" || echo $GITHUB_URL)
# check the $GITLAB_PROJECT_NAME exists or not
# create it if necessary and update its description
# https://docs.gitlab.com/ee/api/projects.html#create-project
GITLAB_PROJECT_EXISTS=$(curl -s -S -H "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -o /dev/null -s -w "%{http_code}\n" -X GET $GITLAB_HTTP_BASEURL/api/v4/projects/${GITLAB_GROUP_NAME}%2F${GITLAB_PROJECT_NAME})
if [ "$GITLAB_PROJECT_EXISTS" == "404" ]; then
echo "-> Gitlab $GITLAB_GROUP_NAME/$GITLAB_PROJECT_NAME project does not exists, create it !"
curl -s -S --header "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X POST \
--form "namespace_id=$GITLAB_GROUP_ID" \
--form "name=$GITLAB_PROJECT_NAME" \
--form "path=$GITLAB_PROJECT_NAME" \
--form "description=$GITHUB_DESC $GITHUB_HOME (backup de $GITHUB_URL)" \
--form "visibility=public" \
--form "archived=false" \
--form "merge_requests_enabled=false" \
--form "issues_enabled=false" \
--form "wiki_enabled=false" \
--form "snippets_enabled=false" \
--form "jobs_enabled=false" \
$GITLAB_HTTP_BASEURL/api/v4/projects
fi
# update the gitlab project properties
LOCAL_CLONE_FOLDER=$(basename $GITHUB_CU)
curl -s -S --header "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X PUT \
--form "namespace_id=$GITLAB_GROUP_ID" \
--form "name=$GITLAB_PROJECT_NAME" \
--form "description=$GITHUB_DESC $GITHUB_HOME (backup de $GITHUB_URL)" \
--form "visibility=public" \
--form "archived=false" \
--form "merge_requests_enabled=false" \
--form "issues_enabled=false" \
--form "wiki_enabled=false" \
--form "snippets_enabled=false" \
--form "jobs_enabled=false" \
$GITLAB_HTTP_BASEURL/api/v4/projects/${GITLAB_GROUP_NAME}%2F${GITLAB_PROJECT_NAME} \
>/usr/local/apache2/htdocs/$GITHUB_ORGANIZATION/$LOCAL_CLONE_FOLDER/GITLAB_PROJECT_INFO.json
# be sure the gitlab project is unarchive (or it's not possible to git push on it!)
curl -s -S --header "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X POST \
$GITLAB_HTTP_BASEURL/api/v4/projects/${GITLAB_GROUP_NAME}%2F${GITLAB_PROJECT_NAME}/unarchive >/dev/null
echo ""
done # $GITHUB_REPOS_NAMES loop
}
function create_ssh_key_for_gitlab_push() {
echo "-> Creating or updating deploy keys on gitlab projects"
for GITLAB_PROJECT_NAME in $GITHUB_REPOS_NAMES
do
# generate a dedicated ssh key for each projects
# because gitlab do not allow to reuse a ssh key between projects
# see {"message":{"deploy_key.fingerprint":["has already been taken"]}}
# these keys will be used to push/mirror on the gitlab repositories thanks to the git command
if [ ! -f ~/.ssh/id_rsa_$GITLAB_PROJECT_NAME ]; then
ssh-keygen -t rsa -f ~/.ssh/id_rsa_$GITLAB_PROJECT_NAME -q -P ""
echo "StrictHostKeyChecking no" > ~/.ssh/config
fi
# search for the existing deploy key and update or create it
GITLAB_DEPLOY_KEY_ID=$(curl -s -S --header "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X GET \
$GITLAB_HTTP_BASEURL/api/v4/projects/$GITLAB_GROUP_NAME%2F$GITLAB_PROJECT_NAME/deploy_keys/ \
| jq -r ".[] | select(.title == \"$GITLAB_GROUP_NAME\") | .id | select (.!=null)")
if [ "$GITLAB_DEPLOY_KEY_ID" != "" ]; then
GITLAB_DEPLOY_KEY=$(curl -s -S --header "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X GET \
$GITLAB_HTTP_BASEURL/api/v4/projects/$GITLAB_GROUP_NAME%2F$GITLAB_PROJECT_NAME/deploy_keys/$GITLAB_DEPLOY_KEY_ID \
| jq -r ".key | select (.!=null)")
# update the existing gitlab project deploy key only if necessary
if [ "$GITLAB_DEPLOY_KEY" != "$(cat ~/.ssh/id_rsa_$GITLAB_PROJECT_NAME.pub)" ]; then
echo "--> Update the gitlab deply key for $GITLAB_GROUP_NAME/$GITLAB_PROJECT_NAME"
curl -s -S --header "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X DELETE \
$GITLAB_HTTP_BASEURL/api/v4/projects/$GITLAB_GROUP_NAME%2F$GITLAB_PROJECT_NAME/deploy_keys/$GITLAB_DEPLOY_KEY_ID
curl -s -S --header "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X POST \
--form "can_push=true" \
--form "key=$(cat ~/.ssh/id_rsa_$GITLAB_PROJECT_NAME.pub)" \
--form "title=$GITLAB_GROUP_NAME" \
$GITLAB_HTTP_BASEURL/api/v4/projects/$GITLAB_GROUP_NAME%2F$GITLAB_PROJECT_NAME/deploy_keys/ \
>/tmp/ezmaster-globs_gitlab_deploy_keys_post.log
fi
else
# create a totally new deploy key for this gitlab project
curl -s -S --header "Private-Token: $GITLAB_PERSONAL_ACCESS_TOKEN" -X POST \
--form "can_push=true" \
--form "key=$(cat ~/.ssh/id_rsa_$GITLAB_PROJECT_NAME.pub)" \
--form "title=$GITLAB_GROUP_NAME" \
$GITLAB_HTTP_BASEURL/api/v4/projects/$GITLAB_GROUP_NAME%2F$GITLAB_PROJECT_NAME/deploy_keys/ \
>/tmp/ezmaster-globs_gitlab_deploy_keys_post.log
fi
done
}
function do_gitlab_mirrors() {
# all the repositories are clonned locally
# we have to add remote gitlab repository
# and push/mirror to it
for GITLAB_PROJECT_NAME in $GITHUB_REPOS_NAMES
do
echo "-> Mirror $GITHUB_CU to $GITLAB_SSH_BASEURL:$GITLAB_GROUP_NAME/$LOCAL_CLONE_FOLDER"
GITHUB_CU=$(cat /usr/local/apache2/htdocs/$GITHUB_ORGANIZATION/$GITLAB_PROJECT_NAME.cu.txt)
LOCAL_CLONE_FOLDER=$(basename $GITHUB_CU)
cd /usr/local/apache2/htdocs/$GITHUB_ORGANIZATION/$LOCAL_CLONE_FOLDER
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_$GITLAB_PROJECT_NAME" \
git push --mirror $GITLAB_SSH_BASEURL:$GITLAB_GROUP_NAME/$LOCAL_CLONE_FOLDER
done # $GITHUB_REPOS_NAMES loop
}