Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store and use seconds for timeline time comments #25392

Merged
merged 18 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions models/issues/tracked_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package issues
import (
"context"
"errors"
"fmt"
"time"

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

if _, err := CreateComment(ctx, &CreateCommentOptions{
Issue: issue,
Repo: issue.Repo,
Doer: user,
Content: util.SecToTime(amount),
Issue: issue,
Repo: issue.Repo,
Doer: user,
// Content before v1.21 did store the formated string instead of seconds,
// so use "|" as delimeter to mark the new format
Content: fmt.Sprintf("|%d", amount),
Type: CommentTypeAddTimeManual,
TimeID: t.ID,
}); err != nil {
Expand Down Expand Up @@ -251,10 +254,12 @@ func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error {
return err
}
if _, err := CreateComment(ctx, &CreateCommentOptions{
Issue: issue,
Repo: issue.Repo,
Doer: user,
Content: "- " + util.SecToTime(removedTime),
Issue: issue,
Repo: issue.Repo,
Doer: user,
// Content before v1.21 did store the formated string instead of seconds,
// so use "|" as delimeter to mark the new format
Content: fmt.Sprintf("|%d", removedTime),
Type: CommentTypeDeleteTimeManual,
}); err != nil {
return err
Expand All @@ -280,10 +285,12 @@ func DeleteTime(t *TrackedTime) error {
}

if _, err := CreateComment(ctx, &CreateCommentOptions{
Issue: t.Issue,
Repo: t.Issue.Repo,
Doer: t.User,
Content: "- " + util.SecToTime(t.Time),
Issue: t.Issue,
Repo: t.Issue.Repo,
Doer: t.User,
// Content before v1.21 did store the formated string instead of seconds,
// so use "|" as delimeter to mark the new format
Content: fmt.Sprintf("|%d", t.Time),
Type: CommentTypeDeleteTimeManual,
}); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion models/issues/tracked_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAddTime(t *testing.T) {
assert.Equal(t, int64(3661), tt.Time)

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

func TestGetTrackedTimes(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion modules/util/sec_to_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
// 1563418 -> 2 weeks 4 days
// 3937125s -> 1 month 2 weeks
// 45677465s -> 1 year 6 months
func SecToTime(duration int64) string {
func SecToTime(durationVal any) string {
duration, _ := ToInt64(durationVal)

formattedTime := ""

// The following four variables are calculated by taking
Expand Down
15 changes: 14 additions & 1 deletion routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1647,9 +1647,22 @@ func ViewIssue(ctx *context.Context) {
return
}
} else if comment.Type == issues_model.CommentTypeAddTimeManual ||
comment.Type == issues_model.CommentTypeStopTracking {
comment.Type == issues_model.CommentTypeStopTracking ||
comment.Type == issues_model.CommentTypeDeleteTimeManual {
// drop error since times could be pruned from DB..
_ = comment.LoadTime()
if comment.Content != "" {
// Content before v1.21 did store the formated string instead of seconds,
// so "|" is used as delimeter to mark the new format
if comment.Content[0] != '|' {
// handle old time comments that have formatted text stored
comment.RenderedContent = comment.Content
comment.Content = ""
} else {
// else it's just a duration in seconds to pass on to the frontend
comment.Content = comment.Content[1:]
}
}
}

if comment.Type == issues_model.CommentTypeClose || comment.Type == issues_model.CommentTypeMergePull {
Expand Down
12 changes: 12 additions & 0 deletions services/convert/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
)

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

if c.Content != "" {
if (c.Type == issues_model.CommentTypeAddTimeManual ||
c.Type == issues_model.CommentTypeStopTracking ||
c.Type == issues_model.CommentTypeDeleteTimeManual) &&
c.Content[0] == '|' {
// TimeTracking Comments from v1.21 on store the seconds instead of an formated string
// so we check for the "|" delimeter and convert new to legacy format on demand
c.Content = util.SecToTime(c.Content[1:])
}
}
6543 marked this conversation as resolved.
Show resolved Hide resolved

comment := &api.TimelineComment{
ID: c.ID,
Type: c.Type.String(),
Expand Down
21 changes: 18 additions & 3 deletions templates/repo/issue/view_content/comments.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,12 @@
{{template "repo/issue/view_content/comments_delete_time" dict "ctxData" $ "comment" .}}
<div class="detail">
{{svg "octicon-clock"}}
<span class="text grey muted-links">{{.Content}}</span>
{{if .RenderedContent}}
{{/* compatibility with time comments made before v1.21 */}}
<span class="text grey muted-links">{{.RenderedContent}}</span>
{{else}}
<span class="text grey muted-links">{{.Content|Sec2Time}}</span>
{{end}}
</div>
</div>
{{else if eq .Type 14}}
Expand All @@ -277,7 +282,12 @@
{{template "repo/issue/view_content/comments_delete_time" dict "ctxData" $ "comment" .}}
<div class="detail">
{{svg "octicon-clock"}}
<span class="text grey muted-links">{{.Content}}</span>
{{if .RenderedContent}}
{{/* compatibility with time comments made before v1.21 */}}
<span class="text grey muted-links">{{.RenderedContent}}</span>
{{else}}
<span class="text grey muted-links">{{.Content|Sec2Time}}</span>
{{end}}
</div>
</div>
{{else if eq .Type 15}}
Expand Down Expand Up @@ -676,7 +686,12 @@
</span>
<div class="detail">
{{svg "octicon-clock"}}
<span class="text grey muted-links">{{.Content}}</span>
{{if .RenderedContent}}
{{/* compatibility with time comments made before v1.21 */}}
<span class="text grey muted-links">{{.RenderedContent}}</span>
{{else}}
<span class="text grey muted-links">- {{.Content|Sec2Time}}</span>
{{end}}
</div>
</div>
{{else if eq .Type 27}}
Expand Down