File size: 4,011 Bytes
0b505f3
3c873ff
0b505f3
3c873ff
 
 
 
 
0b505f3
3c873ff
 
 
 
0b505f3
3c873ff
 
0b505f3
3c873ff
0b505f3
3c873ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import streamlit as st
import requests
import os
import shutil
import zipfile
from github import Github
from git import Repo
from datetime import datetime

def download_github_repo(url, local_path):
    if os.path.exists(local_path):
        shutil.rmtree(local_path)
    Repo.clone_from(url, local_path)

def create_zip_file(source_dir, output_filename):
    shutil.make_archive(output_filename, 'zip', source_dir)

def check_repo_exists(g, repo_name):
    try:
        g.get_repo(repo_name)
        return True
    except:
        return False

def create_repo(g, repo_name):
    user = g.get_user()
    return user.create_repo(repo_name)

def push_to_github(local_path, repo, github_token):
    repo_url = f"https://{github_token}@github.com/{repo.full_name}.git"
    repo = Repo(local_path)
    if 'origin' in [remote.name for remote in repo.remotes]:
        origin = repo.remote('origin')
        origin.set_url(repo_url)
    else:
        origin = repo.create_remote('origin', repo_url)
    origin.push(refspec='{}:{}'.format('master', 'master'))

def main():
    st.title("GitHub Repository Cloner and Uploader")
    
    # Sidebar instructions (unchanged)
    st.sidebar.title("How to Get Your GitHub Token")
    st.sidebar.markdown("""
    1. Go to your GitHub account settings
    2. Click on 'Developer settings' in the left sidebar
    3. Click on 'Personal access tokens' and then 'Tokens (classic)'
    4. Click 'Generate new token' and select 'Generate new token (classic)'
    5. Give your token a descriptive name
    6. Select the following scopes:
       - repo (all)
       - delete_repo
    7. Click 'Generate token' at the bottom of the page
    8. Copy your new token immediately (you won't be able to see it again!)
    
    **Important:** Keep your token secret and secure. Treat it like a password!
    """)

    # Input for source repository
    source_repo = st.text_input("Source GitHub Repository URL", 
                                value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit/")

    # GitHub token input
    github_token = os.environ.get("GITHUB")
    if not github_token:
        github_token = st.text_input("GitHub Personal Access Token", type="password",
                                     help="Paste your GitHub token here. See sidebar for instructions on how to get it.")

    if st.button("Clone and Upload"):
        if not github_token:
            st.error("Please enter a GitHub token. See the sidebar for instructions.")
            return

        with st.spinner("Processing..."):
            try:
                # Generate a date-stamped name
                timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
                new_repo_name = f"AIExample-Clone-{timestamp}"
                local_path = f"./temp_repo_{timestamp}"

                # Download the source repository
                download_github_repo(source_repo, local_path)

                # Create a zip file
                zip_filename = f"repo_contents_{timestamp}"
                create_zip_file(local_path, zip_filename)

                # Create the new repository
                g = Github(github_token)
                user = g.get_user()
                new_repo = create_repo(g, new_repo_name)

                # Push the contents to the new repository
                push_to_github(local_path, new_repo, github_token)

                # Clean up
                shutil.rmtree(local_path)
                os.remove(zip_filename + ".zip")

                st.success(f"Repository cloned and uploaded successfully to {new_repo.html_url}!")

            except Exception as e:
                st.error(f"An error occurred: {str(e)}")
            finally:
                # Ensure cleanup even if an error occurs
                if os.path.exists(local_path):
                    shutil.rmtree(local_path)
                if os.path.exists(zip_filename + ".zip"):
                    os.remove(zip_filename + ".zip")

if __name__ == "__main__":
    main()