|
| 1 | +import os |
| 2 | +import requests |
| 3 | +import git |
| 4 | +from pathlib import Path |
| 5 | +import shutil |
| 6 | + |
| 7 | +# Your GitHub repository URL and token |
| 8 | +repo_url = "https://github.com/Scratch-Coding-Hut/Scratch-Coding-Hut" |
| 9 | +token = "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" # This is your PAT stored as a secret in GitHub |
| 10 | + |
| 11 | +# Function to clone the repo using GitHub API |
| 12 | +def clone_repo(): |
| 13 | + # Clone the repo using GitPython (it can also clone using git command internally) |
| 14 | + repo_dir = Path("./Scratch-Coding-Hut") |
| 15 | + if repo_dir.exists(): |
| 16 | + shutil.rmtree(repo_dir) # Remove the old repo if it exists, but not the Python script |
| 17 | + git.Repo.clone_from(repo_url, repo_dir, depth=1) |
| 18 | + return repo_dir |
| 19 | + |
| 20 | +# Function to copy files excluding .github and .git folder |
| 21 | +def copy_files(src_dir, dest_dir): |
| 22 | + # Iterate over the source directory and copy files, skipping .github and .git |
| 23 | + for item in os.listdir(src_dir): |
| 24 | + if item not in [".github", ".git"]: # Skip both .github and .git folder |
| 25 | + src_path = src_dir / item |
| 26 | + dest_path = dest_dir / item |
| 27 | + if src_path.is_dir(): |
| 28 | + shutil.copytree(src_path, dest_path) # Copy directories |
| 29 | + else: |
| 30 | + shutil.copy2(src_path, dest_path) # Copy files |
| 31 | + |
| 32 | +# Function to set Git config |
| 33 | +def set_git_config(): |
| 34 | + # Set the Git config for user name and email to avoid identity issues |
| 35 | + os. system( 'git config --global user.email "[email protected]"') |
| 36 | + os.system('git config --global user.name "GitHub Actions"') |
| 37 | + |
| 38 | +# Main function |
| 39 | +def main(): |
| 40 | + # Step 1: Set Git config |
| 41 | + set_git_config() |
| 42 | + |
| 43 | + # Step 2: Clone the repo |
| 44 | + src_repo_dir = clone_repo() |
| 45 | + |
| 46 | + # Step 3: Prepare the destination path (current repository) |
| 47 | + dest_repo_dir = Path(".") # This is the current directory where the GitHub Action is running |
| 48 | + |
| 49 | + # Step 4: Copy files excluding .github and .git folders |
| 50 | + copy_files(src_repo_dir, dest_repo_dir) |
| 51 | + |
| 52 | + # Step 5: Clean up - Remove the cloned repo directory if you don't need it anymore (excluding Python script) |
| 53 | + shutil.rmtree(src_repo_dir) |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + main() |
0 commit comments