| 149 | } |
| 150 | |
| 151 | func CommentableRun(opts *CommentableOptions) error { |
| 152 | commentable, repo, err := opts.RetrieveCommentable() |
| 153 | if err != nil { |
| 154 | return err |
| 155 | } |
| 156 | opts.Host = repo.RepoHost() |
| 157 | if opts.DeleteLast { |
| 158 | return deleteComment(commentable, opts) |
| 159 | } |
| 160 | |
| 161 | // Guarded on the assets, because createComment and updateComment return |
| 162 | // early on the web path. An unguarded check here would fail a run that |
| 163 | // attached nothing. |
| 164 | var uploader *attachments.Uploader |
| 165 | if len(opts.Assets) > 0 { |
| 166 | httpClient, err := opts.HttpClient() |
| 167 | if err != nil { |
| 168 | return err |
| 169 | } |
| 170 | cfg, err := opts.Config() |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | // Asked of opts.Host, so the answer is about the host the commentable |
| 175 | // was actually fetched from. |
| 176 | tokenType := cfg.Authentication().ActiveTokenType(opts.Host) |
| 177 | uploader, err = attachments.NewUploader(httpClient, tokenType, opts.Host, commentable.RepositoryDatabaseID(), commentable.RepositoryViewerPermission()) |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Create new comment, bail before complexities of updating the last comment |
| 184 | if !opts.EditLast { |
| 185 | return createComment(commentable, opts, uploader) |
| 186 | } |
| 187 | |
| 188 | // Update the last comment, handling success or unexpected errors accordingly |
| 189 | err = updateComment(commentable, opts, uploader) |
| 190 | if err == nil { |
| 191 | return nil |
| 192 | } |
| 193 | if !errors.Is(err, errNoUserComments) { |
| 194 | return err |
| 195 | } |
| 196 | |
| 197 | // Determine whether to create new comment, prompt user if interactive and missing option |
| 198 | if !opts.CreateIfNone && opts.Interactive { |
| 199 | opts.CreateIfNone, err = opts.ConfirmCreateIfNoneSurvey() |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | } |
| 204 | if !opts.CreateIfNone { |
| 205 | return errNoUserComments |
| 206 | } |
| 207 | |
| 208 | // Create new comment because updating the last comment failed due to no user comments |