File size: 2,472 Bytes
b68a0cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import json
import os

# --- Constants ---
GITHUB_API_URL = "https://api.github.com"
GITHUB_REPO_OWNER = "canstralian"
GITHUB_REPO_NAME = "CodeShieldAI"
GITHUB_HEADERS = {
    "Authorization": f"Bearer {os.environ.get('GITHUB_TOKEN')}",
    "Accept": "application/vnd.github+json",
    "X-GitHub-Api-Version": "2022-11-28",
}

# --- Functions ---
def get_repo_contents(path=""):
    url = f"{GITHUB_API_URL}/repos/{GITHUB_REPO_OWNER}/{GITHUB_REPO_NAME}/contents/{path}"
    response = requests.get(url, headers=GITHUB_HEADERS)
    if response.status_code == 200:
        return response.json()
    else:
        st.error(f"Failed to fetch contents. Status code: {response.status_code}")
        return None

def get_file_content(file_path):
    url = f"{GITHUB_API_URL}/repos/{GITHUB_REPO_OWNER}/{GITHUB_REPO_NAME}/contents/{file_path}"
    response = requests.get(url, headers=GITHUB_HEADERS)
    if response.status_code == 200:
        content = response.json().get("content")
        if content:
            import base64
            return base64.b64decode(content).decode("utf-8")
        else:
            st.error("File content not found.")
            return None
    else:
        st.error(f"Failed to fetch file content. Status code: {response.status_code}")
        return None

# --- Streamlit App ---
st.title("CodeShieldAI Browser")

if not os.environ.get('GITHUB_TOKEN'):
    st.warning("Please set the GITHUB_TOKEN environment variable.")
else:
    path = st.text_input("Enter path (e.g., 'src'):", "")

    contents = get_repo_contents(path)

    if contents:
        files = [item for item in contents if item["type"] == "file"]
        directories = [item for item in contents if item["type"] == "dir"]

        st.subheader("Directories:")
        for directory in directories:
            if st.button(f"Open {directory['name']}"):
                st.experimental_rerun()
                path = f"{path}/{directory['name']}" if path else directory['name']
                st.text_input("Enter path (e.g., 'src'):", path)

        st.subheader("Files:")
        for file in files:
            if st.button(f"View {file['name']}"):
                file_path = f"{path}/{file['name']}" if path else file['name']
                file_content = get_file_content(file_path)
                if file_content:
                    st.code(file_content, language=file['name'].split('.')[-1] if '.' in file['name'] else '')