Skip to content

Commit b0215c4

Browse files
authored
Store and use seconds for timeline time comments (#25392)
this will allow us to fully localize it later PS: we can not migrate back as the old value was a one-way conversion prepare for #25213 --- *Sponsored by Kithara Software GmbH*
1 parent a954c93 commit b0215c4

File tree

6 files changed

+67
-18
lines changed

6 files changed

+67
-18
lines changed

models/issues/tracked_time.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package issues
66
import (
77
"context"
88
"errors"
9+
"fmt"
910
"time"
1011

1112
"code.gitea.io/gitea/models/db"
@@ -173,10 +174,12 @@ func AddTime(user *user_model.User, issue *Issue, amount int64, created time.Tim
173174
}
174175

175176
if _, err := CreateComment(ctx, &CreateCommentOptions{
176-
Issue: issue,
177-
Repo: issue.Repo,
178-
Doer: user,
179-
Content: util.SecToTime(amount),
177+
Issue: issue,
178+
Repo: issue.Repo,
179+
Doer: user,
180+
// Content before v1.21 did store the formated string instead of seconds,
181+
// so use "|" as delimeter to mark the new format
182+
Content: fmt.Sprintf("|%d", amount),
180183
Type: CommentTypeAddTimeManual,
181184
TimeID: t.ID,
182185
}); err != nil {
@@ -251,10 +254,12 @@ func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error {
251254
return err
252255
}
253256
if _, err := CreateComment(ctx, &CreateCommentOptions{
254-
Issue: issue,
255-
Repo: issue.Repo,
256-
Doer: user,
257-
Content: "- " + util.SecToTime(removedTime),
257+
Issue: issue,
258+
Repo: issue.Repo,
259+
Doer: user,
260+
// Content before v1.21 did store the formated string instead of seconds,
261+
// so use "|" as delimeter to mark the new format
262+
Content: fmt.Sprintf("|%d", removedTime),
258263
Type: CommentTypeDeleteTimeManual,
259264
}); err != nil {
260265
return err
@@ -280,10 +285,12 @@ func DeleteTime(t *TrackedTime) error {
280285
}
281286

282287
if _, err := CreateComment(ctx, &CreateCommentOptions{
283-
Issue: t.Issue,
284-
Repo: t.Issue.Repo,
285-
Doer: t.User,
286-
Content: "- " + util.SecToTime(t.Time),
288+
Issue: t.Issue,
289+
Repo: t.Issue.Repo,
290+
Doer: t.User,
291+
// Content before v1.21 did store the formated string instead of seconds,
292+
// so use "|" as delimeter to mark the new format
293+
Content: fmt.Sprintf("|%d", t.Time),
287294
Type: CommentTypeDeleteTimeManual,
288295
}); err != nil {
289296
return err

models/issues/tracked_time_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestAddTime(t *testing.T) {
3535
assert.Equal(t, int64(3661), tt.Time)
3636

3737
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{Type: issues_model.CommentTypeAddTimeManual, PosterID: 3, IssueID: 1})
38-
assert.Equal(t, "1 hour 1 minute", comment.Content)
38+
assert.Equal(t, "|3661", comment.Content)
3939
}
4040

4141
func TestGetTrackedTimes(t *testing.T) {

modules/util/sec_to_time.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515
// 1563418 -> 2 weeks 4 days
1616
// 3937125s -> 1 month 2 weeks
1717
// 45677465s -> 1 year 6 months
18-
func SecToTime(duration int64) string {
18+
func SecToTime(durationVal any) string {
19+
duration, _ := ToInt64(durationVal)
20+
1921
formattedTime := ""
2022

2123
// The following four variables are calculated by taking

routers/web/repo/issue.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1647,9 +1647,22 @@ func ViewIssue(ctx *context.Context) {
16471647
return
16481648
}
16491649
} else if comment.Type == issues_model.CommentTypeAddTimeManual ||
1650-
comment.Type == issues_model.CommentTypeStopTracking {
1650+
comment.Type == issues_model.CommentTypeStopTracking ||
1651+
comment.Type == issues_model.CommentTypeDeleteTimeManual {
16511652
// drop error since times could be pruned from DB..
16521653
_ = comment.LoadTime()
1654+
if comment.Content != "" {
1655+
// Content before v1.21 did store the formated string instead of seconds,
1656+
// so "|" is used as delimeter to mark the new format
1657+
if comment.Content[0] != '|' {
1658+
// handle old time comments that have formatted text stored
1659+
comment.RenderedContent = comment.Content
1660+
comment.Content = ""
1661+
} else {
1662+
// else it's just a duration in seconds to pass on to the frontend
1663+
comment.Content = comment.Content[1:]
1664+
}
1665+
}
16531666
}
16541667

16551668
if comment.Type == issues_model.CommentTypeClose || comment.Type == issues_model.CommentTypeMergePull {

services/convert/issue_comment.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
user_model "code.gitea.io/gitea/models/user"
1212
"code.gitea.io/gitea/modules/log"
1313
api "code.gitea.io/gitea/modules/structs"
14+
"code.gitea.io/gitea/modules/util"
1415
)
1516

1617
// ToComment converts a issues_model.Comment to the api.Comment format
@@ -66,6 +67,17 @@ func ToTimelineComment(ctx context.Context, c *issues_model.Comment, doer *user_
6667
return nil
6768
}
6869

70+
if c.Content != "" {
71+
if (c.Type == issues_model.CommentTypeAddTimeManual ||
72+
c.Type == issues_model.CommentTypeStopTracking ||
73+
c.Type == issues_model.CommentTypeDeleteTimeManual) &&
74+
c.Content[0] == '|' {
75+
// TimeTracking Comments from v1.21 on store the seconds instead of an formated string
76+
// so we check for the "|" delimeter and convert new to legacy format on demand
77+
c.Content = util.SecToTime(c.Content[1:])
78+
}
79+
}
80+
6981
comment := &api.TimelineComment{
7082
ID: c.ID,
7183
Type: c.Type.String(),

templates/repo/issue/view_content/comments.tmpl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,12 @@
263263
{{template "repo/issue/view_content/comments_delete_time" dict "ctxData" $ "comment" .}}
264264
<div class="detail">
265265
{{svg "octicon-clock"}}
266-
<span class="text grey muted-links">{{.Content}}</span>
266+
{{if .RenderedContent}}
267+
{{/* compatibility with time comments made before v1.21 */}}
268+
<span class="text grey muted-links">{{.RenderedContent}}</span>
269+
{{else}}
270+
<span class="text grey muted-links">{{.Content|Sec2Time}}</span>
271+
{{end}}
267272
</div>
268273
</div>
269274
{{else if eq .Type 14}}
@@ -277,7 +282,12 @@
277282
{{template "repo/issue/view_content/comments_delete_time" dict "ctxData" $ "comment" .}}
278283
<div class="detail">
279284
{{svg "octicon-clock"}}
280-
<span class="text grey muted-links">{{.Content}}</span>
285+
{{if .RenderedContent}}
286+
{{/* compatibility with time comments made before v1.21 */}}
287+
<span class="text grey muted-links">{{.RenderedContent}}</span>
288+
{{else}}
289+
<span class="text grey muted-links">{{.Content|Sec2Time}}</span>
290+
{{end}}
281291
</div>
282292
</div>
283293
{{else if eq .Type 15}}
@@ -676,7 +686,12 @@
676686
</span>
677687
<div class="detail">
678688
{{svg "octicon-clock"}}
679-
<span class="text grey muted-links">{{.Content}}</span>
689+
{{if .RenderedContent}}
690+
{{/* compatibility with time comments made before v1.21 */}}
691+
<span class="text grey muted-links">{{.RenderedContent}}</span>
692+
{{else}}
693+
<span class="text grey muted-links">- {{.Content|Sec2Time}}</span>
694+
{{end}}
680695
</div>
681696
</div>
682697
{{else if eq .Type 27}}

0 commit comments

Comments
 (0)