| 266 | } |
| 267 | |
| 268 | func parsePRRef(input string) (string, string, int, error) { |
| 269 | candidate := strings.TrimSpace(strings.TrimSuffix(input, "/")) |
| 270 | if candidate == "" { |
| 271 | return "", "", 0, fmt.Errorf("PR reference cannot be empty") |
| 272 | } |
| 273 | |
| 274 | if strings.HasPrefix(candidate, "http://") || strings.HasPrefix(candidate, "https://") { |
| 275 | u, err := url.Parse(candidate) |
| 276 | if err != nil { |
| 277 | return "", "", 0, fmt.Errorf("parse url %q: %w", input, err) |
| 278 | } |
| 279 | if !strings.EqualFold(u.Host, "github.com") { |
| 280 | return "", "", 0, fmt.Errorf("expected github.com host, got %s", u.Host) |
| 281 | } |
| 282 | segments := strings.Split(strings.Trim(u.Path, "/"), "/") |
| 283 | if len(segments) < 4 { |
| 284 | return "", "", 0, fmt.Errorf("expected GitHub PR URL, got %q", input) |
| 285 | } |
| 286 | owner := segments[0] |
| 287 | repo := strings.TrimSuffix(segments[1], ".git") |
| 288 | number := 0 |
| 289 | for i := 2; i < len(segments); i++ { |
| 290 | if segments[i] == "pull" || segments[i] == "pulls" { |
| 291 | if i+1 < len(segments) { |
| 292 | if n, err := strconv.Atoi(strings.TrimSpace(segments[i+1])); err == nil && n > 0 { |
| 293 | number = n |
| 294 | break |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | if owner == "" || repo == "" || number == 0 { |
| 300 | return "", "", 0, fmt.Errorf("unable to parse PR from %q", input) |
| 301 | } |
| 302 | return owner, repo, number, nil |
| 303 | } |
| 304 | |
| 305 | if hash := strings.Index(candidate, "#"); hash > 0 { |
| 306 | repoPart := strings.TrimSpace(candidate[:hash]) |
| 307 | numberPart := strings.TrimSpace(candidate[hash+1:]) |
| 308 | parts := strings.Split(repoPart, "/") |
| 309 | if len(parts) != 2 || parts[0] == "" || parts[1] == "" { |
| 310 | return "", "", 0, fmt.Errorf("invalid repo format %q, expected owner/repo", repoPart) |
| 311 | } |
| 312 | number, err := strconv.Atoi(numberPart) |
| 313 | if err != nil || number <= 0 { |
| 314 | return "", "", 0, fmt.Errorf("invalid PR number %q", numberPart) |
| 315 | } |
| 316 | return parts[0], parts[1], number, nil |
| 317 | } |
| 318 | |
| 319 | return "", "", 0, fmt.Errorf("unrecognized PR reference format: %q", input) |
| 320 | } |