Skip to content

Commit 761a84b

Browse files
committed
feat: add repository name filtering option for permission management script
1 parent 032685f commit 761a84b

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ export REPO_MAINTAIN="maintainers" # Maintain permissions
139139
export REPO_PUSH="developers contributors" # Write/push permissions
140140
export REPO_TRIAGE="support-team" # Triage permissions
141141
export REPO_PULL="external-auditors" # Read/pull permissions
142+
143+
# Optional: restrict to repos whose names start with a given prefix
144+
export REPO_NAME_FILTER="my-service-"
142145
```
143146

144147
**Usage:**
@@ -149,6 +152,7 @@ cd org-admin/github-add-repo-permissions
149152

150153
**What it does:**
151154
- Retrieves all repositories in the organization (paginated)
155+
- Filters to repositories whose names start with `REPO_NAME_FILTER` (all repos when unset)
152156
- Grants permissions to specified teams based on permission level
153157
- Supports multiple teams per permission level (space-separated)
154158
- Processes all five GitHub permission levels: admin, maintain, push, triage, pull

‎org-admin/github-add-repo-permissions/github-add-repo-permissions.sh‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
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:
@@ -13,14 +15,15 @@
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:-''}
4144
ORG=${ORG:-''}
4245
API_URL_PREFIX=${API_URL_PREFIX:-'https://api.github.com'}
4346
GIT_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)
4650
REPO_ADMIN=${REPO_ADMIN:-''}
@@ -63,6 +67,9 @@ fi
6367
validate_github_token
6468

6569
print_status "Organization: ${ORG}"
70+
if [ -n "${REPO_NAME_FILTER}" ]; then
71+
print_status "Repository filter: ${REPO_NAME_FILTER}*"
72+
fi
6673

6774
get_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

Comments
 (0)