Skip to content

Commit

Permalink
Merge remote-tracking branch 'giteaofficial/main'
Browse files Browse the repository at this point in the history
* giteaofficial/main:
  Various Mermaid improvements (go-gitea#18776)
  [skip ci] Updated translations via Crowdin
  Fix display time of milestones (go-gitea#18753)
  • Loading branch information
zjjhot committed Feb 16, 2022
2 parents 9b30dbb + 616146f commit cce9d48
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 91 deletions.
34 changes: 3 additions & 31 deletions models/issue_stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
)

// ErrIssueStopwatchNotExist represents an error that stopwatch is not exist
Expand Down Expand Up @@ -53,7 +54,7 @@ func (s Stopwatch) Seconds() int64 {

// Duration returns a human-readable duration string based on local server time
func (s Stopwatch) Duration() string {
return SecToTime(s.Seconds())
return util.SecToTime(s.Seconds())
}

func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
Expand Down Expand Up @@ -164,7 +165,7 @@ func FinishIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss
Doer: user,
Issue: issue,
Repo: issue.Repo,
Content: SecToTime(timediff),
Content: util.SecToTime(timediff),
Type: CommentTypeStopTracking,
TimeID: tt.ID,
}); err != nil {
Expand Down Expand Up @@ -263,32 +264,3 @@ func cancelStopwatch(ctx context.Context, user *user_model.User, issue *Issue) e
}
return nil
}

// SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s)
func SecToTime(duration int64) string {
seconds := duration % 60
minutes := (duration / (60)) % 60
hours := duration / (60 * 60)

var hrs string

if hours > 0 {
hrs = fmt.Sprintf("%dh", hours)
}
if minutes > 0 {
if hours == 0 {
hrs = fmt.Sprintf("%dmin", minutes)
} else {
hrs = fmt.Sprintf("%s %dmin", hrs, minutes)
}
}
if seconds > 0 {
if hours == 0 && minutes == 0 {
hrs = fmt.Sprintf("%ds", seconds)
} else {
hrs = fmt.Sprintf("%s %ds", hrs, seconds)
}
}

return hrs
}
9 changes: 5 additions & 4 deletions models/issue_tracked_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
)
Expand Down Expand Up @@ -177,7 +178,7 @@ func AddTime(user *user_model.User, issue *Issue, amount int64, created time.Tim
Issue: issue,
Repo: issue.Repo,
Doer: user,
Content: SecToTime(amount),
Content: util.SecToTime(amount),
Type: CommentTypeAddTimeManual,
TimeID: t.ID,
}); err != nil {
Expand Down Expand Up @@ -226,7 +227,7 @@ func TotalTimes(options *FindTrackedTimesOptions) (map[*user_model.User]string,
}
return nil, err
}
totalTimes[user] = SecToTime(total)
totalTimes[user] = util.SecToTime(total)
}
return totalTimes, nil
}
Expand Down Expand Up @@ -260,7 +261,7 @@ func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error {
Issue: issue,
Repo: issue.Repo,
Doer: user,
Content: "- " + SecToTime(removedTime),
Content: "- " + util.SecToTime(removedTime),
Type: CommentTypeDeleteTimeManual,
}); err != nil {
return err
Expand Down Expand Up @@ -289,7 +290,7 @@ func DeleteTime(t *TrackedTime) error {
Issue: t.Issue,
Repo: t.Issue.Repo,
Doer: t.User,
Content: "- " + SecToTime(t.Time),
Content: "- " + util.SecToTime(t.Time),
Type: CommentTypeDeleteTimeManual,
}); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions models/issue_tracked_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAddTime(t *testing.T) {
assert.Equal(t, int64(3661), tt.Time)

comment := unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*Comment)
assert.Equal(t, comment.Content, "1h 1min 1s")
assert.Equal(t, comment.Content, "1h 1m 1s")
}

func TestGetTrackedTimes(t *testing.T) {
Expand Down Expand Up @@ -86,15 +86,15 @@ func TestTotalTimes(t *testing.T) {
assert.Len(t, total, 1)
for user, time := range total {
assert.Equal(t, int64(1), user.ID)
assert.Equal(t, "6min 40s", time)
assert.Equal(t, "6m 40s", time)
}

total, err = TotalTimes(&FindTrackedTimesOptions{IssueID: 2})
assert.NoError(t, err)
assert.Len(t, total, 2)
for user, time := range total {
if user.ID == 2 {
assert.Equal(t, "1h 1min 2s", time)
assert.Equal(t, "1h 1m 2s", time)
} else if user.ID == 1 {
assert.Equal(t, "20s", time)
} else {
Expand Down
4 changes: 2 additions & 2 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func NewFuncMap() []template.FuncMap {
},
"Printf": fmt.Sprintf,
"Escape": Escape,
"Sec2Time": models.SecToTime,
"Sec2Time": util.SecToTime,
"ParseDeadline": func(deadline string) []string {
return strings.Split(deadline, "|")
},
Expand Down Expand Up @@ -447,7 +447,7 @@ func NewTextFuncMap() []texttmpl.FuncMap {
},
"Printf": fmt.Sprintf,
"Escape": Escape,
"Sec2Time": models.SecToTime,
"Sec2Time": util.SecToTime,
"ParseDeadline": func(deadline string) []string {
return strings.Split(deadline, "|")
},
Expand Down
44 changes: 44 additions & 0 deletions modules/util/sec_to_time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 Gitea. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package util

import "fmt"

// SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s)
func SecToTime(duration int64) string {
seconds := duration % 60
minutes := (duration / (60)) % 60
hours := duration / (60 * 60) % 24
days := duration / (60 * 60) / 24

var formattedTime string

if days > 0 {
formattedTime = fmt.Sprintf("%dd", days)
}
if hours > 0 {
if formattedTime == "" {
formattedTime = fmt.Sprintf("%dh", hours)
} else {
formattedTime = fmt.Sprintf("%s %dh", formattedTime, hours)
}
}
if minutes > 0 {
if formattedTime == "" {
formattedTime = fmt.Sprintf("%dm", minutes)
} else {
formattedTime = fmt.Sprintf("%s %dm", formattedTime, minutes)
}
}
if seconds > 0 {
if formattedTime == "" {
formattedTime = fmt.Sprintf("%ds", seconds)
} else {
formattedTime = fmt.Sprintf("%s %ds", formattedTime, seconds)
}
}

return formattedTime
}
20 changes: 20 additions & 0 deletions modules/util/sec_to_time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2022 Gitea. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSecToTime(t *testing.T) {
assert.Equal(t, SecToTime(10), "10s")
assert.Equal(t, SecToTime(100), "1m 40s")
assert.Equal(t, SecToTime(1000), "16m 40s")
assert.Equal(t, SecToTime(10000), "2h 46m 40s")
assert.Equal(t, SecToTime(100000), "1d 3h 46m 40s")
assert.Equal(t, SecToTime(1000000), "11d 13h 46m 40s")
}
5 changes: 5 additions & 0 deletions options/locale/locale_zh-TW.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2581,9 +2581,13 @@ auths.filter=使用者篩選器
auths.admin_filter=管理者篩選器
auths.restricted_filter=受限制的篩選器
auths.restricted_filter_helper=留白則不限制任何使用者。使用米字「*」將所有不符合管理員篩選條件的使用者設定為受限。
auths.verify_group_membership=驗證 LDAP 群組成員資格(篩選器留空以跳過)
auths.group_search_base=群組搜尋的 Base DN
auths.group_attribute_list_users=包含使用者清單的群組屬性
auths.user_attribute_in_group=群組中列出的使用者屬性
auths.map_group_to_team=對應 LDAP 群組到組織團隊(欄位留空以跳過)
auths.map_group_to_team_removal=如果使用者不屬於相對應的 LDAP 群組,將使用者從已同步的團隊移除。
auths.enable_ldap_groups=啟用 LDAP 群組
auths.ms_ad_sa=MS AD 搜尋屬性
auths.smtp_auth=SMTP 驗證類型
auths.smtphost=SMTP 主機地址
Expand Down Expand Up @@ -2816,6 +2820,7 @@ monitor.queue.type=類型
monitor.queue.exemplar=型別
monitor.queue.numberworkers=工作者數量
monitor.queue.maxnumberworkers=最大工作者數量
monitor.queue.numberinqueue=佇列中的數量
monitor.queue.review=檢視組態
monitor.queue.review_add=檢視/新增工作者
monitor.queue.configuration=初始組態
Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"less": "4.1.2",
"less-loader": "10.2.0",
"license-checker-webpack-plugin": "0.2.1",
"mermaid": "8.13.10",
"mermaid": "8.14.0",
"mini-css-extract-plugin": "2.5.3",
"monaco-editor": "0.32.1",
"monaco-editor-webpack-plugin": "7.0.1",
Expand Down
3 changes: 2 additions & 1 deletion routers/web/repo/issue_timetrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/forms"
)
Expand Down Expand Up @@ -81,6 +82,6 @@ func DeleteTime(c *context.Context) {
return
}

c.Flash.Success(c.Tr("repo.issues.del_time_history", models.SecToTime(t.Time)))
c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToTime(t.Time)))
c.Redirect(issue.HTMLURL())
}
Loading

0 comments on commit cce9d48

Please sign in to comment.