Skip to content

Instantly share code, notes, and snippets.

@d1rtym0nk3y
Created February 29, 2024 10:05
Show Gist options
  • Save d1rtym0nk3y/1d31ccda6c0c88cbb0f249c9e1078b56 to your computer and use it in GitHub Desktop.
Save d1rtym0nk3y/1d31ccda6c0c88cbb0f249c9e1078b56 to your computer and use it in GitHub Desktop.
#!/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