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