Skip to content

Commit cd38789

Browse files
fluzzearl-warren
authored andcommitted
[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) (cherry picked from commit 2e43e49961c1cd5791744fa4e7994ce929c31837) (cherry picked from commit 3bfb6cc1c085a1ae11885d0eb138d7e977fa1a16) (cherry picked from commit 38918d5f5cb148b8f53d6707fe6bc677c19c7f79) (cherry picked from commit 174f6ac3453c7ba1a88655af5d0fff807eb94dc1) (cherry picked from commit 08a2bed45dc48547c0ab79fe1de2e3c62c823ae2)
1 parent de6474c commit cd38789

26 files changed

+795
-32
lines changed

models/issues/comment.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,11 @@ func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment,
817817
IsForcePush: opts.IsForcePush,
818818
Invalidated: opts.Invalidated,
819819
}
820+
if opts.Issue.NoAutoTime {
821+
comment.CreatedUnix = opts.Issue.UpdatedUnix
822+
comment.UpdatedUnix = opts.Issue.UpdatedUnix
823+
e.NoAutoTime()
824+
}
820825
if _, err = e.Insert(comment); err != nil {
821826
return nil, err
822827
}
@@ -1095,9 +1100,17 @@ func UpdateComment(ctx context.Context, c *Comment, doer *user_model.User) error
10951100
return err
10961101
}
10971102
defer committer.Close()
1098-
sess := db.GetEngine(ctx)
10991103

1100-
if _, err := sess.ID(c.ID).AllCols().Update(c); err != nil {
1104+
sess := db.GetEngine(ctx).ID(c.ID).AllCols()
1105+
if c.Issue.NoAutoTime {
1106+
// update the DataBase
1107+
sess = sess.NoAutoTime().SetExpr("updated_unix", c.Issue.UpdatedUnix)
1108+
// the UpdatedUnix value of the Comment also has to be set,
1109+
// to return the adequate valuè
1110+
// see https://codeberg.org/forgejo/forgejo/pulls/764#issuecomment-1023801
1111+
c.UpdatedUnix = c.Issue.UpdatedUnix
1112+
}
1113+
if _, err := sess.Update(c); err != nil {
11011114
return err
11021115
}
11031116
if err := c.LoadIssue(ctx); 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func (issue *Issue) createCrossReferences(stdCtx context.Context, ctx *crossRefe
110110
if ctx.OrigComment != nil {
111111
refCommentID = ctx.OrigComment.ID
112112
}
113+
if ctx.OrigIssue.NoAutoTime {
114+
xref.Issue.NoAutoTime = true
115+
xref.Issue.UpdatedUnix = ctx.OrigIssue.UpdatedUnix
116+
}
113117
opts := &CreateCommentOptions{
114118
Type: ctx.Type,
115119
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
@@ -110,6 +110,8 @@ type EditIssueOption struct {
110110
// swagger:strfmt date-time
111111
Deadline *time.Time `json:"due_date"`
112112
RemoveDeadline *bool `json:"unset_due_date"`
113+
// swagger:strfmt date-time
114+
Updated *time.Time `json:"updated_at"`
113115
}
114116

115117
// 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
@@ -1297,8 +1297,8 @@ func Routes() *web.Route {
12971297
m.Combo("").Get(repo.ListIssueLabels).
12981298
Post(reqToken(), bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
12991299
Put(reqToken(), bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
1300-
Delete(reqToken(), repo.ClearIssueLabels)
1301-
m.Delete("/{id}", reqToken(), repo.DeleteIssueLabel)
1300+
Delete(reqToken(), bind(api.DeleteLabelsOption{}), repo.ClearIssueLabels)
1301+
m.Delete("/{id}", reqToken(), bind(api.DeleteLabelsOption{}), repo.DeleteIssueLabel)
13021302
})
13031303
m.Group("/times", func() {
13041304
m.Combo("").

0 commit comments

Comments
 (0)