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.
This commit is contained in:
fluzz 2023-07-26 09:07:02 +02:00
parent cf787ad7fd
commit 1e4ff424d3
3 changed files with 53 additions and 9 deletions

View file

@ -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
}

View file

@ -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)

View file

@ -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",