bluenevus commited on
Commit
aca813e
·
verified ·
1 Parent(s): 213d688

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -36
app.py CHANGED
@@ -12,6 +12,8 @@ import os
12
  from io import BytesIO
13
  from docx import Document
14
  import markdown
 
 
15
 
16
  # Hugging Face variables
17
  GIT_TOKEN = os.environ.get('GIT_TOKEN')
@@ -28,7 +30,12 @@ def fetch_git_files(git_url, git_provider):
28
  parts = git_url.split('/')
29
  owner = parts[3]
30
  repo = parts[4].split('.git')[0]
31
- branch = 'main' # You might want to make this configurable
 
 
 
 
 
32
 
33
  # List of common dependency files to look for
34
  dependency_files = [
@@ -38,48 +45,21 @@ def fetch_git_files(git_url, git_provider):
38
 
39
  all_content = ""
40
 
41
- # Set up headers with the personal access token
42
- headers = {
43
- "Authorization": f"token {GIT_TOKEN}",
44
- "Accept": "application/vnd.github.v3+json"
45
- }
46
-
47
- base_url = {
48
- 'GitHub': 'https://api.github.com',
49
- 'GitLab': 'https://gitlab.com/api/v4',
50
- 'Gitea': 'https://gitea.com/api/v1' # Adjust this URL for your Gitea instance
51
- }.get(git_provider)
52
-
53
  for file_path in dependency_files:
54
- # Construct the API URL based on the git provider
55
- if git_provider == 'GitHub':
56
- api_url = f"{base_url}/repos/{owner}/{repo}/contents/{file_path}?ref={branch}"
57
- elif git_provider == 'GitLab':
58
- api_url = f"{base_url}/projects/{owner}%2F{repo}/repository/files/{file_path}/raw?ref={branch}"
59
- elif git_provider == 'Gitea':
60
- api_url = f"{base_url}/repos/{owner}/{repo}/contents/{file_path}?ref={branch}"
61
-
62
- # Make the API request
63
- response = requests.get(api_url, headers=headers)
64
-
65
- if response.status_code == 200:
66
- if git_provider == 'GitHub' or git_provider == 'Gitea':
67
- content = response.json()
68
- if isinstance(content, dict) and 'content' in content:
69
- file_content = base64.b64decode(content['content']).decode('utf-8')
70
- all_content += f"\n\n--- {file_path} ---\n{file_content}"
71
- elif git_provider == 'GitLab':
72
- file_content = response.text
73
- all_content += f"\n\n--- {file_path} ---\n{file_content}"
74
 
75
  if not all_content:
76
  return "Error: No dependency files found in the repository."
77
 
78
  return all_content
79
- except requests.exceptions.RequestException as e:
 
80
  return f"Error accessing {git_provider}: {str(e)}"
81
- except json.JSONDecodeError:
82
- return f"Error: Unable to parse {git_provider} API response for {file_path}"
83
 
84
  def process_chunk_with_gemini(chunk, gemini_api_key):
85
  genai.configure(api_key=gemini_api_key)
 
12
  from io import BytesIO
13
  from docx import Document
14
  import markdown
15
+ from github import Github
16
+ from github import GithubException
17
 
18
  # Hugging Face variables
19
  GIT_TOKEN = os.environ.get('GIT_TOKEN')
 
30
  parts = git_url.split('/')
31
  owner = parts[3]
32
  repo = parts[4].split('.git')[0]
33
+
34
+ # Initialize PyGitHub with the token
35
+ g = Github(GIT_TOKEN)
36
+
37
+ # Get the repository
38
+ repo = g.get_repo(f"{owner}/{repo}")
39
 
40
  # List of common dependency files to look for
41
  dependency_files = [
 
45
 
46
  all_content = ""
47
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  for file_path in dependency_files:
49
+ try:
50
+ file_content = repo.get_contents(file_path)
51
+ all_content += f"\n\n--- {file_path} ---\n{file_content.decoded_content.decode('utf-8')}"
52
+ except GithubException:
53
+ # File not found, skip to next file
54
+ continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  if not all_content:
57
  return "Error: No dependency files found in the repository."
58
 
59
  return all_content
60
+
61
+ except GithubException as e:
62
  return f"Error accessing {git_provider}: {str(e)}"
 
 
63
 
64
  def process_chunk_with_gemini(chunk, gemini_api_key):
65
  genai.configure(api_key=gemini_api_key)