Skip to content

Commit c10262a

Browse files
Merge pull request #88 from joshjohanning/add-all-organization-members-to-a-team.sh
feat: add script to add all organization members to a team
2 parents 11a7e45 + 1db85c5 commit c10262a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

gh-cli/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ See the [docs](https://cli.github.com/manual/gh_auth_login) for further informat
6565
6666
## Scripts
6767
68+
### add-all-organization-members-to-a-team.sh
69+
70+
Adds all members of an organization to a team.
71+
6872
### add-branch-protection-status-checks.sh
6973
7074
Adds a status check to the branch protection status check contexts.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
# gh cli's token needs to be able to admin org - run this first if it can't
4+
# gh auth refresh -h github.com -s admin:org
5+
6+
# this script is currently cumulative-only; it won't remove any users from the team
7+
# (but this shouldn't matter, if someone gets pulled from org they won't be in team anymore anyway)
8+
9+
if [ -z "$1" ]; then
10+
echo "Usage: $0 <org> <team>"
11+
echo "Example: ./add-all-organization-members-to-a-team.sh joshjohanning-org all-users"
12+
exit 1
13+
fi
14+
15+
org="$1"
16+
team="$2"
17+
18+
# Define color codes
19+
RED='\033[0;31m'
20+
NC='\033[0m' # No Color
21+
22+
members=$(gh api /orgs/$org/members --jq '.[].login' --paginate)
23+
24+
# loop thru each member and gracefully try to add them to a team
25+
for member in $members; do
26+
echo "Adding $member to $team"
27+
if ! gh api -X PUT /orgs/$org/teams/$team/memberships/$member -f "role=member"; then
28+
echo -e "${RED}Failed to add $member to $team${NC}"
29+
fi
30+
done

0 commit comments

Comments
 (0)