Skip to content

Instantly share code, notes, and snippets.

@pierrotsmnrd
Created February 7, 2025 15:31
Show Gist options
  • Save pierrotsmnrd/433c6a60ff6bbe7e9e7438b141259fa2 to your computer and use it in GitHub Desktop.
Save pierrotsmnrd/433c6a60ff6bbe7e9e7438b141259fa2 to your computer and use it in GitHub Desktop.
Add a maintainer to all teams of a GH org
#!/bin/bash
# Usage: ./add_maintainer_to_org_teams.sh [ORG NAME] [GITHUB HANDLE]
# Example: ./add_maintainer_to_org_teams.sh prusa3D pierrotsmnrd
# Ensure the script exits on error
set -e
# Check if DuckDB and gh CLI are installed
if ! command -v duckdb &>/dev/null; then
echo "Error: DuckDB is not installed. Install it first." >&2
exit 1
fi
if ! command -v gh &>/dev/null; then
echo "Error: GitHub CLI (gh) is not installed. Install it first." >&2
exit 1
fi
# Ensure correct usage
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <organization> <github_username>"
exit 1
fi
ORG_NAME="$1"
GITHUB_USER="$2"
# Temporary file for team slugs
TEMP_FILE=$(mktemp)
# Get the teams from GH
gh api --paginate "orgs/$ORG_NAME/teams" > teams_$ORG_NAME.json
# Extract team slugs using DuckDB
duckdb -c "COPY (SELECT slug FROM read_json_auto('teams_$ORG_NAME.json')) TO '$TEMP_FILE' (FORMAT 'csv', HEADER false)"
# Loop through each slug and add the user as a maintainer
while IFS= read -r TEAM_SLUG; do
echo "Adding $GITHUB_USER as a maintainer to team: $TEAM_SLUG"
gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
"/orgs/$ORG_NAME/teams/$TEAM_SLUG/memberships/$GITHUB_USER" \
-f role="maintainer"
done < "$TEMP_FILE"
# Clean up temporary file
rm -f "$TEMP_FILE"
echo "✅ User $GITHUB_USER has been added as a maintainer to all teams of $ORG_NAME."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment