Repository: cli/cli
File: pkg/cmd/pr/shared/finder.go (Go)

Code before:
// have non-empty hostname.
//
// Any path components (not query params or fragments) after the pull request
// number are also returned.
func ParseURL(prURL string) (ghrepo.Interface, int, string, error) {
	if prURL == "" {
		return nil, 0, "", fmt.Errorf("invalid URL: %q", prURL)
	}

	u, err := url.Parse(prURL)
	if err != nil {
		return nil, 0, "", err
	}

	if u.Scheme != "https" && u.Scheme != "http" {
		return nil, 0, "", fmt.Errorf("invalid scheme: %s", u.Scheme)
	}

	m := pullURLRE.FindStringSubmatch(u.Path)
	if m == nil {
		return nil, 0, "", fmt.Errorf("not a pull request URL: %s", prURL)
	}

	repo := ghrepo.NewWithHost(m[1], m[2], u.Hostname())
	prNumber, _ := strconv.Atoi(m[3])
	rest := m[4]
	return repo, prNumber, rest, nil
}

var fullReferenceRE = regexp.MustCompile(`^(?:([^/]+)/([^/]+))#(\d+)$`)

// ParseFullReference parses a short issue/pull request reference of the form
// "owner/repo#number", where owner, repo and number are all required.
func ParseFullReference(s string) (ghrepo.Interface, int, error) {
	if s == "" {
		return nil, 0, errors.New("empty reference")
	}

	m := fullReferenceRE.FindStringSubmatch(s)
	if m == nil {
		return nil, 0, fmt.Errorf("invalid reference: %q", s)
	}

	number, err := strconv.Atoi(m[3])
	if err != nil {
		return nil, 0, fmt.Errorf("invalid reference: %q", number)
	}

	owner := m[1]
	repo := m[2]


Review comment (suggestion):
💭 Calling this `rest` here and in the tests has a been of terminology overloading in my head (REST & rest) 🤪 

Maybe it's just me (ignore if you disagree because I don't feel strongly about this), but I figured I'd mention some alternative ideas anyhow:

- `remaining`
- `components`
- `tail`
- `trailer`
- `slugs`

More verbose options

- `remainingPathComponents`
- `tailComponents`
- `trailerComponents`
- `trailingSlugs`

Code after:
// repo is not nil and will have non-empty hostname.
func ParseURL(prURL string) (ghrepo.Interface, int, string, error) {
	if prURL == "" {
		return nil, 0, "", fmt.Errorf("invalid URL: %q", prURL)
	}

	u, err := url.Parse(prURL)
	if err != nil {
		return nil, 0, "", err
	}

	if u.Scheme != "https" && u.Scheme != "http" {
		return nil, 0, "", fmt.Errorf("invalid scheme: %s", u.Scheme)
	}

	m := pullURLRE.FindStringSubmatch(u.Path)
	if m == nil {
		return nil, 0, "", fmt.Errorf("not a pull request URL: %s", prURL)
	}

	repo := ghrepo.NewWithHost(m[1], m[2], u.Hostname())
	prNumber, _ := strconv.Atoi(m[3])
	tail := m[4]
	return repo, prNumber, tail, nil
}

var fullReferenceRE = regexp.MustCompile(`^(?:([^/]+)/([^/]+))#(\d+)$`)

// ParseFullReference parses a short issue/pull request reference of the form
// "owner/repo#number", where owner, repo and number are all required.
func ParseFullReference(s string) (ghrepo.Interface, int, error) {
	if s == "" {
		return nil, 0, errors.New("empty reference")
	}

	m := fullReferenceRE.FindStringSubmatch(s)
	if m == nil {
		return nil, 0, fmt.Errorf("invalid reference: %q", s)
	}

	number, err := strconv.Atoi(m[3])
	if err != nil {
		return nil, 0, fmt.Errorf("invalid reference: %q", number)
	}

	owner := m[1]
	repo := m[2]

	return ghrepo.New(owner, repo), number, nil
}