From e0e1dca5d6d08230187d18008b5e6c9ec64584fb Mon Sep 17 00:00:00 2001 From: Saravana Dhandapani Date: Sat, 8 Feb 2025 22:54:54 -0800 Subject: [PATCH] fix: more robustness clean up but still broken --- list_repos_contributors_by_user.sh | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/list_repos_contributors_by_user.sh b/list_repos_contributors_by_user.sh index f9f6801..c87b14d 100644 --- a/list_repos_contributors_by_user.sh +++ b/list_repos_contributors_by_user.sh @@ -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