loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

[FEAT] allow setting the update date on issues and comments

This field adds the possibility to set the update date when modifying
an issue through the API.

A 'NoAutoDate' in-memory field is added in the Issue struct.
If the update_at field is set, NoAutoDate is set to true and the
Issue's UpdatedUnix field is filled.

That information is passed down to the functions that actually updates
the database, which have been modified to not auto update dates if
requested.

A guard is added to the 'EditIssue' API call, to checks that the
udpate_at date is between the issue's creation date and the current
date (to avoid 'malicious' changes). It also limits the new feature
to project's owners and admins.

(cherry picked from commit c524d33402c76bc4cccea2806f289e08a009baae)

Add a SetIssueUpdateDate() function in services/issue.go

That function is used by some API calls to set the NoAutoDate and
UpdatedUnix fields of an Issue if an updated_at date is provided.

(cherry picked from commit f061caa6555e0c9e922ee1e73dd2e4337360e9fe)

Add an updated_at field to the API calls related to Issue's Labels.

The update date is applied to the issue's comment created to inform
about the modification of the issue's labels.

(cherry picked from commit ea36cf80f58f0ab20c565a8f5d063b90fd741f97)

Add an updated_at field to the API call for issue's attachment creation

The update date is applied to the issue's comment created to inform
about the modification of the issue's content, and is set as the
asset creation date.

(cherry picked from commit 96150971ca31b97e97e84d5f5eb95a177cc44e2e)

Checking Issue changes, with and without providing an updated_at date

Those unit tests are added:

- TestAPIEditIssueWithAutoDate
- TestAPIEditIssueWithNoAutoDate

- TestAPIAddIssueLabelsWithAutoDate
- TestAPIAddIssueLabelsWithNoAutoDate

- TestAPICreateIssueAttachmentWithAutoDate
- TestAPICreateIssueAttachmentWithNoAutoDate

(cherry picked from commit 4926a5d7a28581003545256632213bf4136b193d)

Add an updated_at field to the API call for issue's comment creation

The update date is used as the comment creation date, and is applied to
the issue as the update creation date.

(cherry picked from commit 76c8faecdc6cba48ca4fe07d1a916d1f1a4b37b4)

Add an updated_at field to the API call for issue's comment edition

The update date is used as the comment update date, and is applied to
the issue as an update date.

(cherry picked from commit cf787ad7fdb8e6273fdc35d7b5cc164b400207e9)

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.

(cherry picked from commit 1e4ff424d39db7a4256cd9abf9c58b8d3e1b5c14)

Checking Comment changes, with and without providing an updated_at date

Those unit tests are added:

- TestAPICreateCommentWithAutoDate
- TestAPICreateCommentWithNoAutoDate

- TestAPIEditCommentWithAutoDate
- TestAPIEditCommentWithNoAutoDate

- TestAPICreateCommentAttachmentWithAutoDate
- TestAPICreateCommentAttachmentWithNoAutoDate

(cherry picked from commit da932152f1deb3039a399516a51c8b6757059c91)

Pettier code to set the update time of comments

Now uses sess.AllCols().NoAutoToime().SetExpr("updated_unix", ...)

XORM is smart enough to compose one single SQL UPDATE which all
columns + updated_unix.

(cherry picked from commit 1f6a42808dd739c0c2e49e6b7ae2967f120f43c2)

Issue edition: Keep the max of the milestone and issue update dates.

When editing an issue via the API, an updated_at date can be provided.
If the EditIssue call changes the issue's milestone, the milestone's
update date is to be changed accordingly, but only with a greater
value.

This ensures that a milestone's update date is the max of all issue's
update dates.

(cherry picked from commit 8f22ea182e6b49e933dc6534040160dd739ff18a)

Rewrite the 'AutoDate' tests using subtests

Also add a test to check the permissions to set a date, and a test
to check update dates on milestones.

The tests related to 'AutoDate' are:
- TestAPIEditIssueAutoDate
- TestAPIAddIssueLabelsAutoDate
- TestAPIEditIssueMilestoneAutoDate
- TestAPICreateIssueAttachmentAutoDate
- TestAPICreateCommentAutoDate
- TestAPIEditCommentWithDate
- TestAPICreateCommentAttachmentAutoDate

(cherry picked from commit 961fd13c551b3e50040acb7c914a00ead92de63f)
(cherry picked from commit d52f4eea44692ee773010cb66a69a603663947d5)
(cherry picked from commit 3540ea2a43155ca8cf5ab1a4a246babfb829db16)

Conflicts:
services/issue/issue.go
https://codeberg.org/forgejo/forgejo/pulls/1415
(cherry picked from commit 56720ade008c09122d825959171aa5346d645987)

Conflicts:
routers/api/v1/repo/issue_label.go
https://codeberg.org/forgejo/forgejo/pulls/1462
(cherry picked from commit 47c78927d6c7e7a50298fa67efad1e73723a0981)
(cherry picked from commit 2030f3b965cde401976821083c3250b404954ecc)
(cherry picked from commit f02aeb76981cd688ceaf6613f142a8a725be1437)

Conflicts:
routers/api/v1/repo/issue_attachment.go
routers/api/v1/repo/issue_comment_attachment.go
https://codeberg.org/forgejo/forgejo/pulls/1575
(cherry picked from commit d072525b35e44faf7ff87143c0e52b8ba8a625c8)
(cherry picked from commit 8424d0ab3df75ac3ffa30f42d398e22995ada5e7)
(cherry picked from commit 5cc62caec788b54afd9da5b9193ce06ee8ec562b)
(cherry picked from commit d6300d5dcd01c7ddc65d8b0f326f9c19cb53b58e)

[FEAT] allow setting the update date on issues and comments (squash) apply the 'update_at' value to the cross-ref comments (#1676)

[this is a follow-up to PR #764]

When a comment of issue A referencing issue B is added with a forced 'updated_at' date, that date has to be applied to the comment created in issue B.

-----

Comment:

While trying my 'RoundUp migration script', I found that this case was forgotten in PR #764 - my apologies...

I'll try to write a functional test, base on models/issues/issue_xref_test.go

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/1676
Co-authored-by: fluzz <fluzz@freedroid.org>
Co-committed-by: fluzz <fluzz@freedroid.org>
(cherry picked from commit ac4f727f63a2dd746dd84a31ebf7f70d5b5d7c52)
(cherry picked from commit 5110476ee9010ba8cdca0e0f37f765f8800e9fe1)
(cherry picked from commit 77ba6be1dab4f6f3678d79a394da56e6447ebbe1)
(cherry picked from commit 9c8337b5c442cfd72d97597c2089e776f42828b7)
(cherry picked from commit 1d689eb686f0f7df09c7861b3faf9d8683cb933b)
(cherry picked from commit 511c519c875a4c4e65c02ef0c4e3b941f4da4371)
(cherry picked from commit 2f0b4a8f610837d34844bb79cda1360ab23b6b1c)
(cherry picked from commit fdd4da111c449322901a0acf6d0857eac4716581)

[FEAT] allow setting the update date on issues and comments (squash) do not use token= query param

See https://codeberg.org/forgejo/forgejo/commit/33439b733a

(cherry picked from commit c5139a75b9e4af612a628171bd4f63a24860c272)
(cherry picked from commit c7b572c35d3e9e22017fd74045bcdc1109bd06df)
(cherry picked from commit aec7503ff6dd177980f3d9f367122ffc2fec8986)
(cherry picked from commit 87c65f2a490faeccb85088fa0981dd50f7199eb8)
(cherry picked from commit bd47ee33c20e53ae616a7e53d63c3b51809585fb)
(cherry picked from commit f3dbd90a747c14fb1b5b4271db6c10abbf86d586)

+907 -37
+20 -3
models/issues/comment.go
··· 823 823 IsForcePush: opts.IsForcePush, 824 824 Invalidated: opts.Invalidated, 825 825 } 826 + if opts.Issue.NoAutoTime { 827 + // Preload the comment with the Issue containing the forced update 828 + // date. This is needed to propagate those data in AddCrossReferences() 829 + comment.Issue = opts.Issue 830 + comment.CreatedUnix = opts.Issue.UpdatedUnix 831 + comment.UpdatedUnix = opts.Issue.UpdatedUnix 832 + e.NoAutoTime() 833 + } 826 834 if _, err = e.Insert(comment); err != nil { 827 835 return nil, err 828 836 } ··· 1105 1113 return err 1106 1114 } 1107 1115 defer committer.Close() 1108 - sess := db.GetEngine(ctx) 1109 1116 1110 - if _, err := sess.ID(c.ID).AllCols().Update(c); err != nil { 1117 + if err := c.LoadIssue(ctx); err != nil { 1111 1118 return err 1112 1119 } 1113 - if err := c.LoadIssue(ctx); err != nil { 1120 + 1121 + sess := db.GetEngine(ctx).ID(c.ID).AllCols() 1122 + if c.Issue.NoAutoTime { 1123 + // update the DataBase 1124 + sess = sess.NoAutoTime().SetExpr("updated_unix", c.Issue.UpdatedUnix) 1125 + // the UpdatedUnix value of the Comment also has to be set, 1126 + // to return the adequate value 1127 + // see https://codeberg.org/forgejo/forgejo/pulls/764#issuecomment-1023801 1128 + c.UpdatedUnix = c.Issue.UpdatedUnix 1129 + } 1130 + if _, err := sess.Update(c); err != nil { 1114 1131 return err 1115 1132 } 1116 1133 if err := c.AddCrossReferences(ctx, doer, true); err != nil {
+1
models/issues/issue.go
··· 126 126 CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` 127 127 UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` 128 128 ClosedUnix timeutil.TimeStamp `xorm:"INDEX"` 129 + NoAutoTime bool `xorm:"-"` 129 130 130 131 Attachments []*repo_model.Attachment `xorm:"-"` 131 132 Comments CommentList `xorm:"-"`
+32 -10
models/issues/issue_update.go
··· 27 27 28 28 // UpdateIssueCols updates cols of issue 29 29 func UpdateIssueCols(ctx context.Context, issue *Issue, cols ...string) error { 30 - if _, err := db.GetEngine(ctx).ID(issue.ID).Cols(cols...).Update(issue); err != nil { 30 + sess := db.GetEngine(ctx).ID(issue.ID) 31 + if issue.NoAutoTime { 32 + cols = append(cols, []string{"updated_unix"}...) 33 + sess.NoAutoTime() 34 + } 35 + if _, err := sess.Cols(cols...).Update(issue); err != nil { 31 36 return err 32 37 } 33 38 return nil ··· 71 76 } 72 77 73 78 if issue.IsClosed { 74 - issue.ClosedUnix = timeutil.TimeStampNow() 79 + if issue.NoAutoTime { 80 + issue.ClosedUnix = issue.UpdatedUnix 81 + } else { 82 + issue.ClosedUnix = timeutil.TimeStampNow() 83 + } 75 84 } else { 76 85 issue.ClosedUnix = 0 77 86 } ··· 92 101 93 102 // Update issue count of milestone 94 103 if issue.MilestoneID > 0 { 95 - if err := UpdateMilestoneCounters(ctx, issue.MilestoneID); err != nil { 96 - return nil, err 104 + if issue.NoAutoTime { 105 + if err := UpdateMilestoneCountersWithDate(ctx, issue.MilestoneID, issue.UpdatedUnix); err != nil { 106 + return nil, err 107 + } 108 + } else { 109 + if err := UpdateMilestoneCounters(ctx, issue.MilestoneID); err != nil { 110 + return nil, err 111 + } 97 112 } 98 113 } 99 114 ··· 259 274 return fmt.Errorf("UpdateIssueCols: %w", err) 260 275 } 261 276 277 + historyDate := timeutil.TimeStampNow() 278 + if issue.NoAutoTime { 279 + historyDate = issue.UpdatedUnix 280 + } 262 281 if err = SaveIssueContentHistory(ctx, doer.ID, issue.ID, 0, 263 - timeutil.TimeStampNow(), issue.Content, false); err != nil { 282 + historyDate, issue.Content, false); err != nil { 264 283 return fmt.Errorf("SaveIssueContentHistory: %w", err) 265 284 } 266 285 ··· 449 468 return nil, false, err 450 469 } 451 470 452 - if _, err := db.GetEngine(ctx).ID(issue.ID).Cols( 453 - "name", "content", "milestone_id", "priority", 454 - "deadline_unix", "updated_unix", "is_locked"). 455 - Update(issue); err != nil { 471 + sess := db.GetEngine(ctx).ID(issue.ID) 472 + cols := []string{"name", "content", "milestone_id", "priority", "deadline_unix", "is_locked"} 473 + if issue.NoAutoTime { 474 + cols = append(cols, "updated_unix") 475 + sess.NoAutoTime() 476 + } 477 + if _, err := sess.Cols(cols...).Update(issue); err != nil { 456 478 return nil, false, err 457 479 } 458 480 ··· 498 520 defer committer.Close() 499 521 500 522 // Update the deadline 501 - if err = UpdateIssueCols(ctx, &Issue{ID: issue.ID, DeadlineUnix: deadlineUnix}, "deadline_unix"); err != nil { 523 + if err = UpdateIssueCols(ctx, &Issue{ID: issue.ID, DeadlineUnix: deadlineUnix, NoAutoTime: issue.NoAutoTime, UpdatedUnix: issue.UpdatedUnix}, "deadline_unix"); err != nil { 502 524 return err 503 525 } 504 526
+12 -4
models/issues/issue_xref.go
··· 46 46 for i, c := range active { 47 47 ids[i] = c.ID 48 48 } 49 - return neuterCrossReferencesIds(ctx, ids) 49 + return neuterCrossReferencesIds(ctx, nil, ids) 50 50 } 51 51 52 - func neuterCrossReferencesIds(ctx context.Context, ids []int64) error { 53 - _, err := db.GetEngine(ctx).In("id", ids).Cols("`ref_action`").Update(&Comment{RefAction: references.XRefActionNeutered}) 52 + func neuterCrossReferencesIds(stdCtx context.Context, ctx *crossReferencesContext, ids []int64) error { 53 + sess := db.GetEngine(stdCtx).In("id", ids).Cols("`ref_action`") 54 + if ctx != nil && ctx.OrigIssue.NoAutoTime { 55 + sess.SetExpr("updated_unix", ctx.OrigIssue.UpdatedUnix).NoAutoTime() 56 + } 57 + _, err := sess.Update(&Comment{RefAction: references.XRefActionNeutered}) 54 58 return err 55 59 } 56 60 ··· 100 104 } 101 105 } 102 106 if len(ids) > 0 { 103 - if err = neuterCrossReferencesIds(stdCtx, ids); err != nil { 107 + if err = neuterCrossReferencesIds(stdCtx, ctx, ids); err != nil { 104 108 return err 105 109 } 106 110 } ··· 109 113 var refCommentID int64 110 114 if ctx.OrigComment != nil { 111 115 refCommentID = ctx.OrigComment.ID 116 + } 117 + if ctx.OrigIssue.NoAutoTime { 118 + xref.Issue.NoAutoTime = true 119 + xref.Issue.UpdatedUnix = ctx.OrigIssue.UpdatedUnix 112 120 } 113 121 opts := &CreateCommentOptions{ 114 122 Type: ctx.Type,
+17 -5
models/issues/milestone.go
··· 187 187 return UpdateMilestoneCounters(ctx, m.ID) 188 188 } 189 189 190 - // UpdateMilestoneCounters calculates NumIssues, NumClosesIssues and Completeness 191 - func UpdateMilestoneCounters(ctx context.Context, id int64) error { 190 + func updateMilestoneCounters(ctx context.Context, id int64, noAutoTime bool, updatedUnix timeutil.TimeStamp) error { 192 191 e := db.GetEngine(ctx) 193 - _, err := e.ID(id). 192 + sess := e.ID(id). 194 193 SetExpr("num_issues", builder.Select("count(*)").From("issue").Where( 195 194 builder.Eq{"milestone_id": id}, 196 195 )). ··· 199 198 "milestone_id": id, 200 199 "is_closed": true, 201 200 }, 202 - )). 203 - Update(&Milestone{}) 201 + )) 202 + if noAutoTime { 203 + sess.SetExpr("updated_unix", updatedUnix).NoAutoTime() 204 + } 205 + _, err := sess.Update(&Milestone{}) 204 206 if err != nil { 205 207 return err 206 208 } ··· 208 210 id, 209 211 ) 210 212 return err 213 + } 214 + 215 + // UpdateMilestoneCounters calculates NumIssues, NumClosesIssues and Completeness 216 + func UpdateMilestoneCounters(ctx context.Context, id int64) error { 217 + return updateMilestoneCounters(ctx, id, false, 0) 218 + } 219 + 220 + // UpdateMilestoneCountersWithDate calculates NumIssues, NumClosesIssues and Completeness and set the UpdatedUnix date 221 + func UpdateMilestoneCountersWithDate(ctx context.Context, id int64, updatedUnix timeutil.TimeStamp) error { 222 + return updateMilestoneCounters(ctx, id, true, updatedUnix) 211 223 } 212 224 213 225 // ChangeMilestoneStatusByRepoIDAndID changes a milestone open/closed status if the milestone ID is in the repo.
+1
models/repo/attachment.go
··· 28 28 Name string 29 29 DownloadCount int64 `xorm:"DEFAULT 0"` 30 30 Size int64 `xorm:"DEFAULT 0"` 31 + NoAutoTime bool `xorm:"-"` 31 32 CreatedUnix timeutil.TimeStamp `xorm:"created"` 32 33 CustomDownloadURL string `xorm:"-"` 33 34 }
+2
modules/structs/issue.go
··· 111 111 // swagger:strfmt date-time 112 112 Deadline *time.Time `json:"due_date"` 113 113 RemoveDeadline *bool `json:"unset_due_date"` 114 + // swagger:strfmt date-time 115 + Updated *time.Time `json:"updated_at"` 114 116 } 115 117 116 118 // EditDeadlineOption options for creating a deadline
+4
modules/structs/issue_comment.go
··· 28 28 type CreateIssueCommentOption struct { 29 29 // required:true 30 30 Body string `json:"body" binding:"Required"` 31 + // swagger:strfmt date-time 32 + Updated *time.Time `json:"updated_at"` 31 33 } 32 34 33 35 // EditIssueCommentOption options for editing a comment 34 36 type EditIssueCommentOption struct { 35 37 // required: true 36 38 Body string `json:"body" binding:"Required"` 39 + // swagger:strfmt date-time 40 + Updated *time.Time `json:"updated_at"` 37 41 } 38 42 39 43 // TimelineComment represents a timeline comment (comment of any type) on a commit or issue
+12
modules/structs/issue_label.go
··· 4 4 5 5 package structs 6 6 7 + import ( 8 + "time" 9 + ) 10 + 7 11 // Label a label to an issue or a pr 8 12 // swagger:model 9 13 type Label struct { ··· 45 49 IsArchived *bool `json:"is_archived"` 46 50 } 47 51 52 + // DeleteLabelOption options for deleting a label 53 + type DeleteLabelsOption struct { 54 + // swagger:strfmt date-time 55 + Updated *time.Time `json:"updated_at"` 56 + } 57 + 48 58 // IssueLabelsOption a collection of labels 49 59 type IssueLabelsOption struct { 50 60 // list of label IDs 51 61 Labels []int64 `json:"labels"` 62 + // swagger:strfmt date-time 63 + Updated *time.Time `json:"updated_at"` 52 64 } 53 65 54 66 // LabelTemplate info of a Label template
+2 -2
routers/api/v1/api.go
··· 1337 1337 m.Combo("").Get(repo.ListIssueLabels). 1338 1338 Post(reqToken(), bind(api.IssueLabelsOption{}), repo.AddIssueLabels). 1339 1339 Put(reqToken(), bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels). 1340 - Delete(reqToken(), repo.ClearIssueLabels) 1341 - m.Delete("/{id}", reqToken(), repo.DeleteIssueLabel) 1340 + Delete(reqToken(), bind(api.DeleteLabelsOption{}), repo.ClearIssueLabels) 1341 + m.Delete("/{id}", reqToken(), bind(api.DeleteLabelsOption{}), repo.DeleteIssueLabel) 1342 1342 }) 1343 1343 m.Group("/times", func() { 1344 1344 m.Combo("").
+6
routers/api/v1/repo/issue.go
··· 802 802 return 803 803 } 804 804 805 + err = issue_service.SetIssueUpdateDate(ctx, issue, form.Updated, ctx.Doer) 806 + if err != nil { 807 + ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err) 808 + return 809 + } 810 + 805 811 oldTitle := issue.Title 806 812 if len(form.Title) > 0 { 807 813 issue.Title = form.Title
+26 -4
routers/api/v1/repo/issue_attachment.go
··· 5 5 6 6 import ( 7 7 "net/http" 8 + "time" 8 9 9 10 issues_model "code.gitea.io/gitea/models/issues" 10 11 repo_model "code.gitea.io/gitea/models/repo" ··· 141 142 // description: name of the attachment 142 143 // type: string 143 144 // required: false 145 + // - name: updated_at 146 + // in: query 147 + // description: time of the attachment's creation. This is a timestamp in RFC 3339 format 148 + // type: string 149 + // format: date-time 144 150 // - name: attachment 145 151 // in: formData 146 152 // description: attachment to upload ··· 165 171 return 166 172 } 167 173 174 + updatedAt := ctx.Req.FormValue("updated_at") 175 + if len(updatedAt) != 0 { 176 + updated, err := time.Parse(time.RFC3339, updatedAt) 177 + if err != nil { 178 + ctx.Error(http.StatusInternalServerError, "time.Parse", err) 179 + return 180 + } 181 + err = issue_service.SetIssueUpdateDate(ctx, issue, &updated, ctx.Doer) 182 + if err != nil { 183 + ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err) 184 + return 185 + } 186 + } 187 + 168 188 // Get uploaded file from request 169 189 file, header, err := ctx.Req.FormFile("attachment") 170 190 if err != nil { ··· 179 199 } 180 200 181 201 attachment, err := attachment.UploadAttachment(ctx, file, setting.Attachment.AllowedTypes, header.Size, &repo_model.Attachment{ 182 - Name: filename, 183 - UploaderID: ctx.Doer.ID, 184 - RepoID: ctx.Repo.Repository.ID, 185 - IssueID: issue.ID, 202 + Name: filename, 203 + UploaderID: ctx.Doer.ID, 204 + RepoID: ctx.Repo.Repository.ID, 205 + IssueID: issue.ID, 206 + NoAutoTime: issue.NoAutoTime, 207 + CreatedUnix: issue.UpdatedUnix, 186 208 }) 187 209 if err != nil { 188 210 ctx.Error(http.StatusInternalServerError, "UploadAttachment", err)
+17
routers/api/v1/repo/issue_comment.go
··· 399 399 return 400 400 } 401 401 402 + err = issue_service.SetIssueUpdateDate(ctx, issue, form.Updated, ctx.Doer) 403 + if err != nil { 404 + ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err) 405 + return 406 + } 407 + 402 408 comment, err := issue_service.CreateIssueComment(ctx, ctx.Doer, ctx.Repo.Repository, issue, form.Body, nil) 403 409 if err != nil { 404 410 ctx.Error(http.StatusInternalServerError, "CreateIssueComment", err) ··· 604 610 605 611 if !comment.Type.HasContentSupport() { 606 612 ctx.Status(http.StatusNoContent) 613 + return 614 + } 615 + 616 + err = comment.LoadIssue(ctx) 617 + if err != nil { 618 + ctx.Error(http.StatusInternalServerError, "LoadIssue", err) 619 + return 620 + } 621 + err = issue_service.SetIssueUpdateDate(ctx, comment.Issue, form.Updated, ctx.Doer) 622 + if err != nil { 623 + ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err) 607 624 return 608 625 } 609 626
+32 -5
routers/api/v1/repo/issue_comment_attachment.go
··· 5 5 6 6 import ( 7 7 "net/http" 8 + "time" 8 9 9 10 issues_model "code.gitea.io/gitea/models/issues" 10 11 repo_model "code.gitea.io/gitea/models/repo" ··· 144 145 // description: name of the attachment 145 146 // type: string 146 147 // required: false 148 + // - name: updated_at 149 + // in: query 150 + // description: time of the attachment's creation. This is a timestamp in RFC 3339 format 151 + // type: string 152 + // format: date-time 147 153 // - name: attachment 148 154 // in: formData 149 155 // description: attachment to upload ··· 169 175 return 170 176 } 171 177 178 + updatedAt := ctx.Req.FormValue("updated_at") 179 + if len(updatedAt) != 0 { 180 + updated, err := time.Parse(time.RFC3339, updatedAt) 181 + if err != nil { 182 + ctx.Error(http.StatusInternalServerError, "time.Parse", err) 183 + return 184 + } 185 + err = comment.LoadIssue(ctx) 186 + if err != nil { 187 + ctx.Error(http.StatusInternalServerError, "LoadIssue", err) 188 + return 189 + } 190 + err = issue_service.SetIssueUpdateDate(ctx, comment.Issue, &updated, ctx.Doer) 191 + if err != nil { 192 + ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err) 193 + return 194 + } 195 + } 196 + 172 197 // Get uploaded file from request 173 198 file, header, err := ctx.Req.FormFile("attachment") 174 199 if err != nil { ··· 183 208 } 184 209 185 210 attachment, err := attachment.UploadAttachment(ctx, file, setting.Attachment.AllowedTypes, header.Size, &repo_model.Attachment{ 186 - Name: filename, 187 - UploaderID: ctx.Doer.ID, 188 - RepoID: ctx.Repo.Repository.ID, 189 - IssueID: comment.IssueID, 190 - CommentID: comment.ID, 211 + Name: filename, 212 + UploaderID: ctx.Doer.ID, 213 + RepoID: ctx.Repo.Repository.ID, 214 + IssueID: comment.IssueID, 215 + CommentID: comment.ID, 216 + NoAutoTime: comment.Issue.NoAutoTime, 217 + CreatedUnix: comment.Issue.UpdatedUnix, 191 218 }) 192 219 if err != nil { 193 220 ctx.Error(http.StatusInternalServerError, "UploadAttachment", err)
+26
routers/api/v1/repo/issue_label.go
··· 151 151 // type: integer 152 152 // format: int64 153 153 // required: true 154 + // - name: body 155 + // in: body 156 + // schema: 157 + // "$ref": "#/definitions/DeleteLabelsOption" 154 158 // responses: 155 159 // "204": 156 160 // "$ref": "#/responses/empty" ··· 160 164 // "$ref": "#/responses/notFound" 161 165 // "422": 162 166 // "$ref": "#/responses/validationError" 167 + form := web.GetForm(ctx).(*api.DeleteLabelsOption) 163 168 164 169 issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) 165 170 if err != nil { ··· 173 178 174 179 if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) { 175 180 ctx.Status(http.StatusForbidden) 181 + return 182 + } 183 + 184 + if err := issue_service.SetIssueUpdateDate(ctx, issue, form.Updated, ctx.Doer); err != nil { 185 + ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err) 176 186 return 177 187 } 178 188 ··· 275 285 // type: integer 276 286 // format: int64 277 287 // required: true 288 + // - name: body 289 + // in: body 290 + // schema: 291 + // "$ref": "#/definitions/DeleteLabelsOption" 278 292 // responses: 279 293 // "204": 280 294 // "$ref": "#/responses/empty" ··· 282 296 // "$ref": "#/responses/forbidden" 283 297 // "404": 284 298 // "$ref": "#/responses/notFound" 299 + form := web.GetForm(ctx).(*api.DeleteLabelsOption) 285 300 286 301 issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) 287 302 if err != nil { ··· 298 313 return 299 314 } 300 315 316 + if err := issue_service.SetIssueUpdateDate(ctx, issue, form.Updated, ctx.Doer); err != nil { 317 + ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err) 318 + return 319 + } 320 + 301 321 if err := issue_service.ClearLabels(ctx, issue, ctx.Doer); err != nil { 302 322 ctx.Error(http.StatusInternalServerError, "ClearLabels", err) 303 323 return ··· 326 346 if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) { 327 347 ctx.Status(http.StatusForbidden) 328 348 return nil, nil, nil 349 + } 350 + 351 + err = issue_service.SetIssueUpdateDate(ctx, issue, form.Updated, ctx.Doer) 352 + if err != nil { 353 + ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err) 354 + return nil, nil, err 329 355 } 330 356 331 357 return issue, labels, err
+3
routers/api/v1/swagger/options.go
··· 48 48 IssueLabelsOption api.IssueLabelsOption 49 49 50 50 // in:body 51 + DeleteLabelsOption api.DeleteLabelsOption 52 + 53 + // in:body 51 54 CreateKeyOption api.CreateKeyOption 52 55 53 56 // in:body
+6 -1
services/attachment/attachment.go
··· 32 32 } 33 33 attach.Size = size 34 34 35 - return db.Insert(ctx, attach) 35 + eng := db.GetEngine(ctx) 36 + if attach.NoAutoTime { 37 + eng.NoAutoTime() 38 + } 39 + _, err = eng.Insert(attach) 40 + return err 36 41 }) 37 42 38 43 return attach, err
+5 -1
services/issue/comments.go
··· 89 89 } 90 90 91 91 if needsContentHistory { 92 - err := issues_model.SaveIssueContentHistory(ctx, doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false) 92 + historyDate := timeutil.TimeStampNow() 93 + if c.Issue.NoAutoTime { 94 + historyDate = c.Issue.UpdatedUnix 95 + } 96 + err := issues_model.SaveIssueContentHistory(ctx, doer.ID, c.IssueID, c.ID, historyDate, c.Content, false) 93 97 if err != nil { 94 98 return err 95 99 }
+39
services/issue/issue.go
··· 6 6 import ( 7 7 "context" 8 8 "fmt" 9 + "time" 9 10 10 11 activities_model "code.gitea.io/gitea/models/activities" 11 12 "code.gitea.io/gitea/models/db" ··· 17 18 user_model "code.gitea.io/gitea/models/user" 18 19 "code.gitea.io/gitea/modules/git" 19 20 "code.gitea.io/gitea/modules/storage" 21 + "code.gitea.io/gitea/modules/timeutil" 20 22 notify_service "code.gitea.io/gitea/services/notify" 21 23 ) 22 24 ··· 290 292 291 293 return committer.Commit() 292 294 } 295 + 296 + // Set the UpdatedUnix date and the NoAutoTime field of an Issue if a non 297 + // nil 'updated' time is provided 298 + // 299 + // In order to set a specific update time, the DB will be updated with 300 + // NoAutoTime(). A 'NoAutoTime' boolean field in the Issue struct is used to 301 + // propagate down to the DB update calls the will to apply autoupdate or not. 302 + func SetIssueUpdateDate(ctx context.Context, issue *issues_model.Issue, updated *time.Time, doer *user_model.User) error { 303 + issue.NoAutoTime = false 304 + if updated == nil { 305 + return nil 306 + } 307 + 308 + if err := issue.LoadRepo(ctx); err != nil { 309 + return err 310 + } 311 + 312 + // Check if the poster is allowed to set an update date 313 + perm, err := access_model.GetUserRepoPermission(ctx, issue.Repo, doer) 314 + if err != nil { 315 + return err 316 + } 317 + if !perm.IsAdmin() && !perm.IsOwner() { 318 + return fmt.Errorf("user needs to have admin or owner right") 319 + } 320 + 321 + // A simple guard against potential inconsistent calls 322 + updatedUnix := timeutil.TimeStamp(updated.Unix()) 323 + if updatedUnix < issue.CreatedUnix || updatedUnix > timeutil.TimeStampNow() { 324 + return fmt.Errorf("unallowed update date") 325 + } 326 + 327 + issue.UpdatedUnix = updatedUnix 328 + issue.NoAutoTime = true 329 + 330 + return nil 331 + }
+28 -2
services/issue/milestone.go
··· 13 13 notify_service "code.gitea.io/gitea/services/notify" 14 14 ) 15 15 16 + func updateMilestoneCounters(ctx context.Context, issue *issues_model.Issue, id int64) error { 17 + if issue.NoAutoTime { 18 + // We set the milestone's update date to the max of the 19 + // milestone and issue update dates. 20 + // Note: we can not call UpdateMilestoneCounters() if the 21 + // milestone's update date is to be kept, because that function 22 + // auto-updates the dates. 23 + milestone, err := issues_model.GetMilestoneByRepoID(ctx, issue.RepoID, id) 24 + if err != nil { 25 + return fmt.Errorf("GetMilestoneByRepoID: %w", err) 26 + } 27 + updatedUnix := milestone.UpdatedUnix 28 + if issue.UpdatedUnix > updatedUnix { 29 + updatedUnix = issue.UpdatedUnix 30 + } 31 + if err := issues_model.UpdateMilestoneCountersWithDate(ctx, id, updatedUnix); err != nil { 32 + return err 33 + } 34 + } else { 35 + if err := issues_model.UpdateMilestoneCounters(ctx, id); err != nil { 36 + return err 37 + } 38 + } 39 + return nil 40 + } 41 + 16 42 func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) error { 17 43 // Only check if milestone exists if we don't remove it. 18 44 if issue.MilestoneID > 0 { ··· 30 56 } 31 57 32 58 if oldMilestoneID > 0 { 33 - if err := issues_model.UpdateMilestoneCounters(ctx, oldMilestoneID); err != nil { 59 + if err := updateMilestoneCounters(ctx, issue, oldMilestoneID); err != nil { 34 60 return err 35 61 } 36 62 } 37 63 38 64 if issue.MilestoneID > 0 { 39 - if err := issues_model.UpdateMilestoneCounters(ctx, issue.MilestoneID); err != nil { 65 + if err := updateMilestoneCounters(ctx, issue, issue.MilestoneID); err != nil { 40 66 return err 41 67 } 42 68 }
+60
templates/swagger/v1_json.tmpl
··· 6532 6532 "in": "query" 6533 6533 }, 6534 6534 { 6535 + "type": "string", 6536 + "format": "date-time", 6537 + "description": "time of the attachment's creation. This is a timestamp in RFC 3339 format", 6538 + "name": "updated_at", 6539 + "in": "query" 6540 + }, 6541 + { 6535 6542 "type": "file", 6536 6543 "description": "attachment to upload", 6537 6544 "name": "attachment", ··· 7148 7155 "type": "string", 7149 7156 "description": "name of the attachment", 7150 7157 "name": "name", 7158 + "in": "query" 7159 + }, 7160 + { 7161 + "type": "string", 7162 + "format": "date-time", 7163 + "description": "time of the attachment's creation. This is a timestamp in RFC 3339 format", 7164 + "name": "updated_at", 7151 7165 "in": "query" 7152 7166 }, 7153 7167 { ··· 8125 8139 "name": "index", 8126 8140 "in": "path", 8127 8141 "required": true 8142 + }, 8143 + { 8144 + "name": "body", 8145 + "in": "body", 8146 + "schema": { 8147 + "$ref": "#/definitions/DeleteLabelsOption" 8148 + } 8128 8149 } 8129 8150 ], 8130 8151 "responses": { ··· 8180 8201 "name": "id", 8181 8202 "in": "path", 8182 8203 "required": true 8204 + }, 8205 + { 8206 + "name": "body", 8207 + "in": "body", 8208 + "schema": { 8209 + "$ref": "#/definitions/DeleteLabelsOption" 8210 + } 8183 8211 } 8184 8212 ], 8185 8213 "responses": { ··· 17924 17952 "body": { 17925 17953 "type": "string", 17926 17954 "x-go-name": "Body" 17955 + }, 17956 + "updated_at": { 17957 + "type": "string", 17958 + "format": "date-time", 17959 + "x-go-name": "Updated" 17927 17960 } 17928 17961 }, 17929 17962 "x-go-package": "code.gitea.io/gitea/modules/structs" ··· 18684 18717 }, 18685 18718 "x-go-package": "code.gitea.io/gitea/modules/structs" 18686 18719 }, 18720 + "DeleteLabelsOption": { 18721 + "description": "DeleteLabelOption options for deleting a label", 18722 + "type": "object", 18723 + "properties": { 18724 + "updated_at": { 18725 + "type": "string", 18726 + "format": "date-time", 18727 + "x-go-name": "Updated" 18728 + } 18729 + }, 18730 + "x-go-package": "code.gitea.io/gitea/modules/structs" 18731 + }, 18687 18732 "DeployKey": { 18688 18733 "description": "DeployKey a deploy key", 18689 18734 "type": "object", ··· 18941 18986 "body": { 18942 18987 "type": "string", 18943 18988 "x-go-name": "Body" 18989 + }, 18990 + "updated_at": { 18991 + "type": "string", 18992 + "format": "date-time", 18993 + "x-go-name": "Updated" 18944 18994 } 18945 18995 }, 18946 18996 "x-go-package": "code.gitea.io/gitea/modules/structs" ··· 18990 19040 "unset_due_date": { 18991 19041 "type": "boolean", 18992 19042 "x-go-name": "RemoveDeadline" 19043 + }, 19044 + "updated_at": { 19045 + "type": "string", 19046 + "format": "date-time", 19047 + "x-go-name": "Updated" 18993 19048 } 18994 19049 }, 18995 19050 "x-go-package": "code.gitea.io/gitea/modules/structs" ··· 20404 20459 "format": "int64" 20405 20460 }, 20406 20461 "x-go-name": "Labels" 20462 + }, 20463 + "updated_at": { 20464 + "type": "string", 20465 + "format": "date-time", 20466 + "x-go-name": "Updated" 20407 20467 } 20408 20468 }, 20409 20469 "x-go-package": "code.gitea.io/gitea/modules/structs"
+77
tests/integration/api_comment_attachment_test.go
··· 11 11 "mime/multipart" 12 12 "net/http" 13 13 "testing" 14 + "time" 14 15 15 16 auth_model "code.gitea.io/gitea/models/auth" 16 17 "code.gitea.io/gitea/models/db" ··· 119 120 DecodeJSON(t, resp, &apiAttachment) 120 121 121 122 unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID, CommentID: comment.ID}) 123 + } 124 + 125 + func TestAPICreateCommentAttachmentAutoDate(t *testing.T) { 126 + defer tests.PrepareTestEnv(t)() 127 + 128 + comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2}) 129 + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID}) 130 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID}) 131 + repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) 132 + 133 + session := loginUser(t, repoOwner.Name) 134 + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue) 135 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/assets", 136 + repoOwner.Name, repo.Name, comment.ID) 137 + 138 + filename := "image.png" 139 + buff := generateImg() 140 + body := &bytes.Buffer{} 141 + 142 + t.Run("WithAutoDate", func(t *testing.T) { 143 + defer tests.PrintCurrentTest(t)() 144 + 145 + // Setup multi-part 146 + writer := multipart.NewWriter(body) 147 + part, err := writer.CreateFormFile("attachment", filename) 148 + assert.NoError(t, err) 149 + _, err = io.Copy(part, &buff) 150 + assert.NoError(t, err) 151 + err = writer.Close() 152 + assert.NoError(t, err) 153 + 154 + req := NewRequestWithBody(t, "POST", urlStr, body).AddTokenAuth(token) 155 + req.Header.Add("Content-Type", writer.FormDataContentType()) 156 + resp := session.MakeRequest(t, req, http.StatusCreated) 157 + apiAttachment := new(api.Attachment) 158 + DecodeJSON(t, resp, &apiAttachment) 159 + 160 + unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID}) 161 + // the execution of the API call supposedly lasted less than one minute 162 + updatedSince := time.Since(apiAttachment.Created) 163 + assert.LessOrEqual(t, updatedSince, time.Minute) 164 + 165 + commentAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID}) 166 + updatedSince = time.Since(commentAfter.UpdatedUnix.AsTime()) 167 + assert.LessOrEqual(t, updatedSince, time.Minute) 168 + }) 169 + 170 + t.Run("WithUpdateDate", func(t *testing.T) { 171 + defer tests.PrintCurrentTest(t)() 172 + 173 + updatedAt := time.Now().Add(-time.Hour).Truncate(time.Second) 174 + urlStr += fmt.Sprintf("?updated_at=%s", updatedAt.UTC().Format(time.RFC3339)) 175 + 176 + // Setup multi-part 177 + writer := multipart.NewWriter(body) 178 + part, err := writer.CreateFormFile("attachment", filename) 179 + assert.NoError(t, err) 180 + _, err = io.Copy(part, &buff) 181 + assert.NoError(t, err) 182 + err = writer.Close() 183 + assert.NoError(t, err) 184 + 185 + req := NewRequestWithBody(t, "POST", urlStr, body).AddTokenAuth(token) 186 + req.Header.Add("Content-Type", writer.FormDataContentType()) 187 + resp := session.MakeRequest(t, req, http.StatusCreated) 188 + apiAttachment := new(api.Attachment) 189 + DecodeJSON(t, resp, &apiAttachment) 190 + 191 + // dates will be converted into the same tz, in order to compare them 192 + utcTZ, _ := time.LoadLocation("UTC") 193 + unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID}) 194 + assert.Equal(t, updatedAt.In(utcTZ), apiAttachment.Created.In(utcTZ)) 195 + 196 + commentAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID}) 197 + assert.Equal(t, updatedAt.In(utcTZ), commentAfter.UpdatedUnix.AsTime().In(utcTZ)) 198 + }) 122 199 } 123 200 124 201 func TestAPIEditCommentAttachment(t *testing.T) {
+206
tests/integration/api_comment_test.go
··· 8 8 "net/http" 9 9 "net/url" 10 10 "testing" 11 + "time" 11 12 12 13 auth_model "code.gitea.io/gitea/models/auth" 13 14 "code.gitea.io/gitea/models/db" ··· 15 16 repo_model "code.gitea.io/gitea/models/repo" 16 17 "code.gitea.io/gitea/models/unittest" 17 18 user_model "code.gitea.io/gitea/models/user" 19 + "code.gitea.io/gitea/modules/references" 18 20 api "code.gitea.io/gitea/modules/structs" 19 21 "code.gitea.io/gitea/services/convert" 20 22 "code.gitea.io/gitea/tests" ··· 110 112 unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody}) 111 113 } 112 114 115 + func TestAPICreateCommentAutoDate(t *testing.T) { 116 + defer tests.PrepareTestEnv(t)() 117 + 118 + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{}) 119 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID}) 120 + repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) 121 + token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue) 122 + 123 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", 124 + repoOwner.Name, repo.Name, issue.Index) 125 + const commentBody = "Comment body" 126 + 127 + t.Run("WithAutoDate", func(t *testing.T) { 128 + defer tests.PrintCurrentTest(t)() 129 + 130 + req := NewRequestWithValues(t, "POST", urlStr, map[string]string{ 131 + "body": commentBody, 132 + }).AddTokenAuth(token) 133 + resp := MakeRequest(t, req, http.StatusCreated) 134 + var updatedComment api.Comment 135 + DecodeJSON(t, resp, &updatedComment) 136 + 137 + // the execution of the API call supposedly lasted less than one minute 138 + updatedSince := time.Since(updatedComment.Updated) 139 + assert.LessOrEqual(t, updatedSince, time.Minute) 140 + 141 + commentAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody}) 142 + updatedSince = time.Since(commentAfter.UpdatedUnix.AsTime()) 143 + assert.LessOrEqual(t, updatedSince, time.Minute) 144 + }) 145 + 146 + t.Run("WithUpdateDate", func(t *testing.T) { 147 + defer tests.PrintCurrentTest(t)() 148 + 149 + updatedAt := time.Now().Add(-time.Hour).Truncate(time.Second) 150 + 151 + req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueCommentOption{ 152 + Body: commentBody, 153 + Updated: &updatedAt, 154 + }).AddTokenAuth(token) 155 + resp := MakeRequest(t, req, http.StatusCreated) 156 + var updatedComment api.Comment 157 + DecodeJSON(t, resp, &updatedComment) 158 + 159 + // dates will be converted into the same tz, in order to compare them 160 + utcTZ, _ := time.LoadLocation("UTC") 161 + assert.Equal(t, updatedAt.In(utcTZ), updatedComment.Updated.In(utcTZ)) 162 + commentAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody}) 163 + assert.Equal(t, updatedAt.In(utcTZ), commentAfter.UpdatedUnix.AsTime().In(utcTZ)) 164 + }) 165 + } 166 + 167 + func TestAPICommentXRefAutoDate(t *testing.T) { 168 + defer tests.PrepareTestEnv(t)() 169 + 170 + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1}) 171 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID}) 172 + repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) 173 + token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue) 174 + 175 + t.Run("WithAutoDate", func(t *testing.T) { 176 + defer tests.PrintCurrentTest(t)() 177 + 178 + // Create a comment mentioning issue #2 and check that a xref comment was added 179 + // in issue #2 180 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", 181 + repoOwner.Name, repo.Name, issue.Index) 182 + 183 + commentBody := "mention #2" 184 + req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueCommentOption{ 185 + Body: commentBody, 186 + }).AddTokenAuth(token) 187 + resp := MakeRequest(t, req, http.StatusCreated) 188 + var createdComment api.Comment 189 + DecodeJSON(t, resp, &createdComment) 190 + 191 + ref := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: 2, RefIssueID: 1, RefCommentID: createdComment.ID}) 192 + assert.Equal(t, issues_model.CommentTypeCommentRef, ref.Type) 193 + assert.Equal(t, references.XRefActionNone, ref.RefAction) 194 + // the execution of the API call supposedly lasted less than one minute 195 + updatedSince := time.Since(ref.UpdatedUnix.AsTime()) 196 + assert.LessOrEqual(t, updatedSince, time.Minute) 197 + 198 + // Remove the mention to issue #2 and check that the xref was neutered 199 + urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d", 200 + repoOwner.Name, repo.Name, createdComment.ID) 201 + 202 + newCommentBody := "no mention" 203 + req = NewRequestWithJSON(t, "PATCH", urlStr, &api.EditIssueCommentOption{ 204 + Body: newCommentBody, 205 + }).AddTokenAuth(token) 206 + resp = MakeRequest(t, req, http.StatusOK) 207 + var updatedComment api.Comment 208 + DecodeJSON(t, resp, &updatedComment) 209 + 210 + ref = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: 2, RefIssueID: 1, RefCommentID: updatedComment.ID}) 211 + assert.Equal(t, issues_model.CommentTypeCommentRef, ref.Type) 212 + assert.Equal(t, references.XRefActionNeutered, ref.RefAction) 213 + // the execution of the API call supposedly lasted less than one minute 214 + updatedSince = time.Since(ref.UpdatedUnix.AsTime()) 215 + assert.LessOrEqual(t, updatedSince, time.Minute) 216 + }) 217 + 218 + t.Run("WithUpdateDate", func(t *testing.T) { 219 + defer tests.PrintCurrentTest(t)() 220 + 221 + // dates will be converted into the same tz, in order to compare them 222 + utcTZ, _ := time.LoadLocation("UTC") 223 + 224 + // Create a comment mentioning issue #2 and check that a xref comment was added 225 + // in issue #2 226 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", 227 + repoOwner.Name, repo.Name, issue.Index) 228 + 229 + commentBody := "re-mention #2" 230 + updatedAt := time.Now().Add(-time.Hour).Truncate(time.Second) 231 + req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueCommentOption{ 232 + Body: commentBody, 233 + Updated: &updatedAt, 234 + }).AddTokenAuth(token) 235 + resp := MakeRequest(t, req, http.StatusCreated) 236 + var createdComment api.Comment 237 + DecodeJSON(t, resp, &createdComment) 238 + 239 + ref := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: 2, RefIssueID: 1, RefCommentID: createdComment.ID}) 240 + assert.Equal(t, issues_model.CommentTypeCommentRef, ref.Type) 241 + assert.Equal(t, references.XRefActionNone, ref.RefAction) 242 + assert.Equal(t, updatedAt.In(utcTZ), ref.UpdatedUnix.AsTimeInLocation(utcTZ)) 243 + 244 + // Remove the mention to issue #2 and check that the xref was neutered 245 + urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d", 246 + repoOwner.Name, repo.Name, createdComment.ID) 247 + 248 + newCommentBody := "no mention" 249 + updatedAt = time.Now().Add(-time.Hour).Truncate(time.Second) 250 + req = NewRequestWithJSON(t, "PATCH", urlStr, &api.EditIssueCommentOption{ 251 + Body: newCommentBody, 252 + Updated: &updatedAt, 253 + }).AddTokenAuth(token) 254 + resp = MakeRequest(t, req, http.StatusOK) 255 + var updatedComment api.Comment 256 + DecodeJSON(t, resp, &updatedComment) 257 + 258 + ref = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: 2, RefIssueID: 1, RefCommentID: updatedComment.ID}) 259 + assert.Equal(t, issues_model.CommentTypeCommentRef, ref.Type) 260 + assert.Equal(t, references.XRefActionNeutered, ref.RefAction) 261 + assert.Equal(t, updatedAt.In(utcTZ), ref.UpdatedUnix.AsTimeInLocation(utcTZ)) 262 + }) 263 + } 264 + 113 265 func TestAPIGetComment(t *testing.T) { 114 266 defer tests.PrepareTestEnv(t)() 115 267 ··· 210 362 assert.EqualValues(t, comment.ID, updatedComment.ID) 211 363 assert.EqualValues(t, newCommentBody, updatedComment.Body) 212 364 unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody}) 365 + } 366 + 367 + func TestAPIEditCommentWithDate(t *testing.T) { 368 + defer tests.PrepareTestEnv(t)() 369 + 370 + comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{}, 371 + unittest.Cond("type = ?", issues_model.CommentTypeComment)) 372 + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID}) 373 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID}) 374 + repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) 375 + token := getUserToken(t, repoOwner.Name, auth_model.AccessTokenScopeWriteIssue) 376 + 377 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d", 378 + repoOwner.Name, repo.Name, comment.ID) 379 + const newCommentBody = "This is the new comment body" 380 + 381 + t.Run("WithAutoDate", func(t *testing.T) { 382 + defer tests.PrintCurrentTest(t)() 383 + 384 + req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{ 385 + "body": newCommentBody, 386 + }).AddTokenAuth(token) 387 + resp := MakeRequest(t, req, http.StatusOK) 388 + var updatedComment api.Comment 389 + DecodeJSON(t, resp, &updatedComment) 390 + 391 + // the execution of the API call supposedly lasted less than one minute 392 + updatedSince := time.Since(updatedComment.Updated) 393 + assert.LessOrEqual(t, updatedSince, time.Minute) 394 + 395 + commentAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody}) 396 + updatedSince = time.Since(commentAfter.UpdatedUnix.AsTime()) 397 + assert.LessOrEqual(t, updatedSince, time.Minute) 398 + }) 399 + 400 + t.Run("WithUpdateDate", func(t *testing.T) { 401 + defer tests.PrintCurrentTest(t)() 402 + 403 + updatedAt := time.Now().Add(-time.Hour).Truncate(time.Second) 404 + 405 + req := NewRequestWithJSON(t, "PATCH", urlStr, &api.EditIssueCommentOption{ 406 + Body: newCommentBody, 407 + Updated: &updatedAt, 408 + }).AddTokenAuth(token) 409 + resp := MakeRequest(t, req, http.StatusOK) 410 + var updatedComment api.Comment 411 + DecodeJSON(t, resp, &updatedComment) 412 + 413 + // dates will be converted into the same tz, in order to compare them 414 + utcTZ, _ := time.LoadLocation("UTC") 415 + assert.Equal(t, updatedAt.In(utcTZ), updatedComment.Updated.In(utcTZ)) 416 + commentAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody}) 417 + assert.Equal(t, updatedAt.In(utcTZ), commentAfter.UpdatedUnix.AsTime().In(utcTZ)) 418 + }) 213 419 } 214 420 215 421 func TestAPIDeleteComment(t *testing.T) {
+77
tests/integration/api_issue_attachment_test.go
··· 11 11 "mime/multipart" 12 12 "net/http" 13 13 "testing" 14 + "time" 14 15 15 16 auth_model "code.gitea.io/gitea/models/auth" 16 17 issues_model "code.gitea.io/gitea/models/issues" ··· 95 96 DecodeJSON(t, resp, &apiAttachment) 96 97 97 98 unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID, IssueID: issue.ID}) 99 + } 100 + 101 + func TestAPICreateIssueAttachmentAutoDate(t *testing.T) { 102 + defer tests.PrepareTestEnv(t)() 103 + 104 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 105 + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID}) 106 + repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) 107 + 108 + session := loginUser(t, repoOwner.Name) 109 + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue) 110 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/assets", 111 + repoOwner.Name, repo.Name, issue.Index) 112 + 113 + filename := "image.png" 114 + buff := generateImg() 115 + body := &bytes.Buffer{} 116 + 117 + t.Run("WithAutoDate", func(t *testing.T) { 118 + defer tests.PrintCurrentTest(t)() 119 + 120 + // Setup multi-part 121 + writer := multipart.NewWriter(body) 122 + part, err := writer.CreateFormFile("attachment", filename) 123 + assert.NoError(t, err) 124 + _, err = io.Copy(part, &buff) 125 + assert.NoError(t, err) 126 + err = writer.Close() 127 + assert.NoError(t, err) 128 + 129 + req := NewRequestWithBody(t, "POST", urlStr, body).AddTokenAuth(token) 130 + req.Header.Add("Content-Type", writer.FormDataContentType()) 131 + resp := session.MakeRequest(t, req, http.StatusCreated) 132 + 133 + apiAttachment := new(api.Attachment) 134 + DecodeJSON(t, resp, &apiAttachment) 135 + 136 + unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID, IssueID: issue.ID}) 137 + // the execution of the API call supposedly lasted less than one minute 138 + updatedSince := time.Since(apiAttachment.Created) 139 + assert.LessOrEqual(t, updatedSince, time.Minute) 140 + 141 + issueAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issue.Index}) 142 + updatedSince = time.Since(issueAfter.UpdatedUnix.AsTime()) 143 + assert.LessOrEqual(t, updatedSince, time.Minute) 144 + }) 145 + 146 + t.Run("WithUpdateDate", func(t *testing.T) { 147 + defer tests.PrintCurrentTest(t)() 148 + 149 + updatedAt := time.Now().Add(-time.Hour).Truncate(time.Second) 150 + urlStr += fmt.Sprintf("?updated_at=%s", updatedAt.UTC().Format(time.RFC3339)) 151 + 152 + // Setup multi-part 153 + writer := multipart.NewWriter(body) 154 + part, err := writer.CreateFormFile("attachment", filename) 155 + assert.NoError(t, err) 156 + _, err = io.Copy(part, &buff) 157 + assert.NoError(t, err) 158 + err = writer.Close() 159 + assert.NoError(t, err) 160 + 161 + req := NewRequestWithBody(t, "POST", urlStr, body).AddTokenAuth(token) 162 + req.Header.Add("Content-Type", writer.FormDataContentType()) 163 + resp := session.MakeRequest(t, req, http.StatusCreated) 164 + 165 + apiAttachment := new(api.Attachment) 166 + DecodeJSON(t, resp, &apiAttachment) 167 + 168 + // dates will be converted into the same tz, in order to compare them 169 + utcTZ, _ := time.LoadLocation("UTC") 170 + unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID, IssueID: issue.ID}) 171 + assert.Equal(t, updatedAt.In(utcTZ), apiAttachment.Created.In(utcTZ)) 172 + issueAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issue.ID}) 173 + assert.Equal(t, updatedAt.In(utcTZ), issueAfter.UpdatedUnix.AsTime().In(utcTZ)) 174 + }) 98 175 } 99 176 100 177 func TestAPIEditIssueAttachment(t *testing.T) {
+45
tests/integration/api_issue_label_test.go
··· 8 8 "net/http" 9 9 "strings" 10 10 "testing" 11 + "time" 11 12 12 13 auth_model "code.gitea.io/gitea/models/auth" 13 14 issues_model "code.gitea.io/gitea/models/issues" ··· 15 16 "code.gitea.io/gitea/models/unittest" 16 17 user_model "code.gitea.io/gitea/models/user" 17 18 api "code.gitea.io/gitea/modules/structs" 19 + "code.gitea.io/gitea/tests" 18 20 19 21 "github.com/stretchr/testify/assert" 20 22 ) ··· 112 114 assert.Len(t, apiLabels, unittest.GetCount(t, &issues_model.IssueLabel{IssueID: issue.ID})) 113 115 114 116 unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: 2}) 117 + } 118 + 119 + func TestAPIAddIssueLabelsAutoDate(t *testing.T) { 120 + defer tests.PrepareTestEnv(t)() 121 + 122 + issueBefore := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 3}) 123 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issueBefore.RepoID}) 124 + owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) 125 + 126 + session := loginUser(t, owner.Name) 127 + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue) 128 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels", 129 + owner.Name, repo.Name, issueBefore.Index) 130 + 131 + t.Run("WithAutoDate", func(t *testing.T) { 132 + defer tests.PrintCurrentTest(t)() 133 + 134 + req := NewRequestWithJSON(t, "POST", urlStr, &api.IssueLabelsOption{ 135 + Labels: []int64{1}, 136 + }).AddTokenAuth(token) 137 + MakeRequest(t, req, http.StatusOK) 138 + 139 + issueAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issueBefore.ID}) 140 + // the execution of the API call supposedly lasted less than one minute 141 + updatedSince := time.Since(issueAfter.UpdatedUnix.AsTime()) 142 + assert.LessOrEqual(t, updatedSince, time.Minute) 143 + }) 144 + 145 + t.Run("WithUpdatedDate", func(t *testing.T) { 146 + defer tests.PrintCurrentTest(t)() 147 + 148 + updatedAt := time.Now().Add(-time.Hour).Truncate(time.Second) 149 + req := NewRequestWithJSON(t, "POST", urlStr, &api.IssueLabelsOption{ 150 + Labels: []int64{2}, 151 + Updated: &updatedAt, 152 + }).AddTokenAuth(token) 153 + MakeRequest(t, req, http.StatusOK) 154 + 155 + // dates will be converted into the same tz, in order to compare them 156 + utcTZ, _ := time.LoadLocation("UTC") 157 + issueAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issueBefore.ID}) 158 + assert.Equal(t, updatedAt.In(utcTZ), issueAfter.UpdatedUnix.AsTime().In(utcTZ)) 159 + }) 115 160 } 116 161 117 162 func TestAPIReplaceIssueLabels(t *testing.T) {
+151
tests/integration/api_issue_test.go
··· 213 213 assert.Equal(t, title, issueAfter.Title) 214 214 } 215 215 216 + func TestAPIEditIssueAutoDate(t *testing.T) { 217 + defer tests.PrepareTestEnv(t)() 218 + 219 + issueBefore := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 13}) 220 + repoBefore := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issueBefore.RepoID}) 221 + owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repoBefore.OwnerID}) 222 + assert.NoError(t, issueBefore.LoadAttributes(db.DefaultContext)) 223 + 224 + t.Run("WithAutoDate", func(t *testing.T) { 225 + defer tests.PrintCurrentTest(t)() 226 + 227 + // User2 is not owner, but can update the 'public' issue with auto date 228 + session := loginUser(t, "user2") 229 + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue) 230 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", owner.Name, repoBefore.Name, issueBefore.Index) 231 + 232 + body := "new content!" 233 + req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditIssueOption{ 234 + Body: &body, 235 + }).AddTokenAuth(token) 236 + resp := MakeRequest(t, req, http.StatusCreated) 237 + var apiIssue api.Issue 238 + DecodeJSON(t, resp, &apiIssue) 239 + 240 + // the execution of the API call supposedly lasted less than one minute 241 + updatedSince := time.Since(apiIssue.Updated) 242 + assert.LessOrEqual(t, updatedSince, time.Minute) 243 + 244 + issueAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issueBefore.ID}) 245 + updatedSince = time.Since(issueAfter.UpdatedUnix.AsTime()) 246 + assert.LessOrEqual(t, updatedSince, time.Minute) 247 + }) 248 + 249 + t.Run("WithUpdateDate", func(t *testing.T) { 250 + defer tests.PrintCurrentTest(t)() 251 + 252 + // User1 is admin, and so can update the issue without auto date 253 + session := loginUser(t, "user1") 254 + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue) 255 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", owner.Name, repoBefore.Name, issueBefore.Index) 256 + 257 + body := "new content, with updated time" 258 + updatedAt := time.Now().Add(-time.Hour).Truncate(time.Second) 259 + req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditIssueOption{ 260 + Body: &body, 261 + Updated: &updatedAt, 262 + }).AddTokenAuth(token) 263 + resp := MakeRequest(t, req, http.StatusCreated) 264 + var apiIssue api.Issue 265 + DecodeJSON(t, resp, &apiIssue) 266 + 267 + // dates are converted into the same tz, in order to compare them 268 + utcTZ, _ := time.LoadLocation("UTC") 269 + assert.Equal(t, updatedAt.In(utcTZ), apiIssue.Updated.In(utcTZ)) 270 + 271 + issueAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issueBefore.ID}) 272 + assert.Equal(t, updatedAt.In(utcTZ), issueAfter.UpdatedUnix.AsTime().In(utcTZ)) 273 + }) 274 + 275 + t.Run("WithoutPermission", func(t *testing.T) { 276 + defer tests.PrintCurrentTest(t)() 277 + 278 + // User2 is not owner nor admin, and so can't update the issue without auto date 279 + session := loginUser(t, "user2") 280 + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue) 281 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", owner.Name, repoBefore.Name, issueBefore.Index) 282 + 283 + body := "new content, with updated time" 284 + updatedAt := time.Now().Add(-time.Hour).Truncate(time.Second) 285 + req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditIssueOption{ 286 + Body: &body, 287 + Updated: &updatedAt, 288 + }).AddTokenAuth(token) 289 + resp := MakeRequest(t, req, http.StatusForbidden) 290 + var apiError api.APIError 291 + DecodeJSON(t, resp, &apiError) 292 + 293 + assert.Equal(t, "user needs to have admin or owner right", apiError.Message) 294 + }) 295 + } 296 + 297 + func TestAPIEditIssueMilestoneAutoDate(t *testing.T) { 298 + defer tests.PrepareTestEnv(t)() 299 + 300 + issueBefore := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1}) 301 + repoBefore := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issueBefore.RepoID}) 302 + 303 + owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repoBefore.OwnerID}) 304 + assert.NoError(t, issueBefore.LoadAttributes(db.DefaultContext)) 305 + 306 + session := loginUser(t, owner.Name) 307 + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue) 308 + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", owner.Name, repoBefore.Name, issueBefore.Index) 309 + 310 + t.Run("WithAutoDate", func(t *testing.T) { 311 + defer tests.PrintCurrentTest(t)() 312 + 313 + milestone := int64(1) 314 + req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditIssueOption{ 315 + Milestone: &milestone, 316 + }).AddTokenAuth(token) 317 + MakeRequest(t, req, http.StatusCreated) 318 + 319 + // the execution of the API call supposedly lasted less than one minute 320 + milestoneAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: milestone}) 321 + updatedSince := time.Since(milestoneAfter.UpdatedUnix.AsTime()) 322 + assert.LessOrEqual(t, updatedSince, time.Minute) 323 + }) 324 + 325 + t.Run("WithPostUpdateDate", func(t *testing.T) { 326 + defer tests.PrintCurrentTest(t)() 327 + 328 + // Note: the updated_unix field of the test Milestones is set to NULL 329 + // Hence, any date is higher than the Milestone's updated date 330 + updatedAt := time.Now().Add(-time.Hour).Truncate(time.Second) 331 + milestone := int64(2) 332 + req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditIssueOption{ 333 + Milestone: &milestone, 334 + Updated: &updatedAt, 335 + }).AddTokenAuth(token) 336 + MakeRequest(t, req, http.StatusCreated) 337 + 338 + // the milestone date should be set to 'updatedAt' 339 + // dates are converted into the same tz, in order to compare them 340 + utcTZ, _ := time.LoadLocation("UTC") 341 + milestoneAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: milestone}) 342 + assert.Equal(t, updatedAt.In(utcTZ), milestoneAfter.UpdatedUnix.AsTime().In(utcTZ)) 343 + }) 344 + 345 + t.Run("WithPastUpdateDate", func(t *testing.T) { 346 + defer tests.PrintCurrentTest(t)() 347 + 348 + // Note: This Milestone's updated_unix has been set to Now() by the first subtest 349 + milestone := int64(1) 350 + milestoneBefore := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: milestone}) 351 + 352 + updatedAt := time.Now().Add(-time.Hour).Truncate(time.Second) 353 + req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditIssueOption{ 354 + Milestone: &milestone, 355 + Updated: &updatedAt, 356 + }).AddTokenAuth(token) 357 + MakeRequest(t, req, http.StatusCreated) 358 + 359 + // the milestone date should not change 360 + // dates are converted into the same tz, in order to compare them 361 + utcTZ, _ := time.LoadLocation("UTC") 362 + milestoneAfter := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: milestone}) 363 + assert.Equal(t, milestoneAfter.UpdatedUnix.AsTime().In(utcTZ), milestoneBefore.UpdatedUnix.AsTime().In(utcTZ)) 364 + }) 365 + } 366 + 216 367 func TestAPISearchIssues(t *testing.T) { 217 368 defer tests.PrepareTestEnv(t)() 218 369