parseGitHubUrl static method
Extract the (owner, repo) pair from a GitHub URL.
Handles common URL shapes:
https://github.com/owner/repohttps://github.com/owner/repo.githttps://github.com/owner/repo/tree/main/...git@github.com:owner/repo.git
Returns null if the URL cannot be parsed as a GitHub repository.
Implementation
static (String owner, String repo)? parseGitHubUrl(String url) {
// Normalise: strip trailing slashes and `.git` suffix.
var normalized = url.trim();
// Handle SSH URLs: git@github.com:owner/repo.git
if (normalized.startsWith('git@github.com:')) {
normalized = normalized.substring('git@github.com:'.length);
normalized = normalized.replaceAll(RegExp(r'\.git$'), '');
final parts = normalized.split('/');
if (parts.length >= 2) {
return (parts[0], parts[1]);
}
return null;
}
// Handle HTTPS URLs.
final uri = Uri.tryParse(normalized);
if (uri == null || uri.host != 'github.com') return null;
// Path segments: ['', 'owner', 'repo', ...] (the leading '' comes from
// the leading `/`).
final segments = uri.pathSegments;
if (segments.length < 2) return null;
final owner = segments[0];
// Strip .git suffix if present.
final repo = segments[1].replaceAll(RegExp(r'\.git$'), '');
if (owner.isEmpty || repo.isEmpty) return null;
return (owner, repo);
}