Skip to content

Instantly share code, notes, and snippets.

@KyMidd
Created February 19, 2023 17:33
Show Gist options
  • Save KyMidd/ef67eefb07a502e43c4cd506742eb858 to your computer and use it in GitHub Desktop.
Save KyMidd/ef67eefb07a502e43c4cd506742eb858 to your computer and use it in GitHub Desktop.
# Export GITHUB_TOKEN
# Vars
ORG=your-github-org-name
# Find all stash projects to iterate over
PROJECTS=$(cat collections.json| jq -r '.[].name')
# Declare function
create_gh_team() {
# Debug
echo "\$1 is $1" # $1 is team name with lower-case lead, e.g. platformLeads
echo "\$2 is $2" # $2 is team name with upper-case lead, e.g. PlatformLeads
if [[ $(echo "$PROJECT_TEAMS" | jq ".$searchString[]" | grep "isPrimary" | wc -l) -ge 1 ]] ; then
# Set child team name
unset TEAMNAME
TEAMNAME="$PROJECT"$2
echo Child team name will be $TEAMNAME
# Check if exist
unset CURL
CURL=$(curl -s \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/orgs/$ORG/teams/$TEAMNAME)
if [[ $(echo "$CURL" | grep "Not Found" | wc -l) -eq 1 ]]; then
echo "Team does not exist, create"
echo Creating team "$TEAMNAME"
unset CURL
CURL=$(curl -s \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/orgs/$ORG/teams \
-d "{\"name\":\"$TEAMNAME\",\"privacy\":\"closed\",\"parent_team_id\":$PARENT_TEAM_ID}" 2>&1)
if [[ $(echo "$CURL" | wc -l) -ge 10 ]]; then
echo "- $TEAMNAME created successfully"
else
echo "Problem creating $TEAMNAME"
echo $CURL
fi
else
# Team already exists
echo "Team $TEAMNAME already exists"
fi
fi
}
# Create Team Names
for PROJECT in $(echo "$PROJECTS"); do
echo "💥 Working on project $PROJECT"
# Grab project info
PROJECT_TEAMS=$(cat collections.json | jq -r ".[] | select (.name == \"$PROJECT\")")
#### Parent Team
unset TEAMNAME
TEAMNAME="$PROJECT"
# Check if exist
unset CURL
CURL=$(curl -s \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/orgs/$ORG/teams/$TEAMNAME)
if [[ $(echo "$CURL" | grep "Not Found" | wc -l) -eq 1 ]]; then
# Team doesn't exist, create
echo Creating team "$TEAMNAME"
unset CURL
CURL=$(curl -s \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/orgs/$ORG/teams \
-d "{\"name\":\"$TEAMNAME\",\"description\":\"Parent team for project $PROJECT\",\"privacy\":\"closed\"}" 2>&1)
if [[ $(echo "$CURL" | wc -l) -ge 10 ]]; then
echo "- $TEAMNAME created successfully"
# Sleep for 2 seconds to let github coalesce
sleep 2
else
echo "Problem creating $TEAMNAME"
echo $CURL
fi
else
# Team already exists, skipping
echo "Team $TEAMNAME already exists, skipping"
fi
# Get parent team ID so other teams can be children
PARENT_TEAM_ID=$(curl -s \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/orgs/$ORG/teams/$TEAMNAME | jq '.id')
if [[ $(echo "$PARENT_TEAM_ID" | wc -l) -ge 1 ]] ; then
# success
echo "Successfully retrieved Parent team ID, continuing"
else
echo "Failed to get parent team ID"
echo "Skipping other teams, they require this info to build"
continue
fi
# ServicesLeads
create_gh_team servicesLeads ServicesLeads
#TestLeads
create_gh_team testLeads TestLeads
# UiLeads
create_gh_team uiLeads UiLeads
# DataLeads
create_gh_team dataLeads DataLeads
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment