Created
January 15, 2025 00:04
-
-
Save WoozyMasta/511a9ef32973585324dbbfea71fe2813 to your computer and use it in GitHub Desktop.
Recursive change GitLab projects feature visibility settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
# Recursive change GitLab projects feature visibility settings | |
: "${GITLAB_HOST:?}" | |
: "${GITLAB_TOKEN:?}" | |
: "${GITLAB_GROUP_ID:?}" | |
gitlab() { | |
curl -sSfH "PRIVATE-TOKEN: $GITLAB_TOKEN" \ | |
--url "https://$GITLAB_HOST/api/v4/$1" "${@:2}" | |
} | |
gitlab::paginate() { | |
local page=1 per_page=100 response on_page | |
while :; do | |
response="$( | |
gitlab "$1?page=$page&per_page=$per_page" "${@:2}"| jq -cer | |
)" | |
on_page="$(jq -er 'length' <<<"$response")" | |
[ "$on_page" -eq 0 ] && break | |
echo "$response" | |
[ "$on_page" -lt "$per_page" ] && break | |
((page++)) || : | |
done | |
} | |
disable_features() { | |
local project_id="$1" | |
printf '%-10s' Update: | |
# Project feature visibility settings with access control options can be one of: | |
# - disabled: Disable the feature. | |
# - private: Enable and set the feature to Only project members. | |
# - enabled: Enable and set the feature to Everyone with access. | |
gitlab "projects/$project_id" -X PUT \ | |
--data analytics_access_level=disabled \ | |
--data builds_access_level=disabled \ | |
--data container_registry_access_level=disabled \ | |
--data environments_access_level=disabled \ | |
--data feature_flags_access_level=disabled \ | |
--data forking_access_level=private \ | |
--data infrastructure_access_level=disabled \ | |
--data issues_access_level=private \ | |
--data merge_requests_access_level=private \ | |
--data model_experiments_access_level=disabled \ | |
--data model_registry_access_level=disabled \ | |
--data monitor_access_level=disabled \ | |
--data pages_access_level=disabled \ | |
--data releases_access_level=private \ | |
--data repository_access_level=private \ | |
--data requirements_access_level=disabled \ | |
--data security_and_compliance_access_level=disabled \ | |
--data snippets_access_level=disabled \ | |
--data wiki_access_level=disabled \ | |
--data auto_devops_enabled=false \ | |
--data service_desk_enabled=false | jq -er '.path_with_namespace' | |
} | |
process_group() { | |
local group_id="$1" | |
# Disabling features for all projects in this group | |
while IFS=$'\t' read -r project_id _; do | |
disable_features "$project_id" || echo "Fail $project_id" | |
done < <( | |
gitlab::paginate "groups/$group_id/projects" \ | |
| jq -er '.[] | [.id, .path_with_namespace] | @tsv' | |
) | |
# Recursion into subgroups | |
while IFS=$'\t' read -r subgroup_id subgroup_name; do | |
printf '%-10s%s\n' Group: "$subgroup_name" | |
process_group "$subgroup_id" | |
done < <( | |
gitlab::paginate "groups/$group_id/subgroups" \ | |
| jq -er '.[] | [.id, .full_path] | @tsv' | |
) | |
} | |
process_group "$GITLAB_GROUP_ID" | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment