diff --git a/gh-cli/README.md b/gh-cli/README.md index 093232b..c92e852 100644 --- a/gh-cli/README.md +++ b/gh-cli/README.md @@ -1142,6 +1142,10 @@ Example output: ], ``` +### invite-users-to-organization-from-list.sh + +Adds users to an organization team from a CSV input list. + ### lock-repository-with-migration.sh Creates a (mostly) empty migration for a given organization repository so that it can create a lock. diff --git a/gh-cli/invite-users-to-organization-from-list.sh b/gh-cli/invite-users-to-organization-from-list.sh new file mode 100755 index 0000000..65a9219 --- /dev/null +++ b/gh-cli/invite-users-to-organization-from-list.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Adds users to an organization team from a CSV input list + +# Usage: +# Step 1: Create a list of user emails in a csv file, 1 per line, with a trailing empty line at the end of the file +# - DO NOT REMOVE TRAILING NEW LINE IN THE INPUT CSV FILE +# Step 2: ./invite-users-to-organization-from-list.sh users.csv + +if [ $# -lt "2" ]; then + echo "Usage: $0 " + exit 1 +fi + +if [ ! -f "$1" ]; then + echo "File $1 does not exist" + exit 1 +fi + +filename="$1" +org="$2" + +while read -r repofull ; +do + IFS='/' read -ra data <<< "$repofull" + + user=${data[0]} + + echo "Adding user to org: $user" + + response=$(gh api \ + --method POST \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /orgs/$org/invitations \ + -f "email=${user}" -f "role=direct_member") + + echo $response + +done < "$filename"