fix: delete

This commit is contained in:
Saravana Dhandapani 2025-02-08 22:48:36 -08:00
parent 63bb8fd9b4
commit 3917d5cce1

View File

@ -1,5 +1,10 @@
#!/bin/bash #!/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 # Define the whitelist of allowed collaborators
WHITELIST=("saravanakumardb" "saravanange" "abhinaisai2002" "sandho" "akshaj-us" "saravanakumardb1" "bytelyst-ai" "umadev0931") WHITELIST=("saravanakumardb" "saravanange" "abhinaisai2002" "sandho" "akshaj-us" "saravanakumardb1" "bytelyst-ai" "umadev0931")
@ -18,9 +23,14 @@ fi
echo "🔍 Checking repositories for non-whitelisted collaborators..." echo "🔍 Checking repositories for non-whitelisted collaborators..."
for REPO in $REPO_LIST; do 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') 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) # Fetch all collaborators (includes users even if they haven't committed)
ALL_COLLABORATORS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ ALL_COLLABORATORS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$REPO_OWNER/$REPO/collaborators" | jq -r '.[].login') "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 for USER in "${NON_WHITELISTED_COLLABS[@]}"; do
read -p "Do you want to remove collaborator '$USER' from '$REPO'? (yes/no): " CONFIRM read -p "Do you want to remove collaborator '$USER' from '$REPO'? (yes/no): " CONFIRM
if [[ "$CONFIRM" == "yes" ]]; then 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" \ 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") "https://api.github.com/repos/$REPO_OWNER/$REPO/collaborators/$USER")
if [[ "$RESPONSE" -eq 204 ]]; then if [[ "$RESPONSE" -eq 204 ]]; then
echo "✅ Successfully removed $USER from $REPO." echo "✅ Successfully removed $USER from repository $REPO."
else 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 fi
else else
echo "🚫 Skipped removal of $USER from $REPO." echo "🚫 Skipped removal of $USER from $REPO."