Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gh-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions gh-cli/add-all-organization-members-to-a-team.sh
Original file line number Diff line number Diff line change
@@ -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 <org> <team>"
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