Skip to content

Instantly share code, notes, and snippets.

@KyMidd
Created February 19, 2023 17:42
Show Gist options
  • Save KyMidd/6e9d720598c3cfda7342b88bd7fddd59 to your computer and use it in GitHub Desktop.
Save KyMidd/6e9d720598c3cfda7342b88bd7fddd59 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
populate_gh_team() {
#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
TEAM=$1
# Identify bitbucket members of each team
LEADS_MEMBERS=$(echo $PROJECT_INFO | jq -r ".$SEARCH_STRING | .[].slug")
# Check if team has no members. If none, gracefully exit function
if [[ $(echo "$LEADS_MEMBERS" | awk 'NF' | wc -l) -eq 0 ]]; then
# If no team members, return 0 to exit function
#echo "Team has no members"
return 0
fi
echo "## $2 team members:"
TEAM_SLUG=$(echo "$PROJECT"$2 | tr '[A-Z]' '[a-z]')
echo "Team slug is $TEAM_SLUG"
# Translate team members
while IFS= read -r team_member; do
# Normalize casing
team_member=$(echo $team_member | tr '[A-Z]' '[a-z]')
echo "Bitbucket team member is: $team_member"
unset GITHUB_USERNAME
while IFS="," read -r GITHUB_USERNAME HQ_USERNAME
do
# Normalize casing
GITHUB_USERNAME=$(echo $GITHUB_USERNAME | tr '[A-Z]' '[a-z]')
HQ_USERNAME=$(echo $HQ_USERNAME | tr '[A-Z]' '[a-z]')
# Ignore the headers line of the CSV
if [[ $GITHUB_USERNAME == "GITHUB_USERNAME" ]]; then
continue
fi
if [[ $HQ_USERNAME == $team_member ]]; then
GITHUB_USERNAME=$GITHUB_USERNAME
break
fi
done < github_user_and_hq_username.csv
if [ -z "$GITHUB_USERNAME" ]; then
echo "☠️ GitHub username not found"
else
echo "- GitHub username for this user is: $GITHUB_USERNAME"
# Add user to team
unset CURL
CURL=$(curl -s \
-X PUT \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/orgs/$ORG/teams/$TEAM_SLUG/memberships/$GITHUB_USERNAME \
-d '{"role":"maintainer"}' 2>&1)
if [[ $(echo "$CURL" | grep -E "Not Found|pending" | wc -l) -eq 1 ]]; then
echo "Error adding $GITHUB_USERNAME (Bitbucket user "$team_member") to github team $TEAM_SLUG"
echo "$CURL"
else
echo "- Successfully added $GITHUB_USERNAME to github team $TEAM_SLUG"
fi
fi
done <<< "$LEADS_MEMBERS"
# Remove KyMidd user from group, added automatically
unset CURL
CURL=$(curl -s \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/orgs/$ORG/teams/$TEAM_SLUG/memberships/kymidd 2>&1)
if [[ $(echo "$CURL" | awk 'NF' | wc -l) -eq 0 ]]; then
echo "x Successfully removed KyMidd from github team $TEAM_SLUG"
else
echo "Error removing KyMidd from github team $TEAM_SLUG"
echo "$CURL"
fi
}
for PROJECT in $(echo "$PROJECTS"); do
echo "💥 Working on project $PROJECT"
# Grab project info
PROJECT_INFO=$(cat collections.json | jq -r ".[] | select (.name == \"$PROJECT\")")
populate_gh_team servicesLeads ServicesLeads
populate_gh_team testLeads TestLeads
populate_gh_team uiLeads UiLeads
populate_gh_team dataLeads DataLeads
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment