|
| 1 | +#!/usr/bin/env /bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +### GLOBAL VARIABLES |
| 5 | +GITHUB_TOKEN=${GITHUB_TOKEN:-''} |
| 6 | +ORG=${ORG:-''} |
| 7 | +API_URL_PREFIX=${API_URL_PREFIX:-'https://api.github.com'} |
| 8 | +GIT_URL_PREFIX=${GIT_URL_PREFIX:-'https://github.com'} |
| 9 | + |
| 10 | +# Permission-specific team variables (space-separated team slugs) |
| 11 | +REPO_ADMIN=${REPO_ADMIN:-''} |
| 12 | +REPO_MAINTAIN=${REPO_MAINTAIN:-''} |
| 13 | +REPO_PUSH=${REPO_PUSH:-''} |
| 14 | +REPO_TRIAGE=${REPO_TRIAGE:-''} |
| 15 | +REPO_PULL=${REPO_PULL:-''} |
| 16 | + |
| 17 | +# Check if GITHUB_TOKEN is set |
| 18 | +if [ -z "${GITHUB_TOKEN}" ]; then |
| 19 | + echo "GITHUB_TOKEN is empty. Please set your token and try again" |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Check if ORG is set |
| 24 | +if [ -z "${ORG}" ]; then |
| 25 | + echo "ORG is empty. Please set your organization and try again" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +# Check if at least one permission level is set |
| 30 | +if [ -z "${REPO_ADMIN}" ] && [ -z "${REPO_MAINTAIN}" ] && [ -z "${REPO_PUSH}" ] && [ -z "${REPO_TRIAGE}" ] && [ -z "${REPO_PULL}" ]; then |
| 31 | + echo "Error: At least one permission level must be set." |
| 32 | + echo "Available variables: REPO_ADMIN, REPO_MAINTAIN, REPO_PUSH, REPO_TRIAGE, REPO_PULL" |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +# Validate GITHUB_TOKEN by calling GitHub API |
| 37 | +RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user") |
| 38 | + |
| 39 | +if [ "${RESPONSE}" -ne 200 ]; then |
| 40 | + echo "Error: GITHUB_TOKEN is invalid or does not have required permissions." |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +get_repo_pagination () { |
| 45 | + 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;) |
| 46 | + echo "${repo_pages:-1}" |
| 47 | +} |
| 48 | + |
| 49 | +limit_repo_pagination () { |
| 50 | + seq "$(get_repo_pagination)" |
| 51 | +} |
| 52 | + |
| 53 | +apply_team_permissions () { |
| 54 | + local REPO_NAME=$1 |
| 55 | + local PERMISSION=$2 |
| 56 | + local TEAM_SLUGS=$3 |
| 57 | + |
| 58 | + # Loop through space-separated team slugs |
| 59 | + for TEAM in ${TEAM_SLUGS}; do |
| 60 | + echo " Granting ${PERMISSION} permission to team ${TEAM}" |
| 61 | + curl -s -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/orgs/${ORG}/teams/${TEAM}/repos/${ORG}/${REPO_NAME}" -d "{\"permission\":\"${PERMISSION}\"}" |
| 62 | + done |
| 63 | +} |
| 64 | + |
| 65 | +process_repos () { |
| 66 | + for PAGE in $(limit_repo_pagination); do |
| 67 | + for REPO in $(curl -s -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/orgs/${ORG}/repos?page=${PAGE}&per_page=100&sort=full_name" | jq -r 'sort_by(.name) | .[] | .name'); do |
| 68 | + echo "Processing repo ${REPO}" |
| 69 | + |
| 70 | + # Apply admin permissions |
| 71 | + if [ -n "${REPO_ADMIN}" ]; then |
| 72 | + apply_team_permissions "${REPO}" "admin" "${REPO_ADMIN}" |
| 73 | + fi |
| 74 | + |
| 75 | + # Apply maintain permissions |
| 76 | + if [ -n "${REPO_MAINTAIN}" ]; then |
| 77 | + apply_team_permissions "${REPO}" "maintain" "${REPO_MAINTAIN}" |
| 78 | + fi |
| 79 | + |
| 80 | + # Apply push (write) permissions |
| 81 | + if [ -n "${REPO_PUSH}" ]; then |
| 82 | + apply_team_permissions "${REPO}" "push" "${REPO_PUSH}" |
| 83 | + fi |
| 84 | + |
| 85 | + # Apply triage permissions |
| 86 | + if [ -n "${REPO_TRIAGE}" ]; then |
| 87 | + apply_team_permissions "${REPO}" "triage" "${REPO_TRIAGE}" |
| 88 | + fi |
| 89 | + |
| 90 | + # Apply pull (read) permissions |
| 91 | + if [ -n "${REPO_PULL}" ]; then |
| 92 | + apply_team_permissions "${REPO}" "pull" "${REPO_PULL}" |
| 93 | + fi |
| 94 | + |
| 95 | + # Add delay to prevent hitting GitHub rate limit |
| 96 | + sleep 5 |
| 97 | + done |
| 98 | + done |
| 99 | +} |
| 100 | + |
| 101 | +process_repos |
0 commit comments