Parse GitHub URL and extract owner, repo, branch, and path. Returns a dict with keys: owner, repo, branch, path, type.
(url: str)
| 130 | |
| 131 | |
| 132 | def parse_github_resource_url(url: str) -> dict[str, str] | None: |
| 133 | """ |
| 134 | Parse GitHub URL and extract owner, repo, branch, and path. |
| 135 | Returns a dict with keys: owner, repo, branch, path, type. |
| 136 | """ |
| 137 | patterns = { |
| 138 | # File in repository |
| 139 | "file": r"https://github\.com/([^/]+)/([^/]+)/(?:blob|raw)/([^/]+)/(.+)", |
| 140 | # Directory in repository |
| 141 | "dir": r"https://github\.com/([^/]+)/([^/]+)/tree/([^/]+)/(.+)", |
| 142 | # Repository root |
| 143 | "repo": r"https://github\.com/([^/]+)/([^/]+)/?$", |
| 144 | # Gist |
| 145 | "gist": r"https://gist\.github\.com/([^/]+)/([^/#]+)", |
| 146 | } |
| 147 | |
| 148 | for url_type, pattern in patterns.items(): |
| 149 | match = re.match(pattern, url) |
| 150 | if match: |
| 151 | if url_type == "gist": |
| 152 | return { |
| 153 | "type": "gist", |
| 154 | "owner": match.group(1), |
| 155 | "gist_id": match.group(2), |
| 156 | } |
| 157 | elif url_type == "repo": |
| 158 | return { |
| 159 | "type": "repo", |
| 160 | "owner": match.group(1), |
| 161 | "repo": _normalize_repo_name(match.group(2)), |
| 162 | } |
| 163 | else: |
| 164 | return { |
| 165 | "type": url_type, |
| 166 | "owner": match.group(1), |
| 167 | "repo": _normalize_repo_name(match.group(2)), |
| 168 | "branch": match.group(3), |
| 169 | "path": match.group(4), |
| 170 | } |
| 171 | |
| 172 | return None |