parseRawGitHubContentURL parses raw.githubusercontent.com URLs Supports URLs like: - https://raw.githubusercontent.com/owner/repo/refs/heads/branch/path/to/file.md - https://raw.githubusercontent.com/owner/repo/COMMIT_SHA/path/to/file.md - https://raw.githubusercontent.com/owner/repo/refs/tags/tag/p
(parsedURL *url.URL)
| 240 | // - https://raw.githubusercontent.com/owner/repo/COMMIT_SHA/path/to/file.md |
| 241 | // - https://raw.githubusercontent.com/owner/repo/refs/tags/tag/path/to/file.md |
| 242 | func parseRawGitHubContentURL(parsedURL *url.URL) (*GitHubURLComponents, error) { |
| 243 | pathParts := strings.Split(strings.Trim(parsedURL.Path, "/"), "/") |
| 244 | |
| 245 | // Need at least: owner, repo, ref-or-sha, and filename |
| 246 | if len(pathParts) < 4 { |
| 247 | return nil, errors.New("invalid raw.githubusercontent.com URL format: path too short") |
| 248 | } |
| 249 | |
| 250 | owner := pathParts[0] |
| 251 | repo := pathParts[1] |
| 252 | |
| 253 | // Determine the reference and file path based on the third part |
| 254 | var ref string |
| 255 | var filePath string |
| 256 | |
| 257 | if pathParts[2] == "refs" { |
| 258 | // Format: /owner/repo/refs/heads/branch/path/to/file |
| 259 | // or /owner/repo/refs/tags/tag/path/to/file |
| 260 | if len(pathParts) < 5 { |
| 261 | return nil, errors.New("invalid raw.githubusercontent.com URL format: refs path too short") |
| 262 | } |
| 263 | // pathParts[3] is "heads" or "tags" |
| 264 | ref = pathParts[4] // branch or tag name |
| 265 | filePath = strings.Join(pathParts[5:], "/") |
| 266 | } else { |
| 267 | // Format: /owner/repo/COMMIT_SHA/path/to/file or /owner/repo/branch/path/to/file |
| 268 | ref = pathParts[2] |
| 269 | filePath = strings.Join(pathParts[3:], "/") |
| 270 | } |
| 271 | |
| 272 | // Validate owner and repo |
| 273 | if owner == "" || repo == "" { |
| 274 | return nil, errors.New("invalid raw.githubusercontent.com URL: owner and repo cannot be empty") |
| 275 | } |
| 276 | |
| 277 | return &GitHubURLComponents{ |
| 278 | Host: "raw.githubusercontent.com", |
| 279 | Owner: owner, |
| 280 | Repo: repo, |
| 281 | Type: URLTypeRawContent, |
| 282 | Path: filePath, |
| 283 | Ref: ref, |
| 284 | }, nil |
| 285 | } |
| 286 | |
| 287 | // ParseRunURLExtended is similar to ParseRunURL but returns additional information |
| 288 | // including job ID and step details from deep URLs. |