-
Notifications
You must be signed in to change notification settings - Fork 0
/
provide_repo_access_for_teams.sh
54 lines (41 loc) · 1.76 KB
/
provide_repo_access_for_teams.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /bin/bash
# https://docs.github.com/en/rest/teams/teams?apiVersion=2022-11-28#add-or-update-team-repository-permissions
ORGANIZATION=$1
TEAM_NAME=$2
REPO_NAMES=$3
PERMISSION=$4
PER_PAGE=100
# List all teams in the organization
list_all_team_details=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/orgs/$ORGANIZATION/teams?per_page=$PER_PAGE)
echo $list_all_team_details
# Use jq to extract the slug based on the team name
TEAM_SLUG=$(echo "$list_all_team_details" | jq -r ".[] | select(.name == \"$TEAM_NAME\") | .slug")
echo "Team name: $TEAM_NAME | Team slug name is: $TEAM_SLUG"
# Split the comma-separated values into an array
IFS=',' read -ra repo_array <<< "$REPO_NAMES"
# Iterate over each repository name
for repo_name in "${repo_array[@]}"; do
echo "Processing repository: $repo_name"
response=$(curl -i -L \
-X PUT \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/orgs/$ORGANIZATION/teams/$TEAM_SLUG/repos/$ORGANIZATION/$repo_name" \
-d "{\"permission\":\"$PERMISSION\"}")
echo "Added repository: $repo_name to $TEAM_NAME"
# Used for debugging bash script
# response=$(curl -i -L \
# -X PUT \
# -H "Accept: application/vnd.github+json" \
# -H "Authorization: Bearer $GH_TOKEN" \
# -H "X-GitHub-Api-Version: 2022-11-28" \
# "https://api.github.com/orgs/$ORGANIZATION/teams/$TEAM_SLUG/repos/$ORGANIZATION/$repo_name" \
# -d "{\"permission\":\"$PERMISSION\"}" \
# > response.txt 2>&1)
done
echo "Execution completed."