Skip to content

Commit c7a389f

Browse files
committed
[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 47c7892) (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 (go-gitea#1676) [this is a follow-up to PR go-gitea#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 go-gitea#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 <[email protected]> Co-committed-by: fluzz <[email protected]> (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 fdd4da1) [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 c5139a7) (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)
1 parent 563b7f8 commit c7a389f

26 files changed

+907
-37
lines changed

models/issues/comment.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,14 @@ func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment,
823823
IsForcePush: opts.IsForcePush,
824824
Invalidated: opts.Invalidated,
825825
}
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+
}
826834
if _, err = e.Insert(comment); err != nil {
827835
return nil, err
828836
}
@@ -1105,12 +1113,21 @@ func UpdateComment(ctx context.Context, c *Comment, doer *user_model.User) error
11051113
return err
11061114
}
11071115
defer committer.Close()
1108-
sess := db.GetEngine(ctx)
11091116

1110-
if _, err := sess.ID(c.ID).AllCols().Update(c); err != nil {
1117+
if err := c.LoadIssue(ctx); err != nil {
11111118
return err
11121119
}
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 {
11141131
return err
11151132
}
11161133
if err := c.AddCrossReferences(ctx, doer, true); err != nil {

models/issues/issue.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ type Issue struct {
126126
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
127127
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
128128
ClosedUnix timeutil.TimeStamp `xorm:"INDEX"`
129+
NoAutoTime bool `xorm:"-"`
129130

130131
Attachments []*repo_model.Attachment `xorm:"-"`
131132
Comments CommentList `xorm:"-"`

models/issues/issue_update.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ import (
2727

2828
// UpdateIssueCols updates cols of issue
2929
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 {
3136
return err
3237
}
3338
return nil
@@ -71,7 +76,11 @@ func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.Use
7176
}
7277

7378
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+
}
7584
} else {
7685
issue.ClosedUnix = 0
7786
}
@@ -92,8 +101,14 @@ func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.Use
92101

93102
// Update issue count of milestone
94103
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+
}
97112
}
98113
}
99114

@@ -259,8 +274,12 @@ func ChangeIssueContent(ctx context.Context, issue *Issue, doer *user_model.User
259274
return fmt.Errorf("UpdateIssueCols: %w", err)
260275
}
261276

277+
historyDate := timeutil.TimeStampNow()
278+
if issue.NoAutoTime {
279+
historyDate = issue.UpdatedUnix
280+
}
262281
if err = SaveIssueContentHistory(ctx, doer.ID, issue.ID, 0,
263-
timeutil.TimeStampNow(), issue.Content, false); err != nil {
282+
historyDate, issue.Content, false); err != nil {
264283
return fmt.Errorf("SaveIssueContentHistory: %w", err)
265284
}
266285

@@ -449,10 +468,13 @@ func UpdateIssueByAPI(ctx context.Context, issue *Issue, doer *user_model.User)
449468
return nil, false, err
450469
}
451470

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 {
456478
return nil, false, err
457479
}
458480

@@ -498,7 +520,7 @@ func UpdateIssueDeadline(ctx context.Context, issue *Issue, deadlineUnix timeuti
498520
defer committer.Close()
499521

500522
// 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 {
502524
return err
503525
}
504526

models/issues/issue_xref.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ func neuterCrossReferences(ctx context.Context, issueID, commentID int64) error
4646
for i, c := range active {
4747
ids[i] = c.ID
4848
}
49-
return neuterCrossReferencesIds(ctx, ids)
49+
return neuterCrossReferencesIds(ctx, nil, ids)
5050
}
5151

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})
5458
return err
5559
}
5660

@@ -100,7 +104,7 @@ func (issue *Issue) createCrossReferences(stdCtx context.Context, ctx *crossRefe
100104
}
101105
}
102106
if len(ids) > 0 {
103-
if err = neuterCrossReferencesIds(stdCtx, ids); err != nil {
107+
if err = neuterCrossReferencesIds(stdCtx, ctx, ids); err != nil {
104108
return err
105109
}
106110
}
@@ -110,6 +114,10 @@ func (issue *Issue) createCrossReferences(stdCtx context.Context, ctx *crossRefe
110114
if ctx.OrigComment != nil {
111115
refCommentID = ctx.OrigComment.ID
112116
}
117+
if ctx.OrigIssue.NoAutoTime {
118+
xref.Issue.NoAutoTime = true
119+
xref.Issue.UpdatedUnix = ctx.OrigIssue.UpdatedUnix
120+
}
113121
opts := &CreateCommentOptions{
114122
Type: ctx.Type,
115123
Doer: ctx.Doer,

models/issues/milestone.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,9 @@ func updateMilestone(ctx context.Context, m *Milestone) error {
187187
return UpdateMilestoneCounters(ctx, m.ID)
188188
}
189189

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 {
192191
e := db.GetEngine(ctx)
193-
_, err := e.ID(id).
192+
sess := e.ID(id).
194193
SetExpr("num_issues", builder.Select("count(*)").From("issue").Where(
195194
builder.Eq{"milestone_id": id},
196195
)).
@@ -199,8 +198,11 @@ func UpdateMilestoneCounters(ctx context.Context, id int64) error {
199198
"milestone_id": id,
200199
"is_closed": true,
201200
},
202-
)).
203-
Update(&Milestone{})
201+
))
202+
if noAutoTime {
203+
sess.SetExpr("updated_unix", updatedUnix).NoAutoTime()
204+
}
205+
_, err := sess.Update(&Milestone{})
204206
if err != nil {
205207
return err
206208
}
@@ -210,6 +212,16 @@ func UpdateMilestoneCounters(ctx context.Context, id int64) error {
210212
return err
211213
}
212214

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)
223+
}
224+
213225
// ChangeMilestoneStatusByRepoIDAndID changes a milestone open/closed status if the milestone ID is in the repo.
214226
func ChangeMilestoneStatusByRepoIDAndID(ctx context.Context, repoID, milestoneID int64, isClosed bool) error {
215227
ctx, committer, err := db.TxContext(ctx)

models/repo/attachment.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Attachment struct {
2828
Name string
2929
DownloadCount int64 `xorm:"DEFAULT 0"`
3030
Size int64 `xorm:"DEFAULT 0"`
31+
NoAutoTime bool `xorm:"-"`
3132
CreatedUnix timeutil.TimeStamp `xorm:"created"`
3233
CustomDownloadURL string `xorm:"-"`
3334
}

modules/structs/issue.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ type EditIssueOption struct {
111111
// swagger:strfmt date-time
112112
Deadline *time.Time `json:"due_date"`
113113
RemoveDeadline *bool `json:"unset_due_date"`
114+
// swagger:strfmt date-time
115+
Updated *time.Time `json:"updated_at"`
114116
}
115117

116118
// EditDeadlineOption options for creating a deadline

modules/structs/issue_comment.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ type Comment struct {
2828
type CreateIssueCommentOption struct {
2929
// required:true
3030
Body string `json:"body" binding:"Required"`
31+
// swagger:strfmt date-time
32+
Updated *time.Time `json:"updated_at"`
3133
}
3234

3335
// EditIssueCommentOption options for editing a comment
3436
type EditIssueCommentOption struct {
3537
// required: true
3638
Body string `json:"body" binding:"Required"`
39+
// swagger:strfmt date-time
40+
Updated *time.Time `json:"updated_at"`
3741
}
3842

3943
// TimelineComment represents a timeline comment (comment of any type) on a commit or issue

modules/structs/issue_label.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
package structs
66

7+
import (
8+
"time"
9+
)
10+
711
// Label a label to an issue or a pr
812
// swagger:model
913
type Label struct {
@@ -45,10 +49,18 @@ type EditLabelOption struct {
4549
IsArchived *bool `json:"is_archived"`
4650
}
4751

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+
4858
// IssueLabelsOption a collection of labels
4959
type IssueLabelsOption struct {
5060
// list of label IDs
5161
Labels []int64 `json:"labels"`
62+
// swagger:strfmt date-time
63+
Updated *time.Time `json:"updated_at"`
5264
}
5365

5466
// LabelTemplate info of a Label template

routers/api/v1/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,8 +1337,8 @@ func Routes() *web.Route {
13371337
m.Combo("").Get(repo.ListIssueLabels).
13381338
Post(reqToken(), bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
13391339
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)
13421342
})
13431343
m.Group("/times", func() {
13441344
m.Combo("").

0 commit comments

Comments
 (0)