diff --git a/gh-cli/README.md b/gh-cli/README.md index 59aa84e..7d10307 100644 --- a/gh-cli/README.md +++ b/gh-cli/README.md @@ -65,6 +65,10 @@ See the [docs](https://cli.github.com/manual/gh_auth_login) for further informat ## Scripts +### add-all-organization-members-to-a-team.sh + +Adds all members of an organization to a team. + ### add-branch-protection-status-checks.sh Adds a status check to the branch protection status check contexts. diff --git a/gh-cli/add-all-organization-members-to-a-team.sh b/gh-cli/add-all-organization-members-to-a-team.sh new file mode 100755 index 0000000..faa2ec0 --- /dev/null +++ b/gh-cli/add-all-organization-members-to-a-team.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# gh cli's token needs to be able to admin org - run this first if it can't +# gh auth refresh -h github.com -s admin:org + +# this script is currently cumulative-only; it won't remove any users from the team +# (but this shouldn't matter, if someone gets pulled from org they won't be in team anymore anyway) + +if [ -z "$1" ]; then + echo "Usage: $0 " + echo "Example: ./add-all-organization-members-to-a-team.sh joshjohanning-org all-users" + exit 1 +fi + +org="$1" +team="$2" + +# Define color codes +RED='\033[0;31m' +NC='\033[0m' # No Color + +members=$(gh api /orgs/$org/members --jq '.[].login' --paginate) + +# loop thru each member and gracefully try to add them to a team +for member in $members; do + echo "Adding $member to $team" + if ! gh api -X PUT /orgs/$org/teams/$team/memberships/$member -f "role=member"; then + echo -e "${RED}Failed to add $member to $team${NC}" + fi +done