Skip to content

Instantly share code, notes, and snippets.

@ryanj
Last active August 14, 2023 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanj/47da864d6f892bbb86deb918232d1d14 to your computer and use it in GitHub Desktop.
Save ryanj/47da864d6f892bbb86deb918232d1d14 to your computer and use it in GitHub Desktop.
./invite-users.sh
#!/bin/bash
# Invite these users:
export FILE_CSV="BackstageOnboarding\ -\ Invitations.csv"
# To this GH_ORG:
export GH_ORG=summit23Janus2
# Using this API_TOKEN:
export YOUR_TOKEN="YOUR_TOKEN_XYZ"
for ghuser in $(cat $FILE_CSV | cut -f 1 -d ',' | grep -v "^login" ); do
echo "Adding: ${ghuser}"
# Look up user id via username
USER_ID=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $YOUR_TOKEN"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/users/$ghuser | grep '"id":' | sed 's/[^:]*: \([^,]*\),/\1/')
# Invite USER_ID to GH_ORG
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $YOUR_TOKEN"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/orgs/$GH_ORG/invitations \
-d "{\"invitee_id\":${USER_ID},\"role\":\"direct_member\"}"
## rate limit encountered! is the following delay sufficient?
sleep 3
done
# For more usage info see the GH API docs
#https://docs.github.com/en/rest/orgs/members?apiVersion=2022-11-28#create-an-organization-invitation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment