Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 58fdd8e1 authored by Christophe Chaudier's avatar Christophe Chaudier :rocket:
Browse files

Merge branch '32-add-archived-filter' into 'master'

Resolve "add archived filter"

Closes #32

See merge request !26
parents e87d931f 3e03cd4e
No related branches found
No related tags found
1 merge request!26Resolve "add archived filter"
Pipeline #2938 passed with warnings
...@@ -37,7 +37,8 @@ Options : ...@@ -37,7 +37,8 @@ Options :
With export command : With export command :
-p path/to/projet : export the projet -p path/to/projet : export the projet
-g path/to/group : export the group and is subgroups and projects -g path/to/group : export the group and is subgroups and projects
-a : export all my group and subgroup -a : export all my group, subgroup and projects
-u : export only unarchived groups or projects
With import command : With import command :
-p path/to/projet/file : import the projet -p path/to/projet/file : import the projet
...@@ -56,7 +57,9 @@ config_file='.mygb_config' ...@@ -56,7 +57,9 @@ config_file='.mygb_config'
export_all=false export_all=false
dry_run=false dry_run=false
insecure=false insecure=false
export_archived=true
mygb_date="$(date +"%Y%m%d_%H%M")" mygb_date="$(date +"%Y%m%d_%H%M")"
archived_filter=""
# ---[ Functions ]--- # ---[ Functions ]---
_exit() { _exit() {
...@@ -106,6 +109,10 @@ _load_config() { ...@@ -106,6 +109,10 @@ _load_config() {
mygb_file="${backup_dir}/mygb_${mygb_date}.tgz" mygb_file="${backup_dir}/mygb_${mygb_date}.tgz"
mkdir -p "${mygb_tmp_dir}" mkdir -p "${mygb_tmp_dir}"
if [[ ${export_archived} == false ]]; then
archived_filter="&archived=false"
fi
_info _info
} }
...@@ -115,6 +122,7 @@ GitLab URL [${gitlab_url}] ...@@ -115,6 +122,7 @@ GitLab URL [${gitlab_url}]
+ Mode export_all [${export_all}] + Mode export_all [${export_all}]
+ Mode dry_run [${dry_run}] + Mode dry_run [${dry_run}]
+ Mode insecure [${insecure}] + Mode insecure [${insecure}]
+ Mode export_archived [${export_archived}]
" "
} }
...@@ -158,15 +166,14 @@ _api() { ...@@ -158,15 +166,14 @@ _api() {
"${gitlab_url}api/v4/${path}" "${gitlab_url}api/v4/${path}"
} }
_nb_of_projects_page() { _nb_of_projects_from_group_page() {
_api GET "groups/${group_url_encoded}/projects?include_subgroups=true&per_page=${API_PER_PAGE}" --head \ _api GET "groups/${group_url_encoded}/projects?include_subgroups=true&per_page=${API_PER_PAGE}" --head \
| grep 'x-total-pages'| cut -d ':' -f 2 | tr -d "[:space:]" | grep 'x-total-pages'| cut -d ':' -f 2 | tr -d "[:space:]"
} }
_get_projects_from_group() { _get_projects_from_group() {
for (( page=1; page<=$(_nb_of_projects_from_group_page); page++ )); do
for (( page=1; page<=$(_nb_of_projects_page); page++ )); do _api GET "groups/${group_url_encoded}/projects?include_subgroups=true&per_page=${API_PER_PAGE}&page=${page}${archived_filter}" \
_api GET "groups/${group_url_encoded}/projects?include_subgroups=true&per_page=${API_PER_PAGE}&page=${page}" \
| jq -r '.[].path_with_namespace' | jq -r '.[].path_with_namespace'
done done
} }
...@@ -185,11 +192,11 @@ _get_export_filename() { ...@@ -185,11 +192,11 @@ _get_export_filename() {
} }
_get_curent_user_id() { _get_curent_user_id() {
_api GET "/user" | jq -r '.id' _api GET "user" | jq -r '.id'
} }
_get_curent_user_email() { _get_curent_user_email() {
_api GET "/user" | jq -r '.email' _api GET "user" | jq -r '.email'
} }
_dry_run(){ _dry_run(){
...@@ -286,21 +293,41 @@ _nb_of_groups_page() { ...@@ -286,21 +293,41 @@ _nb_of_groups_page() {
} }
_get_my_groups() { _get_my_groups() {
total_pages=$(_nb_of_groups_page) total_groups_pages=$(_nb_of_groups_page)
for (( page=1; page<=total_pages; page++ )); do for (( page=1; page<=total_groups_pages; page++ )); do
_api GET "groups?owned=true&top_level_only=true&per_page=${API_PER_PAGE}&page=${page}" | jq -r '.[].path' _api GET "groups?owned=true&top_level_only=true&per_page=${API_PER_PAGE}&page=${page}" | jq -r '.[].path'
done done
} }
_nb_of_projects_page() {
_api GET "users/${curent_user_id}/projects?owned=true&per_page=${API_PER_PAGE}${archived_filter}" --head \
| grep 'x-total-pages'| cut -d ':' -f 2 | tr -d "[:space:]"
}
_get_my_projects() {
total_projects_pages=$(_nb_of_projects_page)
for (( page=1; page<=total_projects_pages; page++ )); do
_api GET "users/${curent_user_id}/projects?owned=true&per_page=${API_PER_PAGE}&page=${page}${archived_filter}" | jq -r '.[].path'
done
}
_export_all(){ _export_all(){
if [[ ${export_all} == true ]]; then if [[ ${export_all} == true ]]; then
echo "Export all my groups" echo "Export all my groups and projects"
local nb_group_exported=0 local nb_groups_exported=0
for group in $(_get_my_groups); do for group in $(_get_my_groups); do
_export_group _export_group
((nb_group_exported++)) ((nb_groups_exported++))
done done
echo "Number of group exported : ${nb_group_exported}"
curent_user_id="$(_get_curent_user_id)"
for project in $(_get_my_projects); do
_export_project
((nb_projects_exported++))
done
echo "Number of groups exported : ${nb_groups_exported}"
echo "Number of projects exported : ${nb_projects_exported}"
fi fi
} }
...@@ -586,7 +613,7 @@ _check_user_public_email() { ...@@ -586,7 +613,7 @@ _check_user_public_email() {
local public_email_to_check=${2} local public_email_to_check=${2}
echo "Check if username [${username_to_check}] as public_email [${public_email_to_check}]" echo "Check if username [${username_to_check}] as public_email [${public_email_to_check}]"
_api GET "/users?username=${username_to_check}" | jq -r '.[].public_email' \ _api GET "users?username=${username_to_check}" | jq -r '.[].public_email' \
| grep --silent "${public_email_to_check}" | grep --silent "${public_email_to_check}"
} }
...@@ -594,7 +621,7 @@ _check_user_exist() { ...@@ -594,7 +621,7 @@ _check_user_exist() {
local username_to_check=${1} local username_to_check=${1}
echo "Check if username [${username_to_check}] exist" echo "Check if username [${username_to_check}] exist"
test "$(_api GET "/users?username=${username_to_check}" | jq -r '.[].id')" test "$(_api GET "users?username=${username_to_check}" | jq -r '.[].id')"
} }
# ---[ Main ]--- # ---[ Main ]---
...@@ -605,7 +632,7 @@ main() { ...@@ -605,7 +632,7 @@ main() {
action=${1} action=${1}
shift shift
while getopts c:p:g:f:adkh flag; do while getopts c:p:g:f:adkhu flag; do
# shellcheck disable=SC2034 # shellcheck disable=SC2034
case "${flag}" in case "${flag}" in
c) config_file=${OPTARG} ;; c) config_file=${OPTARG} ;;
...@@ -616,6 +643,7 @@ main() { ...@@ -616,6 +643,7 @@ main() {
d) dry_run=true ;; d) dry_run=true ;;
k) insecure=true ;; k) insecure=true ;;
h) action=help ;; h) action=help ;;
u) export_archived=false ;;
*) action="" ;; *) action="" ;;
esac esac
done done
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment