#!/bin/bash # Script to create GitHub repositories and convert contracts/frontend to submodules # Usage: GITHUB_TOKEN= ./scripts/setup-submodules.sh [org-name] set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Check for GitHub token if [ -z "$GITHUB_TOKEN" ]; then echo -e "${YELLOW}GITHUB_TOKEN not found in environment.${NC}" echo "Please provide it:" echo " export GITHUB_TOKEN=your_token_here" echo " ./scripts/setup-submodules.sh [org-name]" exit 1 fi # Get organization/user name if [ -z "$1" ]; then # Try to get from current git remote ORG=$(git remote get-url origin 2>/dev/null | sed -E 's|.*github.com[:/]([^/]+)/.*|\1|' || echo "") if [ -z "$ORG" ]; then echo -e "${RED}Error: Please provide GitHub organization/username${NC}" echo "Usage: GITHUB_TOKEN= ./scripts/setup-submodules.sh " exit 1 fi else ORG="$1" fi echo -e "${GREEN}Setting up submodules for organization: ${ORG}${NC}" # Get repository name from current directory or git remote REPO_NAME=$(basename $(git rev-parse --show-toplevel)) MAIN_REPO="${ORG}/${REPO_NAME}" echo -e "${GREEN}Main repository: ${MAIN_REPO}${NC}" # Function to create GitHub repository create_repo() { local repo_name=$1 local description=$2 local is_private=${3:-false} echo -e "${YELLOW}Creating repository: ${ORG}/${repo_name}${NC}" response=$(curl -s -w "\n%{http_code}" -X POST \ -H "Authorization: token ${GITHUB_TOKEN}" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/user/repos \ -d "{ \"name\": \"${repo_name}\", \"description\": \"${description}\", \"private\": ${is_private}, \"auto_init\": false }") http_code=$(echo "$response" | tail -n1) body=$(echo "$response" | sed '$d') if [ "$http_code" -eq 201 ]; then echo -e "${GREEN}✓ Repository ${repo_name} created successfully${NC}" return 0 elif [ "$http_code" -eq 422 ]; then echo -e "${YELLOW}⚠ Repository ${repo_name} may already exist, continuing...${NC}" return 0 else echo -e "${RED}✗ Failed to create repository ${repo_name}${NC}" echo "Response: $body" return 1 fi } # Function to push directory to repository push_to_repo() { local dir=$1 local repo_name=$2 echo -e "${YELLOW}Pushing ${dir} to ${ORG}/${repo_name}${NC}" cd "$dir" # Initialize git if not already if [ ! -d ".git" ]; then git init git add . git commit -m "Initial commit" fi # Add remote and push git remote remove origin 2>/dev/null || true git remote add origin "https://${GITHUB_TOKEN}@github.com/${ORG}/${repo_name}.git" git branch -M main git push -u origin main --force cd .. echo -e "${GREEN}✓ ${dir} pushed to ${ORG}/${repo_name}${NC}" } # Step 1: Create repositories echo -e "\n${GREEN}=== Step 1: Creating GitHub repositories ===${NC}" create_repo "${REPO_NAME}-contracts" "ASLE Smart Contracts (Foundry)" false create_repo "${REPO_NAME}-frontend" "ASLE Frontend (Next.js)" false # Step 2: Push contracts and frontend to their repositories echo -e "\n${GREEN}=== Step 2: Pushing code to repositories ===${NC}" # Remove from main repo temporarily echo -e "${YELLOW}Removing contracts and frontend from main repo...${NC}" git rm -r --cached contracts frontend 2>/dev/null || true # Push contracts if [ -d "contracts" ]; then push_to_repo "contracts" "${REPO_NAME}-contracts" fi # Push frontend if [ -d "frontend" ]; then push_to_repo "frontend" "${REPO_NAME}-frontend" fi # Step 3: Add as submodules echo -e "\n${GREEN}=== Step 3: Adding as submodules ===${NC}" # Remove directories rm -rf contracts frontend # Add as submodules echo -e "${YELLOW}Adding contracts as submodule...${NC}" git submodule add "https://github.com/${ORG}/${REPO_NAME}-contracts.git" contracts echo -e "${YELLOW}Adding frontend as submodule...${NC}" git submodule add "https://github.com/${ORG}/${REPO_NAME}-frontend.git" frontend # Step 4: Commit changes echo -e "\n${GREEN}=== Step 4: Committing submodule configuration ===${NC}" git add .gitmodules contracts frontend git commit -m "Convert contracts and frontend to git submodules" || echo "No changes to commit" echo -e "\n${GREEN}✓ Submodules setup complete!${NC}" echo -e "\nRepository URLs:" echo -e " Contracts: https://github.com/${ORG}/${REPO_NAME}-contracts" echo -e " Frontend: https://github.com/${ORG}/${REPO_NAME}-frontend" echo -e "\nTo clone with submodules:" echo -e " git clone --recurse-submodules "