Repository: cli/cli
File: pkg/cmd/agent-task/view/view.go (Go)

Code before:

			if repo.RepoHost() != ghinstance.Default() {
				return fmt.Errorf("agent tasks are not supported on this host: %s", repo.RepoHost())
			}

			databaseID, err := strconv.ParseInt(pr.FullDatabaseID, 10, 64)
			if err != nil {
				return fmt.Errorf("failed to parse pull request: %w", err)
			}

			resourceID = databaseID
		}

		// TODO(babakks): currently we just fetch a pre-defined number of
		// matching sessions to avoid hitting the API too many times, but it's
		// technically possible for a PR to be associated with lots of sessions
		// (i.e. above our selected limit).
		sessions, err := capiClient.ListSessionsByResourceID(ctx, "pull", resourceID, defaultLimit)
		if err != nil {
			return fmt.Errorf("failed to list sessions for pull request: %w", err)
		}

		if len(sessions) == 0 {
			fmt.Fprintln(opts.IO.ErrOut, "no session found for pull request")
			return cmdutil.SilentError
		}

		session = sessions[0]
		if len(sessions) > 1 {
			now := time.Now()
			options := make([]string, 0, len(sessions))
			for _, session := range sessions {
				options = append(options, fmt.Sprintf(
					"%s %s • %s",
					shared.SessionSymbol(cs, session.State),
					session.Name,
					text.FuzzyAgo(now, session.CreatedAt),
				))
			}

			opts.IO.StopProgressIndicator()
			selected, err := opts.Prompter.Select("Select a session", "", options)
			if err != nil {
				return err
			}

			session = sessions[selected]
		}
	}

	opts.IO.StopProgressIndicator()

Review comment (suggestion):
💭 I wonder if this `resourceID` should be named `prID` since the func we're calling is `GetPullRequestDatabaseID` - we know it's a PR at this point.

Code after:
			return opts.Browser.Browse(webURL)
		}

		session = sess
	} else {
		var resourceID int64
		var prURL string

		if opts.SelectorArg != "" {
			// Finder does not support the PR/issue reference format (e.g. owner/repo#123)
			// so we need to check if the selector arg is a reference and fetch the PR
			// directly.
			if repo, num, err := prShared.ParseFullReference(opts.SelectorArg); err == nil {
				// Since the selector was a reference (i.e. without hostname data), we need to
				// check the base repo to get the hostname.
				baseRepo, err := opts.BaseRepo()
				if err != nil {
					return err
				}

				hostname := baseRepo.RepoHost()
				if hostname != ghinstance.Default() {
					return fmt.Errorf("agent tasks are not supported on this host: %s", hostname)
				}

				resourceID, prURL, err = capiClient.GetPullRequestDatabaseID(ctx, hostname, repo.RepoOwner(), repo.RepoName(), num)
				if err != nil {
					return fmt.Errorf("failed to fetch pull request: %w", err)
				}
			}
		}

		if resourceID == 0 {
			findOptions := prShared.FindOptions{
				Selector: opts.SelectorArg,
				Fields:   []string{"id", "url", "fullDatabaseId"},
			}

			pr, repo, err := opts.Finder.Find(findOptions)
			if err != nil {
				return err
			}

			if repo.RepoHost() != ghinstance.Default() {
				return fmt.Errorf("agent tasks are not supported on this host: %s", repo.RepoHost())
			}

			databaseID, err := strconv.ParseInt(pr.FullDatabaseID, 10, 64)
			if err != nil {
				return fmt.Errorf("failed to parse pull request: %w", err)
			}