Skip to content

Instantly share code, notes, and snippets.

@malantin
Created February 18, 2025 11:59
Show Gist options
  • Save malantin/71aa880e719bdaab5db92eee140250e7 to your computer and use it in GitHub Desktop.
Save malantin/71aa880e719bdaab5db92eee140250e7 to your computer and use it in GitHub Desktop.
A quick and dirty example of a bash script that reads GitHub Enterprise teams and adds the team members to a cost center
#!/bin/bash
# Configuration
#GITHUB_TOKEN="YOUR_PAT_TOKEN" # Personal access token with appropriate permissions
#ENTERPRISE="YOUR_ENTERPRISE" # Your enterprise slug/name
API_VERSION="2022-11-28" # GitHub API version
TEAMS_FILE="teams.txt" # File containing team to cost center mapping in the following format: team_slug,cost_center_id
# Check if teams file exists
if [ ! -f "$TEAMS_FILE" ]; then
echo "Error: $TEAMS_FILE not found!"
exit 1
fi
# Check if required environment variables are set
if [ -z "$GITHUB_TOKEN" ]; then
echo "Error: GitHub token not set!"
exit 1
fi
if [ -z "$ENTERPRISE" ]; then
echo "Error: Enterprise name not set!"
exit 1
fi
# Function to get all enterprise teams
get_enterprise_teams() {
echo "Getting teams for enterprise: $ENTERPRISE"
local page=1
local teams=""
local response=""
while true; do
response=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/enterprises/$ENTERPRISE/teams?per_page=100&page=$page")
# Check if response is empty array
if [ "$(echo "$response" | jq '. | length')" -eq 0 ]; then
break
fi
# Append new teams to existing teams
if [ -z "$teams" ]; then
teams="$response"
else
teams=$(echo "$teams" "$response" | jq -s 'add')
fi
((page++))
done
# Output all teams in the required format
echo "$teams" | jq -r '.[] | "\(.slug),\(.id)"'
}
# Function to get team members
get_team_members() {
local team_slug=$1
curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/enterprises/$ENTERPRISE/teams/$team_slug/memberships" | \
jq -r '.[] | .login'
}
# Function to add users to cost center
add_to_cost_center() {
local cost_center_id=$1
local users=$2
# Convert newline-separated users to JSON array
local users_json=$(echo "$users" | jq -R . | jq -s .)
curl -s -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "Content-Type: application/json" \
"https://api.github.com/enterprises/$ENTERPRISE/settings/billing/cost-centers/$cost_center_id/resource" \
-d "{\"users\": $users_json}"
}
# Main process
echo "Starting cost center management process..."
declare team_slugs=()
declare cost_center_ids=()
index=0
while IFS=',' read -r team_slug cost_center_id || [ -n "$team_slug" ]; do
# Trim any whitespace
team_slug=$(echo "$team_slug" | tr -d '[:space:]')
cost_center_id=$(echo "$cost_center_id" | tr -d '[:space:]')
# Skip empty lines
if [ -n "$team_slug" ]; then
team_slugs[$index]="$team_slug"
cost_center_ids[$index]="$cost_center_id"
((index++))
fi
done < "$TEAMS_FILE"
echo "Read teams file, found ${#team_slugs[@]} team mappings"
# Process each enterprise team
get_enterprise_teams | while IFS=',' read -r team_slug team_id; do
echo "Processing team: $team_slug"
# Check if team has a cost center mapping
cost_center_id=""
for ((i=0; i<${#team_slugs[@]}; i++)); do
if [ "${team_slugs[$i]}" = "$team_slug" ]; then
cost_center_id="${cost_center_ids[$i]}"
break
fi
done
if [ -n "$cost_center_id" ]; then
echo "Found cost center mapping: $cost_center_id"
# Get team members and add them to the cost center
members=$(get_team_members "$team_slug")
# Print members
echo "Team has the following members: $members"
if [ -n "$members" ]; then
echo "Adding members to cost center $cost_center_id ..."
add_to_cost_center "$cost_center_id" "$members"
echo "Members added successfully"
else
echo "No members found in team"
fi
else
echo "No cost center mapping found for team: $team_slug"
fi
done
echo "Cost center management process completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment