-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path_bitbucket_repo_functions.sh
executable file
·426 lines (378 loc) · 14.9 KB
/
_bitbucket_repo_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env bash
curl_PUT_cmd="curl --fail -D- --insecure --netrc-file ${netrc_file} -X PUT"
curl_PUT_nofail_cmd="curl --insecure --netrc-file ${netrc_file} -X PUT"
curl_DELETE_cmd="curl --fail -D- --insecure --netrc-file ${netrc_file} -X DELETE"
curl_POST_cmd="curl --fail -D- --insecure --netrc-file ${netrc_file} -X POST -H Content-Type:application/json "
curl_POST_nofail_cmd="curl --insecure --netrc-file ${netrc_file} -X POST -H Content-Type:application/json "
function generate_restriction_json {
local _restriction_type=$1
local _restriction_pattern=$2
local _restriction_type=$3
if [ "${3}x" == "x" ] ; then
echo "{ \
\"id\": 1, \
\"type\": \"$1\", \
\"matcher\": { \
\"id\": \"$2\", \
\"displayId\": \"$2\", \
\"type\": { \
\"id\": \"PATTERN\", \
\"name\": \"Pattern\" \
}, \
\"active\": true \
}, \
\"users\": [ ], \
\"groups\": [ ] \
} "
else
echo "{ \
\"id\": 1, \
\"type\": \"$1\", \
\"matcher\": { \
\"id\": \"$2\", \
\"displayId\": \"$2\", \
\"type\": { \
\"id\": \"PATTERN\", \
\"name\": \"Pattern\" \
}, \
\"active\": true \
}, \
\"users\": [ \"$3\" ], \
\"groups\": [ ] \
} "
fi
}
function generate_restriction_json_groups {
local _restriction_type=$1
local _restriction_pattern=$2
local _restriction_type=$3
echo "{ \
\"id\": 1, \
\"type\": \"$1\", \
\"matcher\": { \
\"id\": \"$2\", \
\"displayId\": \"$2\", \
\"type\": { \
\"id\": \"PATTERN\", \
\"name\": \"Pattern\" \
}, \
\"active\": true \
}, \
\"users\": [ ], \
\"groups\": [ \"$3\" ] \
} "
}
function create_permission_set_restricted {
local _branch_pattern=$1
local _user=$2
local _bitbucket_project=$3
local _repo_name=$4
generate_restriction_json read-only "${_branch_pattern}" "${_user}" > bitbucket_branch_permissions.json
${curl_POST_cmd} ${bitbucket_url}/rest/branch-permissions/2.0/projects/${_bitbucket_project}/repos/${_repo_name}/restrictions --upload-file bitbucket_branch_permissions.json
generate_restriction_json fast-forward-only "${_branch_pattern}" "" > bitbucket_branch_permissions.json
${curl_POST_cmd} ${bitbucket_url}/rest/branch-permissions/2.0/projects/${_bitbucket_project}/repos/${_repo_name}/restrictions --upload-file bitbucket_branch_permissions.json
generate_restriction_json no-deletes "${_branch_pattern}" "" > bitbucket_branch_permissions.json
${curl_POST_cmd} ${bitbucket_url}/rest/branch-permissions/2.0/projects/${_bitbucket_project}/repos/${_repo_name}/restrictions --upload-file bitbucket_branch_permissions.json
rm bitbucket_branch_permissions.json
}
function create_permission_set_restricted_groups {
local _branch_pattern=$1
local _group=$2
local _bitbucket_project=$3
local _repo_name=$4
generate_restriction_json_groups read-only "${_branch_pattern}" "${_group}" > bitbucket_branch_permissions.json
${curl_POST_cmd} ${bitbucket_url}/rest/branch-permissions/2.0/projects/${_bitbucket_project}/repos/${_repo_name}/restrictions --upload-file bitbucket_branch_permissions.json
generate_restriction_json fast-forward-only "${_branch_pattern}" "" > bitbucket_branch_permissions.json
${curl_POST_cmd} ${bitbucket_url}/rest/branch-permissions/2.0/projects/${_bitbucket_project}/repos/${_repo_name}/restrictions --upload-file bitbucket_branch_permissions.json
generate_restriction_json no-deletes "${_branch_pattern}" "" > bitbucket_branch_permissions.json
${curl_POST_cmd} ${bitbucket_url}/rest/branch-permissions/2.0/projects/${_bitbucket_project}/repos/${_repo_name}/restrictions --upload-file bitbucket_branch_permissions.json
rm bitbucket_branch_permissions.json
}
function create_permission_set_rewrite_history {
local _branch_pattern=$1
local _user=$2
local _bitbucket_project=$3
local _repo_name=$4
generate_restriction_json fast-forward-only "${_branch_pattern}" "" > bitbucket_branch_permissions.json
${curl_POST_cmd} ${bitbucket_url}/rest/branch-permissions/2.0/projects/${_bitbucket_project}/repos/${_repo_name}/restrictions --upload-file bitbucket_branch_permissions.json
rm bitbucket_branch_permissions.json
}
function create_permission_set_rewrite_history_deletion {
local _branch_pattern=$1
local _group=$2
local _bitbucket_project=$3
local _repo_name=$4
generate_restriction_json fast-forward-only "${_branch_pattern}" "" > bitbucket_branch_permissions.json
${curl_POST_cmd} ${bitbucket_url}/rest/branch-permissions/2.0/projects/${_bitbucket_project}/repos/${_repo_name}/restrictions --upload-file bitbucket_branch_permissions.json
generate_restriction_json_groups no-deletes "${_branch_pattern}" $_group > bitbucket_branch_permissions.json
${curl_POST_cmd} ${bitbucket_url}/rest/branch-permissions/2.0/projects/${_bitbucket_project}/repos/${_repo_name}/restrictions --upload-file bitbucket_branch_permissions.json
rm bitbucket_branch_permissions.json
}
function create_repo {
if [[ -z $1 ]] ; then
echo "Please set bitbucket_url as parameter 1"
return 1
fi
local _bitbucket_url=$1
if [[ -z $2 ]] ; then
echo "Please set bitbucket project name as parameter 2"
return 1
fi
local _bitbucket_project=$2
if [[ -z $3 ]] ; then
echo "Please set repo name as parameter 3"
return 1
fi
local _repo_name=$3
if [[ -z ${4:-} ]] ; then
echo "Push type skipped Skip"
local _push_type=""
else
local _push_type=$4
fi
#-w "%{stderr}{\"status\": \"%{http_code}\", \"body\":\"%{stdout}\"}"
reponse_json=$(${curl_POST_nofail_cmd} -sS -w ",{ \"status\": \"%{http_code}\" }" ${_bitbucket_url}/rest/api/1.0/projects/${_bitbucket_project}/repos -d "{ \"name\":\"${_repo_name}\", \"forkable\":true }" )
reponse_code=$(echo "[$reponse_json]" | jq -r .[1].status )
case $reponse_code in
201)
echo "All good - repo created"
;;
409)
echo "Skip - repo already exists"
echo "[$reponse_json]" | jq -r .[0].errors[]
;;
*)
echo "ERROR - something when wrong"
echo "[$reponse_json]" | jq -r .[0].errors[]
exit 1
;;
esac
# Fill repo
if [ -d ${_repo_name} ]; then
cd ${_repo_name}
if [[ "${_push_type}" == "--mirror" ]]; then
git push ${_bitbucket_url}/scm/${_bitbucket_project}/${_repo_name}.git ${_push_type}
else
git push ${_bitbucket_url}/scm/${_bitbucket_project}/${_repo_name}.git master:master
git push ${_bitbucket_url}/scm/${_bitbucket_project}/${_repo_name}.git master:stable || echo "Skipped"
git push ${_bitbucket_url}/scm/${_bitbucket_project}/${_repo_name}.git master:release || echo "Skipped"
git push ${_bitbucket_url}/scm/${_bitbucket_project}/${_repo_name}.git --tags
fi
cd -
fi
}
function repo_move {
if [[ -z $1 ]] ; then
echo "Please set bitbucket_url as parameter 1"
return 1
fi
local _bitbucket_url=$1
if [[ -z $2 ]] ; then
echo "Please set bitbucket project name as parameter 2"
return 1
fi
local _bitbucket_project=$2
if [[ -z $3 ]] ; then
echo "Please set repo name (slug) as parameter 3"
return 1
fi
local _repo_name=$3
if [[ -z $4 ]] ; then
echo "Please set bitbucket project name as parameter 2"
return 1
fi
local _bitbucket_new_project=$4
#-w "%{stderr}{\"status\": \"%{http_code}\", \"body\":\"%{stdout}\"}"
response_json=$(${curl_PUT_nofail_cmd} -H Content-Type:application/json -sS -w ",{ \"status\": \"%{http_code}\" }" ${_bitbucket_url}/rest/api/1.0/projects/${_bitbucket_project}/repos/${_repo_name} -d "{ \"project\": { \"key\" : \"${_bitbucket_new_project}\" }} " )
response_code=$(echo "[$response_json]" | jq -r .[1].status 2> /dev/null ) || {
IFS=, read -r response_string response_status_json <<< "$response_json"
response_code=$(jq -r .status <<< ${response_status_json} )
}
case $response_code in
201)
echo "All good - repo moved: ${_bitbucket_url}/rest/api/1.0/projects/${_bitbucket_project}/repos/${_repo_name} to ${_bitbucket_new_project}"
;;
307)
echo "Skip - repo source ${_bitbucket_url}/rest/api/1.0/projects/${_bitbucket_project}/repos/${_repo_name} already moved"
echo "$response_string"
;;
404)
echo "Skip - repo source ${_bitbucket_url}/rest/api/1.0/projects/${_bitbucket_project}/repos/${_repo_name} does not exist"
echo "[$response_json]" | jq -r .[0].errors[]
;;
409)
echo "Skip - repo already exists in project: ${_bitbucket_new_project}"
echo "[$response_json]" | jq -r .[0].errors[]
;;
*)
echo "ERROR - something when wrong"
echo "[$response_json]" | jq -r .[0].errors[]
exit 1
;;
esac
}
function repo_prereceive_force_push_hook_enable () {
if [[ -z $1 ]] ; then
echo "Please set repo name as parameter 1"
return 1
fi
local _repo_name=${1}
if [[ -z $2 ]] ; then
echo "Please set bitbucket project as parameter 2"
return 1
fi
local _bitbucket_project=${2}
if [[ -z ${bitbucket_url} ]] ; then
echo "Please make sure that bitbucket_url variable before calling this function "
return 1
fi
cmd="${curl_PUT_cmd} ${bitbucket_url}/rest/api/1.0/projects/${_bitbucket_project}/repos/${_repo_name}/settings/hooks/com.atlassian.bitbucket.server.bitbucket-bundled-hooks:force-push-hook/enabled"
printf "excecute: $cmd\n"
$cmd
printf "\n"
}
function repo_prereceive_force_push_hook_disable () {
if [[ -z $1 ]] ; then
echo "Please set repo name as parameter 1"
return 1
fi
local _repo_name=${1}
if [[ -z $2 ]] ; then
echo "Please set bitbucket project as parameter 2"
return 1
fi
local _bitbucket_project=${2}
if [[ -z ${bitbucket_url} ]] ; then
echo "Please make sure that bitbucket_url variable before calling this function "
return 1
fi
cmd="${curl_DELETE_cmd} ${bitbucket_url}/rest/api/1.0/projects/${_bitbucket_project}/repos/${_repo_name}/settings/hooks/com.atlassian.bitbucket.server.bitbucket-bundled-hooks:force-push-hook/enabled"
printf "excecute: $cmd\n"
$cmd
printf "\n"
}
function delete_repo () {
if [[ -z $1 ]] ; then
echo "Please set bitbucket_url as parameter 1"
return 1
fi
if [[ -z $2 ]] ; then
echo "Please set bitbucket project name as parameter 2"
return 1
fi
if [[ -z $3 ]] ; then
echo "Please set repo name as parameter 3"
return 1
fi
if [[ -z $4 ]] ; then
echo "Please set netrc file as parameter 4"
return 1
fi
local _bitbucket_url=$1
local _project=$2
local _repo=$3
local _netrc_file="$4"
curl --fail --insecure --netrc-file ${_netrc_file} -X DELETE ${_bitbucket_url}/rest/api/1.0/projects/${_project}/repos/${_repo}
}
function repo_git_lfs_enable () {
# https://jira.atlassian.com/browse/BSERV-8935
#There's actually an undocumented endpoint for interacting with LFS. It's rough and clearly not polished, but it works.
# rest/git-lfs/admin/projects/<key>/repos/<slug>/enabled
# GET will return a 200 if its enabled, 404 if it's disabled.
# PUT to enable
# DELETE to disable
if [[ -z $1 ]] ; then
echo "Please set bitbucket_url as parameter 1"
return 1
fi
if [[ -z $2 ]] ; then
echo "Please set bitbucket project name as parameter 2"
return 1
fi
if [[ -z $3 ]] ; then
echo "Please set repo name as parameter 3"
return 1
fi
if [[ -z $4 ]] ; then
echo "Please set netrc file as parameter 4"
return 1
fi
local _bitbucket_url=$1
local _project=$2
local _repo=$3
local _netrc_file=$4
curl --fail --insecure --netrc-file ${_netrc_file} -X PUT -H Content-Type:application/json -o - --url ${_bitbucket_url}/rest/git-lfs/admin/projects/${_project}/repos/${_repo}/enabled
}
function repo_branch_create () {
if [[ -z $1 ]] ; then
echo "Please set bitbucket_url as parameter 1"
return 1
fi
if [[ -z $2 ]] ; then
echo "Please set bitbucket project name as parameter 2"
return 2
fi
if [[ -z $3 ]] ; then
echo "Please set repo name as parameter 3"
return 3
fi
if [[ -z $4 ]] ; then
echo "Please set netrc-file as parameter 4"
return 4
fi
if [[ -z $5 ]] ; then
echo "Please set sha1 as parameter 5"
return 5
fi
if [[ -z $6 ]] ; then
echo "Please set branch as parameter 6"
return 6
fi
local _bitbucket_url=$1
local _bitbucket_project=$2
local _repo=$3
local _netrc_file=$4
local _sha1=$5
local _branch=$6
reponse_json=$(${curl_POST_nofail_cmd} -sS -w ",{ \"status\": \"%{http_code}\" }" ${_bitbucket_url}/rest/api/1.0/projects/${_bitbucket_project}/repos/${_repo}/branches \
-d "{ \"name\": \"${_branch}\", \"startPoint\": \"${_sha1}\", \"message\": \"Branch creation $_branch\"} " )
reponse_code=$(echo "[$reponse_json]" | jq -r .[1].status )
case $reponse_code in
200)
echo "All good - branch created: $_branch @ $_repo @ $_bitbucket_project"
;;
409)
echo "Skip - repo already exists: $_branch @ $_repo @ $_bitbucket_project"
echo "[$reponse_json]" | jq -r .[0].errors[]
;;
*)
echo "ERROR - something when wrong"
echo "[$reponse_json]" | jq -r .[0].errors[]
exit 1
;;
esac
#https://docs.atlassian.com/bitbucket-server/rest/7.6.0/bitbucket-rest.html
# /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/branches
#METHODS
#POST
#This API can also be invoked via a user-centric URL when addressing repositories in personal projects.
#
#Creates a branch using the information provided in the {@link RestCreateBranchRequest request}
#
#The authenticated user must have REPO_WRITE permission for the context repository to call this resource.
#
#Example request representations:
#
#application/json [collapse]
#EXAMPLE
#{
# "name": "my-branch",
# "startPoint": "8d351a10fb428c0c1239530256e21cf24f136e73",
# "message": "This is my branch"
#}
#Example response representations:
#
#200 - application/json (branch) [expand]
#401 - application/json (errors) [expand]
#404 - application/json (errors) [expand]
}