From 63bb8fd9b4fc444ae33732bd6c01c272bea22055 Mon Sep 17 00:00:00 2001 From: Saravana Dhandapani Date: Sat, 8 Feb 2025 22:34:31 -0800 Subject: [PATCH] feat: delete users who are not part of whitelist --- list_repos_contributors_by_user.sh | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/list_repos_contributors_by_user.sh b/list_repos_contributors_by_user.sh index 1897c6a..a20d9e0 100644 --- a/list_repos_contributors_by_user.sh +++ b/list_repos_contributors_by_user.sh @@ -16,7 +16,7 @@ if [[ -z "$REPO_LIST" ]]; then exit 1 fi -echo "🔍 Fetching repositories with non-whitelisted collaborators..." +echo "🔍 Checking repositories for non-whitelisted collaborators..." for REPO in $REPO_LIST; do # Determine the actual owner (useful if it belongs to an org) REPO_OWNER=$(echo "$REPO_DATA" | jq -r --arg REPO "$REPO" '.[] | select(.name==$REPO) | .owner.login') @@ -39,5 +39,24 @@ for REPO in $REPO_LIST; do echo "❌ Non-Whitelisted Collaborators:" printf '%s\n' "${NON_WHITELISTED_COLLABS[@]}" echo "--------------------------------------------" + + # Ask for confirmation and delete non-whitelisted collaborators + 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 + 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." + else + echo "❌ Failed to remove $USER from $REPO (HTTP Status: $RESPONSE)" + fi + else + echo "🚫 Skipped removal of $USER from $REPO." + fi + done + echo "--------------------------------------------" fi done