| 106 | } |
| 107 | |
| 108 | func viewRun(opts *ViewOptions) error { |
| 109 | findOptions := shared.FindOptions{ |
| 110 | Selector: opts.SelectorArg, |
| 111 | Fields: defaultFields, |
| 112 | Detector: opts.Detector, |
| 113 | } |
| 114 | if opts.BrowserMode { |
| 115 | findOptions.Fields = []string{"url"} |
| 116 | } else if opts.Exporter != nil { |
| 117 | findOptions.Fields = opts.Exporter.Fields() |
| 118 | // The agent hint is skipped for merged or closed pull requests, which needs the state even when it was not |
| 119 | // requested. The exporter still writes only the requested fields. |
| 120 | if opts.InvokingAgent != "" && !slices.Contains(findOptions.Fields, "state") { |
| 121 | findOptions.Fields = slices.Concat(findOptions.Fields, []string{"state"}) |
| 122 | } |
| 123 | } |
| 124 | pr, baseRepo, err := opts.Finder.Find(findOptions) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | |
| 129 | connectedToTerminal := opts.IO.IsStdoutTTY() |
| 130 | |
| 131 | if opts.BrowserMode { |
| 132 | openURL := pr.URL |
| 133 | if connectedToTerminal { |
| 134 | fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", text.DisplayURL(openURL)) |
| 135 | } |
| 136 | return opts.Browser.Browse(openURL) |
| 137 | } |
| 138 | |
| 139 | printEventListenHint(opts, baseRepo, pr) |
| 140 | |
| 141 | opts.IO.DetectTerminalTheme() |
| 142 | if err := opts.IO.StartPager(); err == nil { |
| 143 | defer opts.IO.StopPager() |
| 144 | } else { |
| 145 | fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err) |
| 146 | } |
| 147 | |
| 148 | if opts.Exporter != nil { |
| 149 | return opts.Exporter.Write(opts.IO, pr) |
| 150 | } |
| 151 | |
| 152 | if connectedToTerminal { |
| 153 | return printHumanPrPreview(opts, baseRepo, pr) |
| 154 | } |
| 155 | |
| 156 | if opts.Comments { |
| 157 | fmt.Fprint(opts.IO.Out, shared.RawCommentList(pr.Comments, pr.DisplayableReviews())) |
| 158 | return nil |
| 159 | } |
| 160 | |
| 161 | return printRawPrPreview(opts.IO, pr) |
| 162 | } |
| 163 | |
| 164 | func printRawPrPreview(io *iostreams.IOStreams, pr *api.PullRequest) error { |
| 165 | out := io.Out |