parseGitHubRepoSlugFromURL extracts owner/repo from a GitHub URL Supports HTTPS (https://github.com/owner/repo), SCP-style SSH (git@github.com:owner/repo), and SSH URL scheme (ssh://git@github.com/owner/repo) formats. Also supports GitHub Enterprise URLs.
(url string)
| 57 | // and SSH URL scheme (ssh://git@github.com/owner/repo) formats. |
| 58 | // Also supports GitHub Enterprise URLs. |
| 59 | func parseGitHubRepoSlugFromURL(url string) string { |
| 60 | gitLog.Printf("Parsing GitHub repo slug from URL: %s", url) |
| 61 | |
| 62 | // Remove .git suffix if present |
| 63 | url = strings.TrimSuffix(url, ".git") |
| 64 | |
| 65 | githubHost := getGitHubHost() |
| 66 | githubHostWithoutScheme := strings.TrimPrefix(strings.TrimPrefix(githubHost, "https://"), "http://") |
| 67 | |
| 68 | // Handle HTTPS URLs: https://github.com/owner/repo or https://enterprise.github.com/owner/repo |
| 69 | if after, ok := strings.CutPrefix(url, githubHost+"/"); ok { |
| 70 | slug := after |
| 71 | gitLog.Printf("Extracted slug from HTTPS URL: %s", slug) |
| 72 | return slug |
| 73 | } |
| 74 | |
| 75 | // Handle SCP-style SSH URLs: git@github.com:owner/repo or git@enterprise.github.com:owner/repo |
| 76 | sshPrefix := "git@" + githubHostWithoutScheme + ":" |
| 77 | if after, ok := strings.CutPrefix(url, sshPrefix); ok { |
| 78 | slug := after |
| 79 | gitLog.Printf("Extracted slug from SSH URL: %s", slug) |
| 80 | return slug |
| 81 | } |
| 82 | |
| 83 | // Handle SSH URL scheme: ssh://git@github.com/owner/repo or ssh://github.com/owner/repo |
| 84 | if after, ok := strings.CutPrefix(url, "ssh://"); ok { |
| 85 | // Strip optional user info (e.g. "git@") |
| 86 | if _, userStripped, hasAt := strings.Cut(after, "@"); hasAt { |
| 87 | after = userStripped |
| 88 | } |
| 89 | if slug, ok := strings.CutPrefix(after, githubHostWithoutScheme+"/"); ok { |
| 90 | gitLog.Printf("Extracted slug from SSH URL scheme: %s", slug) |
| 91 | return slug |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | gitLog.Print("Could not extract slug from URL") |
| 96 | return "" |
| 97 | } |
| 98 | |
| 99 | // extractHostFromRemoteURL extracts the host (optionally including port) from a git remote URL. |
| 100 | // Supports HTTPS (https://host[:port]/path), HTTP (http://host[:port]/path), and SSH (git@host[:port]:path or ssh://git@host[:port]/path) formats. |