bytelyst-devops-tools/make_repos_private.sh

66 lines
2.3 KiB
Bash

#!/bin/bash
# Load environment variables
GITHUB_TOKEN="${GITHUB_TOKEN:?❌ Error: GITHUB_TOKEN is not set}"
REPO_LIST_FILE="repos.txt" # Name of the file containing the repo list
# Ensure the file exists
if [[ ! -f "$REPO_LIST_FILE" ]]; then
echo "❌ Error: File '$REPO_LIST_FILE' not found!"
exit 1
fi
echo "🔍 Checking and updating repositories to private..."
# Loop through each repository in the file
while IFS= read -r REPO_FULL_NAME; do
echo "🔄 Processing repository: $REPO_FULL_NAME..."
# Get repository details
REPO_INFO=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$REPO_FULL_NAME")
IS_PRIVATE=$(echo "$REPO_INFO" | jq -r '.private')
IS_FORK=$(echo "$REPO_INFO" | jq -r '.fork')
HAS_ADMIN_ACCESS=$(echo "$REPO_INFO" | jq -r '.permissions.admin')
IS_GITHUB_PAGES=$(echo "$REPO_INFO" | jq -r '.name' | grep -i '\.github\.io')
# Skip if already private
if [[ "$IS_PRIVATE" == "true" ]]; then
echo "✅ Repository '$REPO_FULL_NAME' is already private. Skipping..."
continue
fi
# Skip if the repo is a fork (GitHub doesn't allow making forked repos private)
if [[ "$IS_FORK" == "true" ]]; then
echo "⚠️ Skipping '$REPO_FULL_NAME' because it is a forked repository."
continue
fi
# Skip if the repo is a GitHub Pages site
if [[ -n "$IS_GITHUB_PAGES" ]]; then
echo "⚠️ Skipping '$REPO_FULL_NAME' because it is a GitHub Pages site."
continue
fi
# Skip if the user doesn't have admin access
if [[ "$HAS_ADMIN_ACCESS" != "true" ]]; then
echo "❌ Skipping '$REPO_FULL_NAME' because you do NOT have admin permissions."
continue
fi
# Convert the repository to private
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X PATCH -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-d '{"private": true}' \
"https://api.github.com/repos/$REPO_FULL_NAME")
if [[ "$RESPONSE" -eq 200 ]]; then
echo "✅ Successfully converted '$REPO_FULL_NAME' to private."
else
echo "❌ Failed to make '$REPO_FULL_NAME' private. HTTP Status: $RESPONSE"
fi
done < "$REPO_LIST_FILE"
echo "✅ All repositories processed."