From 1e4ff424d39db7a4256cd9abf9c58b8d3e1b5c14 Mon Sep 17 00:00:00 2001 From: fluzz Date: Wed, 26 Jul 2023 09:07:02 +0200 Subject: [PATCH] Add an updated_at field to the API call for comment's attachment creation The update date is applied to the comment, and is set as the asset creation date. --- models/issues/comment.go | 18 +++++++-- .../api/v1/repo/issue_comment_attachment.go | 37 ++++++++++++++++--- templates/swagger/v1_json.tmpl | 7 ++++ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index 0b46f40589..a4b5dec5b7 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -1107,13 +1107,23 @@ func UpdateComment(c *Comment, doer *user_model.User) error { } defer committer.Close() sess := db.GetEngine(ctx).ID(c.ID).AllCols() - if c.Issue.NoAutoTime { - c.UpdatedUnix = c.Issue.UpdatedUnix - sess = sess.NoAutoTime() - } if _, err := sess.Update(c); err != nil { return err } + if c.Issue.NoAutoTime { + // AllCols().Update() does not change the "update_unix" field + // even if NoAutoTime is set. + // So, we need to commit the former pending Update() and + // then call an other Update() specifically to set "updated_unix". + if err := committer.Commit(); err != nil { + return fmt.Errorf("Commit: %w", err) + } + c.UpdatedUnix = c.Issue.UpdatedUnix + sess := db.GetEngine(ctx).ID(c.ID).Cols("updated_unix").NoAutoTime() + if _, err := sess.Update(c); err != nil { + return err + } + } if err := c.LoadIssue(ctx); err != nil { return err } diff --git a/routers/api/v1/repo/issue_comment_attachment.go b/routers/api/v1/repo/issue_comment_attachment.go index 121e3f10e0..10ae4f9167 100644 --- a/routers/api/v1/repo/issue_comment_attachment.go +++ b/routers/api/v1/repo/issue_comment_attachment.go @@ -5,6 +5,7 @@ package repo import ( "net/http" + "time" issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" @@ -144,6 +145,11 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) { // description: name of the attachment // type: string // required: false + // - name: updated_at + // in: query + // description: time of the attachment's creation. This is a timestamp in RFC 3339 format + // type: string + // format: date-time // - name: attachment // in: formData // description: attachment to upload @@ -167,6 +173,25 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) { return } + updatedAt := ctx.Req.FormValue("updated_at") + if len(updatedAt) != 0 { + updated, err := time.Parse(time.RFC3339, updatedAt) + if err != nil { + ctx.Error(http.StatusInternalServerError, "time.Parse", err) + return + } + err = comment.LoadIssue(ctx) + if err != nil { + ctx.Error(http.StatusInternalServerError, "LoadIssue", err) + return + } + err = issue_service.SetIssueUpdateDate(ctx, comment.Issue, &updated, ctx.Doer) + if err != nil { + ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err) + return + } + } + // Get uploaded file from request file, header, err := ctx.Req.FormFile("attachment") if err != nil { @@ -181,11 +206,13 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) { } attachment, err := attachment.UploadAttachment(file, setting.Attachment.AllowedTypes, header.Size, &repo_model.Attachment{ - Name: filename, - UploaderID: ctx.Doer.ID, - RepoID: ctx.Repo.Repository.ID, - IssueID: comment.IssueID, - CommentID: comment.ID, + Name: filename, + UploaderID: ctx.Doer.ID, + RepoID: ctx.Repo.Repository.ID, + IssueID: comment.IssueID, + CommentID: comment.ID, + NoAutoTime: comment.Issue.NoAutoTime, + CreatedUnix: comment.Issue.UpdatedUnix, }) if err != nil { ctx.Error(http.StatusInternalServerError, "UploadAttachment", err) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index ca7672be19..ac84ddd922 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -6120,6 +6120,13 @@ "name": "name", "in": "query" }, + { + "type": "string", + "format": "date-time", + "description": "time of the attachment's creation. This is a timestamp in RFC 3339 format", + "name": "updated_at", + "in": "query" + }, { "type": "file", "description": "attachment to upload",