bluenevus commited on
Commit
711210d
·
verified ·
1 Parent(s): be514d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
app.py CHANGED
@@ -4,14 +4,27 @@ import paramiko
4
  import io
5
  import re
6
 
7
- def fetch_github_file(github_url, ssh_private_key):
8
- try:
9
- # Parse the GitHub URL
 
 
 
 
 
 
10
  parts = github_url.split('/')
11
  owner = parts[3]
12
  repo = parts[4]
13
- branch = parts[6]
14
- file_path = '/'.join(parts[7:])
 
 
 
 
 
 
 
15
 
16
  # Set up SSH client
17
  ssh = paramiko.SSHClient()
@@ -24,7 +37,10 @@ def fetch_github_file(github_url, ssh_private_key):
24
  ssh.connect('github.com', username='git', pkey=private_key)
25
 
26
  # Construct the git command to fetch the file content
27
- git_command = f"git archive [email protected]:{owner}/{repo}.git {branch} {file_path} | tar -xO"
 
 
 
28
 
29
  # Execute the command
30
  stdin, stdout, stderr = ssh.exec_command(git_command)
@@ -38,7 +54,7 @@ def fetch_github_file(github_url, ssh_private_key):
38
  if not file_content:
39
  return {"error": "Error: File not found or empty."}
40
 
41
- return {file_path: file_content}
42
  except paramiko.AuthenticationException:
43
  return {"error": "Error: Authentication failed. Please check your SSH key."}
44
  except paramiko.SSHException as ssh_err:
@@ -75,13 +91,14 @@ def process_input(file, github_url, ssh_private_key, gemini_api_key):
75
  if file is not None:
76
  file_content = {file.name: file.read().decode('utf-8')}
77
  elif github_url and ssh_private_key:
78
- if not github_url.startswith("https://github.com/"):
79
- return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository/blob/branch/path/to/file"
80
  if not ssh_private_key.strip():
81
  return "Error: SSH Private Key is empty. Please provide a valid key."
82
- file_content = fetch_github_file(github_url, ssh_private_key)
83
- if isinstance(file_content, dict) and "error" in file_content:
84
- return file_content["error"]
 
 
 
85
  else:
86
  return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
87
 
@@ -96,7 +113,7 @@ iface = gr.Interface(
96
  fn=process_input,
97
  inputs=[
98
  gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
99
- gr.Textbox(label="GitHub File URL (optional)"),
100
  gr.Textbox(label="SSH Private Key (required if using GitHub URL)", type="password"),
101
  gr.Textbox(label="Gemini API Key", type="password"),
102
  ],
 
4
  import io
5
  import re
6
 
7
+ def parse_github_url(github_url):
8
+ if github_url.startswith("[email protected]:"):
9
+ # SSH format
10
+ parts = github_url.split(':')[1].split('/')
11
+ owner = parts[0]
12
+ repo = parts[1].split('.git')[0]
13
+ return owner, repo, None, None
14
+ elif github_url.startswith("https://github.com/"):
15
+ # HTTPS format
16
  parts = github_url.split('/')
17
  owner = parts[3]
18
  repo = parts[4]
19
+ branch = parts[6] if len(parts) > 6 else None
20
+ file_path = '/'.join(parts[7:]) if len(parts) > 7 else None
21
+ return owner, repo, branch, file_path
22
+ else:
23
+ raise ValueError("Invalid GitHub URL format")
24
+
25
+ def fetch_github_file(github_url, ssh_private_key):
26
+ try:
27
+ owner, repo, branch, file_path = parse_github_url(github_url)
28
 
29
  # Set up SSH client
30
  ssh = paramiko.SSHClient()
 
37
  ssh.connect('github.com', username='git', pkey=private_key)
38
 
39
  # Construct the git command to fetch the file content
40
+ if branch and file_path:
41
+ git_command = f"git archive [email protected]:{owner}/{repo}.git {branch} {file_path} | tar -xO"
42
+ else:
43
+ git_command = f"git ls-tree -r --name-only HEAD | grep -E '(requirements\.txt|package\.json|Gemfile)'"
44
 
45
  # Execute the command
46
  stdin, stdout, stderr = ssh.exec_command(git_command)
 
54
  if not file_content:
55
  return {"error": "Error: File not found or empty."}
56
 
57
+ return {file_path if file_path else "repository_files": file_content}
58
  except paramiko.AuthenticationException:
59
  return {"error": "Error: Authentication failed. Please check your SSH key."}
60
  except paramiko.SSHException as ssh_err:
 
91
  if file is not None:
92
  file_content = {file.name: file.read().decode('utf-8')}
93
  elif github_url and ssh_private_key:
 
 
94
  if not ssh_private_key.strip():
95
  return "Error: SSH Private Key is empty. Please provide a valid key."
96
+ try:
97
+ file_content = fetch_github_file(github_url, ssh_private_key)
98
+ if "error" in file_content:
99
+ return file_content["error"]
100
+ except ValueError as e:
101
+ return str(e)
102
  else:
103
  return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
104
 
 
113
  fn=process_input,
114
  inputs=[
115
  gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
116
+ gr.Textbox(label="GitHub Repository URL (optional, SSH or HTTPS format)"),
117
  gr.Textbox(label="SSH Private Key (required if using GitHub URL)", type="password"),
118
  gr.Textbox(label="Gemini API Key", type="password"),
119
  ],