22# =============================================================================
33# github-add-repo-permissions.sh
44#
5- # Grants team-level permissions across all repositories in a GitHub
6- # organisation. Supports all five permission levels: admin, maintain, push,
5+ # Grants team-level permissions across repositories in a GitHub organisation.
6+ # By default all repositories are processed; set REPO_NAME_FILTER to restrict
7+ # to repos whose names start with a given prefix. Supports all five permission
8+ # levels: admin, maintain, push,
79# triage, and pull. At least one permission level must be specified.
810#
911# Usage:
1315# ./github-add-repo-permissions.sh
1416#
1517# Environment variables:
16- # GITHUB_TOKEN Required. PAT with admin:org scope
17- # ORG Required. GitHub organization name
18- # REPO_ADMIN Optional. Space-separated team slugs to grant admin access
19- # REPO_MAINTAIN Optional. Space-separated team slugs to grant maintain access
20- # REPO_PUSH Optional. Space-separated team slugs to grant push access
21- # REPO_TRIAGE Optional. Space-separated team slugs to grant triage access
22- # REPO_PULL Optional. Space-separated team slugs to grant pull access
23- # API_URL_PREFIX Optional. GitHub API base URL (default: https://api.github.com)
18+ # GITHUB_TOKEN Required. PAT with admin:org scope
19+ # ORG Required. GitHub organization name
20+ # REPO_NAME_FILTER Optional. Prefix filter for repository names (default: all repos)
21+ # REPO_ADMIN Optional. Space-separated team slugs to grant admin access
22+ # REPO_MAINTAIN Optional. Space-separated team slugs to grant maintain access
23+ # REPO_PUSH Optional. Space-separated team slugs to grant push access
24+ # REPO_TRIAGE Optional. Space-separated team slugs to grant triage access
25+ # REPO_PULL Optional. Space-separated team slugs to grant pull access
26+ # API_URL_PREFIX Optional. GitHub API base URL (default: https://api.github.com)
2427#
2528# Note: At least one of REPO_ADMIN, REPO_MAINTAIN, REPO_PUSH, REPO_TRIAGE,
2629# or REPO_PULL must be set.
@@ -41,6 +44,7 @@ GITHUB_TOKEN=${GITHUB_TOKEN:-''}
4144ORG=${ORG:- ' ' }
4245API_URL_PREFIX=${API_URL_PREFIX:- ' https://api.github.com' }
4346GIT_URL_PREFIX=${GIT_URL_PREFIX:- ' https://github.com' }
47+ REPO_NAME_FILTER=${REPO_NAME_FILTER:- ' ' }
4448
4549# Permission-specific team variables (space-separated team slugs)
4650REPO_ADMIN=${REPO_ADMIN:- ' ' }
6367validate_github_token
6468
6569print_status " Organization: ${ORG} "
70+ if [ -n " ${REPO_NAME_FILTER} " ]; then
71+ print_status " Repository filter: ${REPO_NAME_FILTER} *"
72+ fi
6673
6774get_repo_pagination () {
6875 repo_pages=$( curl -s -H " Authorization: token ${GITHUB_TOKEN} " -I " ${API_URL_PREFIX} /orgs/${ORG} /repos?per_page=100" | grep -Eo ' &page=[0-9]+' | grep -Eo ' [0-9]+' | tail -1; )
@@ -140,7 +147,7 @@ process_repos () {
140147
141148 # Add delay to prevent hitting GitHub rate limit
142149 sleep 5
143- done < <( echo " ${repos_json} " | jq -r ' sort_by(.name) | .[] | .name' )
150+ done < <( echo " ${repos_json} " | jq -r --arg filter " ${REPO_NAME_FILTER} " ' sort_by(.name) | .[] | select(.name | startswith($filter)) | .name' )
144151 done
145152}
146153
0 commit comments