Skip to content

Commit 46937d6

Browse files
Merge pull request #84 from whsalazar/main
Added add-team-to-repos-from-list script
2 parents 1ee6d9a + 86947b5 commit 46937d6

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

gh-cli/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ Use the [get-enterprise-id.sh](./get-enterprise-id.sh) or [get-organization-id.s
108108

109109
See the [docs](https://docs.github.com/en/graphql/reference/mutations#createipallowlistentry) for further information.
110110

111+
### add-team-to-repositories-from-list.sh
112+
113+
This script adds a specified team to a list of repositories with specified permissions.
114+
111115
### add-team-to-repository.sh
112116

113117
Adds a team to a repository with a given permission level
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
## This script adds a specified team to a list of repositories with specified permissions.
4+
## Make sure your repo_list_file contains entries in the format repository_name,permission, for example:
5+
## repo1,admin
6+
## repo2,push
7+
## repo3,pull
8+
##
9+
## Usage:
10+
## ./add-team-to-repositories-from-list.sh <organization> <team_slug> <repo_list_file>
11+
##
12+
## Arguments:
13+
## organization - The GitHub organization name
14+
## team_slug - The slug of the team to add to the repositories
15+
## repo_list_file - The file containing the list of repositories and permissions
16+
##
17+
## Example:
18+
## ./add-team-to-repositories-from-list.sh my-org my-team repos.csv
19+
##
20+
## Where repos.csv contains:
21+
## repo1,admin
22+
## repo2,push
23+
## repo3,pull
24+
##
25+
## The available permissions for adding a team to repositories using the add-team-to-repositories-from-list.sh script are:
26+
##
27+
## pull - Read-only access to the repository.
28+
## push - Read and write access to the repository.
29+
## admin - Full access to the repository, including the ability to manage settings and users.
30+
## maintain - Full access to the repository, with the ability to manage settings and users, but not delete the repository.
31+
## triage - Read access to the repository, with the ability to manage issues and pull requests.
32+
##
33+
## These permissions correspond to the levels of access that can be granted to a team for a repository on GitHub.
34+
## See https://docs.github.com/en/rest/teams/teams#add-or-update-team-repository-permissions for more information.
35+
##
36+
## Required Permissions:
37+
## - The user running this script must have admin access to the organization.
38+
## - The GitHub CLI (gh) must be authenticated with sufficient permissions to manage teams and repositories.
39+
40+
41+
42+
# Ensure GitHub CLI is authenticated
43+
if ! gh auth status > /dev/null 2>&1; then
44+
echo "Please authenticate GitHub CLI using 'gh auth login'"
45+
exit 1
46+
fi
47+
48+
# Check if the required arguments are provided
49+
if [ "$#" -ne 3 ]; then
50+
echo "Usage: $0 <organization> <team_slug> <repo_list_file>"
51+
exit 1
52+
fi
53+
54+
ORG=$1
55+
TEAM_SLUG=$2
56+
REPO_LIST_FILE=$3
57+
58+
# Check if the repository list file exists
59+
if [ ! -f "$REPO_LIST_FILE" ]; then
60+
echo "Repository list file not found: $REPO_LIST_FILE"
61+
exit 1
62+
fi
63+
64+
# Read the repository list file and add the team to each repository with the specified permission
65+
while IFS=, read -r REPO PERMISSION; do
66+
if [ -n "$REPO" ] && [ -n "$PERMISSION" ]; then
67+
echo "Adding team '$TEAM_SLUG' to repository '$REPO' in organization '$ORG' with permission '$PERMISSION'..."
68+
gh api -X PUT "/orgs/$ORG/teams/$TEAM_SLUG/repos/$ORG/$REPO" -f permission="$PERMISSION"
69+
if [ $? -eq 0 ]; then
70+
echo "Successfully added team '$TEAM_SLUG' to repository '$REPO' with permission '$PERMISSION'"
71+
else
72+
echo "Failed to add team '$TEAM_SLUG' to repository '$REPO' with permission '$PERMISSION'"
73+
fi
74+
fi
75+
done < "$REPO_LIST_FILE"

0 commit comments

Comments
 (0)