fix: more robustness clean up but still broken

This commit is contained in:
Saravana Dhandapani 2025-02-08 22:54:54 -08:00
parent 3917d5cce1
commit e0e1dca5d6

View File

@ -54,7 +54,7 @@ for REPO in $REPO_LIST; do
for USER in "${NON_WHITELISTED_COLLABS[@]}"; do
read -p "Do you want to remove collaborator '$USER' from '$REPO'? (yes/no): " CONFIRM
if [[ "$CONFIRM" == "yes" ]]; then
# Attempt to remove collaborator from repository
# Attempt to remove as a direct repository collaborator
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$REPO_OWNER/$REPO/collaborators/$USER")
@ -63,11 +63,11 @@ for REPO in $REPO_LIST; do
else
echo "⚠️ Failed to remove $USER from repository $REPO (HTTP Status: $RESPONSE). Checking if they are an org member..."
# If collaborator removal failed, check if they are an organization member
ORG_MEMBER_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/orgs/$GITHUB_ORG/memberships/$USER")
# Check if the user is an organization member
ORG_MEMBER_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/orgs/$GITHUB_ORG/memberships/$USER")
if [[ "$ORG_MEMBER_RESPONSE" -eq 200 ]]; then
if [[ "$ORG_MEMBER_STATUS" -eq 200 ]]; then
read -p "$USER is an organization member. Remove them from org '$GITHUB_ORG'? (yes/no): " CONFIRM_ORG
if [[ "$CONFIRM_ORG" == "yes" ]]; then
ORG_REMOVE_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE -H "Authorization: token $GITHUB_TOKEN" \
@ -76,13 +76,27 @@ for REPO in $REPO_LIST; do
if [[ "$ORG_REMOVE_RESPONSE" -eq 204 ]]; then
echo "✅ Successfully removed $USER from organization '$GITHUB_ORG'."
else
echo "❌ Failed to remove $USER from the organization (HTTP Status: $ORG_REMOVE_RESPONSE)."
echo "❌ Failed to remove $USER from the organization (HTTP Status: $ORG_REMOVE_RESPONSE). Checking if they are in a team..."
# If removal from org fails, check if the user is in a team
TEAMS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/orgs/$GITHUB_ORG/teams" | jq -r '.[].slug')
for TEAM in $TEAMS; do
TEAM_REMOVE_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/orgs/$GITHUB_ORG/teams/$TEAM/memberships/$USER")
if [[ "$TEAM_REMOVE_RESPONSE" -eq 204 ]]; then
echo "✅ Successfully removed $USER from team '$TEAM'."
break
fi
done
fi
else
echo "🚫 Skipped removal of $USER from organization."
fi
else
echo "$USER is neither a direct collaborator nor an organization member. No action taken."
echo "$USER is neither a direct collaborator, nor an organization member, nor a team member. No action taken."
fi
fi
else