Skip to content

Instantly share code, notes, and snippets.

@mcmoe
Created February 29, 2024 23:07
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 mcmoe/0a30bfc28812ca5cdca76df84224ca23 to your computer and use it in GitHub Desktop.
Save mcmoe/0a30bfc28812ca5cdca76df84224ca23 to your computer and use it in GitHub Desktop.
Add users in bulk to a GitHub Organization using the gh cli API
# Below code is based on the gh cli api
# mainly on: https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-a-user
# and on: https://docs.github.com/en/rest/orgs/members?apiVersion=2022-11-28#create-an-organization-invitation
# idea is to get the user int id from the GitHub username, and then user that to invite the user to the org
# this is useful when you have a lot of users to invite - and would like to avoid to do it 1 by 1 from the GUI
# the final function is all you need really, passing it the org and a csv file with the usernames
# note that you will need to have gh installed and you would need to first authenticate gh
# ex: gh auth ligin -s admin:org
# you will also need jq to be installed which pipes the json response to return the id only
function get_github_user_id() {
if [ "$#" -ne 1 ]; then
echo "Usage: get_github_user_id <USERNAME>"
return 1
fi
local USERNAME="$1"
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/users/$USERNAME" | jq -r '.id'
}
# To use this function, you would call it from the command line like this:
# get_github_user_id USERNAME
function invite_github_user() {
if [ "$#" -ne 2 ]; then
echo "Usage: invite_github_user <ORG> <USERID>"
return 1
fi
local ORG="$1"
local USERID="$2"
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/orgs/$ORG/invitations" \
-F invitee_id="$USERID" \
-f role='direct_member'
}
# To use this function, you would call it from the command line like this:
# invite_github_user ORG USERID
function invite_github_user_by_username() {
if [ "$#" -ne 2 ]; then
echo "Usage: invite_github_user_by_username <ORG> <USERNAME>"
return 1
fi
local ORG="$1"
local USERNAME="$2"
local USERID
# Retrieve user ID
USERID=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/users/$USERNAME" | jq -r '.id')
# Check if USERID is not null or not empty
if [ -z "$USERID" ] || [ "$USERID" = "null" ]; then
echo "Failed to retrieve user ID for username: $USERNAME"
return 1
fi
# Invite user by ID
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/orgs/$ORG/invitations" \
-F invitee_id="$USERID" \
-f role='direct_member'
echo "Invitation sent to $USERNAME ($USERID) to join $ORG."
}
# To use this function, you would call it from the command line like this:
# invite_github_user_by_username ORG USERNAME
function invite_github_users_by_usernames() {
if [ "$#" -ne 2 ]; then
echo "Usage: invite_github_users_by_usernames <ORG> <USERNAMES_CSV>"
return 1
fi
local ORG="$1"
local USERNAMES_CSV="$2"
local USERNAME
local USERID
# Convert comma-separated usernames into an array
IFS=',' read -ra USERNAMES <<< "$USERNAMES_CSV"
for USERNAME in "${USERNAMES[@]}"; do
echo "Processing invitation for username: $USERNAME"
# Retrieve user ID
USERID=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/users/$USERNAME" | jq -r '.id')
# Check if USERID is not null or not empty
if [ -z "$USERID" ] || [ "$USERID" = "null" ]; then
echo "Failed to retrieve user ID for username: $USERNAME. Skipping..."
continue
fi
# Invite user by ID
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/orgs/$ORG/invitations" \
-F invitee_id="$USERID" \
-f role='direct_member'
echo "Invitation sent to $USERNAME ($USERID) to join $ORG."
done
}
# To use this function, you would call it from the command line like this:
# invite_github_users_by_usernames ORG "username1,username2,username3"
#================================
# And for the ultimate all in one adding users to an org based on a list of users in a CSV file
#================================
function invite_github_users_by_usernames_from_file() {
if [ "$#" -ne 2 ]; then
echo "Usage: invite_github_users_by_usernames_from_file <ORG> <FILE_PATH>"
return 1
fi
local ORG="$1"
local FILE_PATH="$2"
local USERNAME
local USERID
# Check if the file exists
if [ ! -f "$FILE_PATH" ]; then
echo "File does not exist: $FILE_PATH"
return 1
fi
# Read usernames from file, assuming the file contains a single line with comma-separated usernames
read -r USERNAMES_CSV < "$FILE_PATH"
# Convert comma-separated usernames into an array
IFS=',' read -ra USERNAMES <<< "$USERNAMES_CSV"
for USERNAME in "${USERNAMES[@]}"; do
echo "Processing invitation for username: $USERNAME"
# Retrieve user ID
USERID=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/users/$USERNAME" | jq -r '.id')
# Check if USERID is not null or not empty
if [ -z "$USERID" ] || [ "$USERID" = "null" ]; then
echo "Failed to retrieve user ID for username: $USERNAME. Skipping..."
continue
fi
# Invite user by ID
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/orgs/$ORG/invitations" \
-F invitee_id="$USERID" \
-f role='direct_member'
echo "Invitation sent to $USERNAME ($USERID) to join $ORG."
done
}
# To use this function, you would call it from the command line like this:
# invite_github_users_by_usernames_from_file ORG file_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment