Skip to content

Commit

Permalink
Merge pull request #88 from joshjohanning/add-all-organization-member…
Browse files Browse the repository at this point in the history
…s-to-a-team.sh

feat: add script to add all organization members to a team
  • Loading branch information
joshjohanning authored Nov 15, 2024
2 parents 11a7e45 + 1db85c5 commit c10262a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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

0 comments on commit c10262a

Please sign in to comment.