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 '')