(input: string)
| 43 | } |
| 44 | |
| 45 | export function parseGitHubRepositoryUrl(input: string): GitHubRepositoryLink | null { |
| 46 | const value = input.trim() |
| 47 | if (!value) { |
| 48 | return null |
| 49 | } |
| 50 | |
| 51 | const sshMatch = value.match(githubSshUrlPattern) |
| 52 | if (sshMatch) { |
| 53 | return buildGitHubRepositoryLink(sshMatch[1] ?? '', sshMatch[2] ?? '', 'ssh') |
| 54 | } |
| 55 | |
| 56 | const shorthandMatch = value.match(githubShorthandUrlPattern) |
| 57 | if (shorthandMatch) { |
| 58 | return buildGitHubRepositoryLink(shorthandMatch[1] ?? '', shorthandMatch[2] ?? '') |
| 59 | } |
| 60 | |
| 61 | let url: URL |
| 62 | try { |
| 63 | url = new URL(value) |
| 64 | } catch { |
| 65 | return null |
| 66 | } |
| 67 | |
| 68 | if ( |
| 69 | !githubHostPattern.test(url.hostname) || |
| 70 | (url.protocol !== 'https:' && url.protocol !== 'http:') |
| 71 | ) { |
| 72 | return null |
| 73 | } |
| 74 | |
| 75 | const [owner, repo] = url.pathname.split('/').filter(Boolean) |
| 76 | if (!(owner && repo)) { |
| 77 | return null |
| 78 | } |
| 79 | |
| 80 | return buildGitHubRepositoryLink(owner, repo) |
| 81 | } |
| 82 | |
| 83 | export function normalizeGitHubRepositoryUrl(input: string) { |
| 84 | const link = parseGitHubRepositoryUrl(input) |
no test coverage detected