|  | 
| 8 | 8 | from pathlib import Path | 
| 9 | 9 | from github import Github | 
| 10 | 10 | from models import db, BackupJob | 
|  | 11 | +from urllib.parse import urlparse | 
| 11 | 12 | 
 | 
| 12 | 13 | logger = logging.getLogger(__name__) | 
| 13 | 14 | 
 | 
| @@ -179,22 +180,26 @@ def _get_file_size(self, path): | 
| 179 | 180 |     def verify_github_access(self, repo_url, github_token=None): | 
| 180 | 181 |         """Verify if we can access a GitHub repository""" | 
| 181 | 182 |         try: | 
| 182 |  | -            # Extract owner and repo name from URL | 
| 183 |  | -            if 'github.com/' in repo_url: | 
| 184 |  | -                parts = repo_url.split('github.com/')[-1].split('/') | 
| 185 |  | -                if len(parts) >= 2: | 
| 186 |  | -                    owner = parts[0] | 
| 187 |  | -                    repo_name = parts[1].replace('.git', '') | 
| 188 |  | -                     | 
|  | 183 | +            # Parse the URL and check the hostname | 
|  | 184 | +            parsed = urlparse(repo_url) | 
|  | 185 | +            if parsed.hostname and parsed.hostname.lower() == "github.com": | 
|  | 186 | +                # Path is of the form /owner/repo(.git)? or /owner/repo/ | 
|  | 187 | +                path_parts = parsed.path.strip("/").split("/") | 
|  | 188 | +                if len(path_parts) >= 2: | 
|  | 189 | +                    owner = path_parts[0] | 
|  | 190 | +                    repo_name = path_parts[1] | 
|  | 191 | +                    if repo_name.endswith('.git'): | 
|  | 192 | +                        repo_name = repo_name[:-4] | 
|  | 193 | + | 
| 189 | 194 |                     if github_token: | 
| 190 | 195 |                         g = Github(github_token) | 
| 191 | 196 |                     else: | 
| 192 | 197 |                         g = Github()  # Anonymous access for public repos | 
| 193 |  | -                     | 
|  | 198 | + | 
| 194 | 199 |                     repo = g.get_repo(f"{owner}/{repo_name}") | 
| 195 | 200 |                     return True, f"Repository access verified: {repo.full_name}" | 
| 196 |  | -             | 
|  | 201 | + | 
| 197 | 202 |             return False, "Invalid GitHub repository URL" | 
| 198 |  | -             | 
|  | 203 | + | 
| 199 | 204 |         except Exception as e: | 
| 200 | 205 |             return False, f"Repository access failed: {str(e)}" | 
0 commit comments