Created
February 29, 2024 10:05
-
-
Save d1rtym0nk3y/1d31ccda6c0c88cbb0f249c9e1078b56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
export GH_PAGER="" | |
# Check if 4 arguments are passed | |
if [ "$#" -ne 4 ]; then | |
echo "Usage: $0 <organisation> <team> <repo pattern> <permission>" | |
exit 1 | |
fi | |
organisation=$1 | |
team=$2 | |
repo_pattern=$3 | |
permission=$4 | |
# Define an array with valid permissions | |
valid_permissions=("pull" "push" "admin") | |
# Check if the provided permission is valid | |
if [[ ! " ${valid_permissions[@]} " =~ " ${permission} " ]]; then | |
echo "Invalid permission: $permission" | |
echo "Valid permissions are: ${valid_permissions[*]}" | |
exit 1 | |
fi | |
echo "Fetching repositories for the organisation: $organisation..." | |
# Fetch all repositories for the organisation matching the given pattern | |
repos=$(gh repo list $organisation --limit 1000 | grep "$repo_pattern" | awk '{print $1}') | |
echo "Repositories matching pattern '$repo_pattern':" | |
echo "$repos" | |
# Prompt user for confirmation | |
read -p "Are you sure you want to add all these repositories to the team $team with $permission permission? (y/n) " -n 1 -r | |
echo # (optional) move to a new line | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
# Split the repos string into an array | |
IFS=$'\n' read -rd '' -a repo_array <<<"$repos" | |
for repo in "${repo_array[@]}" | |
do | |
echo "Adding $repo to team $team with $permission permission..." | |
# Add the repository to the team with the specified permission | |
gh api orgs/$organisation/teams/$team/repos/$repo --method PUT --field permission="$permission" | |
done | |
echo "All matched repositories have been added to the team $team with $permission permission." | |
else | |
echo "Operation cancelled." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment