Skip to content

Instantly share code, notes, and snippets.

@decagondev
Last active January 20, 2026 23:06
Show Gist options
  • Select an option

  • Save decagondev/41ac719b9445296755cc9de271a28377 to your computer and use it in GitHub Desktop.

Select an option

Save decagondev/41ac719b9445296755cc9de271a28377 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <ORG_NAME> <GITHUB_TOKEN> <MEMBERS_FILE>"
exit 1
fi
ORG_NAME="$1"
GITHUB_TOKEN="$2"
MEMBERS_FILE="$3"
if [ ! -f "$MEMBERS_FILE" ]; then
echo "Error: Members file '$MEMBERS_FILE' not found."
exit 1
fi
while IFS= read -r username; do
if [ -z "$username" ]; then
continue
fi
echo "Resolving user ID for $username..."
user_response=$(curl -s -w "%{http_code}" -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/users/$username")
http_code="${user_response: -3}"
body="${user_response:0:${#user_response}-3}"
if [ "$http_code" -ne 200 ]; then
echo "Error: Failed to fetch user ID for $username (HTTP $http_code)."
echo "Response: $body"
continue
fi
user_id=$(echo "$body" | jq -r '.id')
if [ -z "$user_id" ] || [ "$user_id" = "null" ]; then
echo "Error: Could not extract user ID for $username."
continue
fi
echo "Inviting $username (ID: $user_id) to $ORG_NAME..."
invite_response=$(curl -s -w "%{http_code}" -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-d "{\"invitee_id\":$user_id}" \
"https://api.github.com/orgs/$ORG_NAME/invitations")
invite_http_code="${invite_response: -3}"
invite_body="${invite_response:0:${#invite_response}-3}"
if [ "$invite_http_code" -eq 201 ]; then
echo "Success: $username invited."
else
echo "Error: Failed to invite $username (HTTP $invite_http_code)."
echo "Response: $invite_body"
fi
sleep 2
done < "$MEMBERS_FILE"
echo "Invitation process complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment