diff --git a/list_repos_contributors_by_user.sh b/list_repos_contributors_by_user.sh index a20d9e0..f9f6801 100644 --- a/list_repos_contributors_by_user.sh +++ b/list_repos_contributors_by_user.sh @@ -1,5 +1,10 @@ #!/bin/bash +# Load environment variables +GITHUB_TOKEN="${GITHUB_TOKEN:?❌ Error: GITHUB_TOKEN is not set in ~/.zshrc}" +GITHUB_ORG="${GITHUB_ORG:?❌ Error: GITHUB_ORG is not set in ~/.zshrc}" +GITHUB_USER="${GITHUB_USER:?❌ Error: GITHUB_USER is not set in ~/.zshrc}" + # Define the whitelist of allowed collaborators WHITELIST=("saravanakumardb" "saravanange" "abhinaisai2002" "sandho" "akshaj-us" "saravanakumardb1" "bytelyst-ai" "umadev0931") @@ -18,9 +23,14 @@ fi echo "🔍 Checking repositories for non-whitelisted collaborators..." for REPO in $REPO_LIST; do - # Determine the actual owner (useful if it belongs to an org) + # Determine if repo is under organization or user REPO_OWNER=$(echo "$REPO_DATA" | jq -r --arg REPO "$REPO" '.[] | select(.name==$REPO) | .owner.login') + # If repo belongs to the org, use $GITHUB_ORG instead of user + if [[ "$REPO_OWNER" == "$GITHUB_ORG" ]]; then + REPO_OWNER="$GITHUB_ORG" + fi + # Fetch all collaborators (includes users even if they haven't committed) ALL_COLLABORATORS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ "https://api.github.com/repos/$REPO_OWNER/$REPO/collaborators" | jq -r '.[].login') @@ -44,14 +54,36 @@ 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 - # API request to remove collaborator + # Attempt to remove collaborator from repository 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") if [[ "$RESPONSE" -eq 204 ]]; then - echo "✅ Successfully removed $USER from $REPO." + echo "✅ Successfully removed $USER from repository $REPO." else - echo "❌ Failed to remove $USER from $REPO (HTTP Status: $RESPONSE)" + 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") + + if [[ "$ORG_MEMBER_RESPONSE" -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" \ + "https://api.github.com/orgs/$GITHUB_ORG/memberships/$USER") + + 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)." + 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." + fi fi else echo "🚫 Skipped removal of $USER from $REPO."