mirror of https://github.com/go-gitea/gitea
Refactor helper functions
This commit is contained in:
parent
40e1373dce
commit
e9afd60d6b
|
@ -5,13 +5,6 @@
|
|||
package issues
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
project_model "code.gitea.io/gitea/models/project"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
|
@ -21,6 +14,9 @@ import (
|
|||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
@ -877,93 +873,6 @@ func IsErrIssueMaxPinReached(err error) bool {
|
|||
return err == ErrIssueMaxPinReached
|
||||
}
|
||||
|
||||
var (
|
||||
// Time estimate match regex
|
||||
rTimeEstimateOnlyHours = regexp.MustCompile(`^([\d]+)$`)
|
||||
rTimeEstimateWeeks = regexp.MustCompile(`([\d]+)w`)
|
||||
rTimeEstimateDays = regexp.MustCompile(`([\d]+)d`)
|
||||
rTimeEstimateHours = regexp.MustCompile(`([\d]+)h`)
|
||||
rTimeEstimateMinutes = regexp.MustCompile(`([\d]+)m`)
|
||||
)
|
||||
|
||||
// TimeEstimateFromStr returns time estimate in seconds from formatted string
|
||||
func (issue *Issue) TimeEstimateFromStr(timeStr string) int64 {
|
||||
timeTotal := 0
|
||||
|
||||
// If single number entered, assume hours
|
||||
timeStrMatches := rTimeEstimateOnlyHours.FindStringSubmatch(timeStr)
|
||||
if len(timeStrMatches) > 0 {
|
||||
raw, _ := strconv.Atoi(timeStrMatches[1])
|
||||
timeTotal += raw * (60 * 60)
|
||||
} else {
|
||||
// Find time weeks
|
||||
timeStrMatches = rTimeEstimateWeeks.FindStringSubmatch(timeStr)
|
||||
if len(timeStrMatches) > 0 {
|
||||
raw, _ := strconv.Atoi(timeStrMatches[1])
|
||||
timeTotal += raw * (60 * 60 * 24 * 7)
|
||||
}
|
||||
|
||||
// Find time days
|
||||
timeStrMatches = rTimeEstimateDays.FindStringSubmatch(timeStr)
|
||||
if len(timeStrMatches) > 0 {
|
||||
raw, _ := strconv.Atoi(timeStrMatches[1])
|
||||
timeTotal += raw * (60 * 60 * 24)
|
||||
}
|
||||
|
||||
// Find time hours
|
||||
timeStrMatches = rTimeEstimateHours.FindStringSubmatch(timeStr)
|
||||
if len(timeStrMatches) > 0 {
|
||||
raw, _ := strconv.Atoi(timeStrMatches[1])
|
||||
timeTotal += raw * (60 * 60)
|
||||
}
|
||||
|
||||
// Find time minutes
|
||||
timeStrMatches = rTimeEstimateMinutes.FindStringSubmatch(timeStr)
|
||||
if len(timeStrMatches) > 0 {
|
||||
raw, _ := strconv.Atoi(timeStrMatches[1])
|
||||
timeTotal += raw * (60)
|
||||
}
|
||||
}
|
||||
|
||||
return int64(timeTotal)
|
||||
}
|
||||
|
||||
// TimeEstimateStr returns formatted time estimate string from seconds (e.g. "2w 4d 12h 5m")
|
||||
func (issue *Issue) TimeEstimateToStr() string {
|
||||
var timeParts []string
|
||||
|
||||
timeSeconds := float64(issue.TimeEstimate)
|
||||
|
||||
// Format weeks
|
||||
weeks := math.Floor(timeSeconds / (60 * 60 * 24 * 7))
|
||||
if weeks > 0 {
|
||||
timeParts = append(timeParts, fmt.Sprintf("%dw", int64(weeks)))
|
||||
}
|
||||
timeSeconds -= weeks * (60 * 60 * 24 * 7)
|
||||
|
||||
// Format days
|
||||
days := math.Floor(timeSeconds / (60 * 60 * 24))
|
||||
if days > 0 {
|
||||
timeParts = append(timeParts, fmt.Sprintf("%dd", int64(days)))
|
||||
}
|
||||
timeSeconds -= days * (60 * 60 * 24)
|
||||
|
||||
// Format hours
|
||||
hours := math.Floor(timeSeconds / (60 * 60))
|
||||
if hours > 0 {
|
||||
timeParts = append(timeParts, fmt.Sprintf("%dh", int64(hours)))
|
||||
}
|
||||
timeSeconds -= hours * (60 * 60)
|
||||
|
||||
// Format minutes
|
||||
minutes := math.Floor(timeSeconds / (60))
|
||||
if minutes > 0 {
|
||||
timeParts = append(timeParts, fmt.Sprintf("%dm", int64(minutes)))
|
||||
}
|
||||
|
||||
return strings.Join(timeParts, " ")
|
||||
}
|
||||
|
||||
// ChangeIssueTimeEstimate changes the plan time of this issue, as the given user.
|
||||
func ChangeIssueTimeEstimate(issue *Issue, doer *user_model.User, timeEstimate int64) (err error) {
|
||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
package timeutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
// Time estimate match regex
|
||||
rTimeEstimateOnlyHours = regexp.MustCompile(`^([\d]+)$`)
|
||||
rTimeEstimateWeeks = regexp.MustCompile(`([\d]+)w`)
|
||||
rTimeEstimateDays = regexp.MustCompile(`([\d]+)d`)
|
||||
rTimeEstimateHours = regexp.MustCompile(`([\d]+)h`)
|
||||
rTimeEstimateMinutes = regexp.MustCompile(`([\d]+)m`)
|
||||
)
|
||||
|
||||
// TimeEstimateFromStr returns time estimate in seconds from formatted string
|
||||
func TimeEstimateFromStr(timeStr string) int64 {
|
||||
timeTotal := 0
|
||||
|
||||
// If single number entered, assume hours
|
||||
timeStrMatches := rTimeEstimateOnlyHours.FindStringSubmatch(timeStr)
|
||||
if len(timeStrMatches) > 0 {
|
||||
raw, _ := strconv.Atoi(timeStrMatches[1])
|
||||
timeTotal += raw * (60 * 60)
|
||||
} else {
|
||||
// Find time weeks
|
||||
timeStrMatches = rTimeEstimateWeeks.FindStringSubmatch(timeStr)
|
||||
if len(timeStrMatches) > 0 {
|
||||
raw, _ := strconv.Atoi(timeStrMatches[1])
|
||||
timeTotal += raw * (60 * 60 * 24 * 7)
|
||||
}
|
||||
|
||||
// Find time days
|
||||
timeStrMatches = rTimeEstimateDays.FindStringSubmatch(timeStr)
|
||||
if len(timeStrMatches) > 0 {
|
||||
raw, _ := strconv.Atoi(timeStrMatches[1])
|
||||
timeTotal += raw * (60 * 60 * 24)
|
||||
}
|
||||
|
||||
// Find time hours
|
||||
timeStrMatches = rTimeEstimateHours.FindStringSubmatch(timeStr)
|
||||
if len(timeStrMatches) > 0 {
|
||||
raw, _ := strconv.Atoi(timeStrMatches[1])
|
||||
timeTotal += raw * (60 * 60)
|
||||
}
|
||||
|
||||
// Find time minutes
|
||||
timeStrMatches = rTimeEstimateMinutes.FindStringSubmatch(timeStr)
|
||||
if len(timeStrMatches) > 0 {
|
||||
raw, _ := strconv.Atoi(timeStrMatches[1])
|
||||
timeTotal += raw * (60)
|
||||
}
|
||||
}
|
||||
|
||||
return int64(timeTotal)
|
||||
}
|
||||
|
||||
// TimeEstimateStr returns formatted time estimate string from seconds (e.g. "2w 4d 12h 5m")
|
||||
func TimeEstimateToStr(amount int64) string {
|
||||
var timeParts []string
|
||||
|
||||
timeSeconds := float64(amount)
|
||||
|
||||
// Format weeks
|
||||
weeks := math.Floor(timeSeconds / (60 * 60 * 24 * 7))
|
||||
if weeks > 0 {
|
||||
timeParts = append(timeParts, fmt.Sprintf("%dw", int64(weeks)))
|
||||
}
|
||||
timeSeconds -= weeks * (60 * 60 * 24 * 7)
|
||||
|
||||
// Format days
|
||||
days := math.Floor(timeSeconds / (60 * 60 * 24))
|
||||
if days > 0 {
|
||||
timeParts = append(timeParts, fmt.Sprintf("%dd", int64(days)))
|
||||
}
|
||||
timeSeconds -= days * (60 * 60 * 24)
|
||||
|
||||
// Format hours
|
||||
hours := math.Floor(timeSeconds / (60 * 60))
|
||||
if hours > 0 {
|
||||
timeParts = append(timeParts, fmt.Sprintf("%dh", int64(hours)))
|
||||
}
|
||||
timeSeconds -= hours * (60 * 60)
|
||||
|
||||
// Format minutes
|
||||
minutes := math.Floor(timeSeconds / (60))
|
||||
if minutes > 0 {
|
||||
timeParts = append(timeParts, fmt.Sprintf("%dm", int64(minutes)))
|
||||
}
|
||||
|
||||
return strings.Join(timeParts, " ")
|
||||
}
|
|
@ -2096,7 +2096,7 @@ func UpdateIssueTimeEstimate(ctx *context.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
total := issue.TimeEstimateFromStr(timeStr)
|
||||
total := timeutil.TimeEstimateFromStr(timeStr)
|
||||
|
||||
// User entered something wrong
|
||||
if total == 0 && len(timeStr) != 0 {
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"code.gitea.io/gitea/models/db"
|
||||
issues_model "code.gitea.io/gitea/models/issues"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/services/forms"
|
||||
|
@ -34,7 +35,7 @@ func AddTimeManually(c *context.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
total := issue.TimeEstimateFromStr(form.TimeString)
|
||||
total := timeutil.TimeEstimateFromStr(form.TimeString)
|
||||
|
||||
if total <= 0 {
|
||||
c.Flash.Error(c.Tr("repo.issues.add_time_sum_to_small"))
|
||||
|
|
|
@ -4,11 +4,10 @@
|
|||
package issue
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"fmt"
|
||||
"html"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -32,54 +31,8 @@ const (
|
|||
secondsByMonth = 4 * secondsByWeek // seconds in a month
|
||||
)
|
||||
|
||||
var reDuration = regexp.MustCompile(`(?i)^(?:(\d+([\.,]\d+)?)(?:mo))?(?:(\d+([\.,]\d+)?)(?:w))?(?:(\d+([\.,]\d+)?)(?:d))?(?:(\d+([\.,]\d+)?)(?:h))?(?:(\d+([\.,]\d+)?)(?:m))?$`)
|
||||
|
||||
// timeLogToAmount parses time log string and returns amount in seconds
|
||||
func timeLogToAmount(str string) int64 {
|
||||
matches := reDuration.FindAllStringSubmatch(str, -1)
|
||||
if len(matches) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
match := matches[0]
|
||||
|
||||
var a int64
|
||||
|
||||
// months
|
||||
if len(match[1]) > 0 {
|
||||
mo, _ := strconv.ParseFloat(strings.Replace(match[1], ",", ".", 1), 64)
|
||||
a += int64(mo * secondsByMonth)
|
||||
}
|
||||
|
||||
// weeks
|
||||
if len(match[3]) > 0 {
|
||||
w, _ := strconv.ParseFloat(strings.Replace(match[3], ",", ".", 1), 64)
|
||||
a += int64(w * secondsByWeek)
|
||||
}
|
||||
|
||||
// days
|
||||
if len(match[5]) > 0 {
|
||||
d, _ := strconv.ParseFloat(strings.Replace(match[5], ",", ".", 1), 64)
|
||||
a += int64(d * secondsByDay)
|
||||
}
|
||||
|
||||
// hours
|
||||
if len(match[7]) > 0 {
|
||||
h, _ := strconv.ParseFloat(strings.Replace(match[7], ",", ".", 1), 64)
|
||||
a += int64(h * secondsByHour)
|
||||
}
|
||||
|
||||
// minutes
|
||||
if len(match[9]) > 0 {
|
||||
d, _ := strconv.ParseFloat(strings.Replace(match[9], ",", ".", 1), 64)
|
||||
a += int64(d * secondsByMinute)
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
func issueAddTime(issue *issues_model.Issue, doer *user_model.User, time time.Time, timeLog string) error {
|
||||
amount := timeLogToAmount(timeLog)
|
||||
amount := timeutil.TimeEstimateFromStr(timeLog)
|
||||
if amount == 0 {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -295,7 +295,7 @@
|
|||
<form method="POST" id="set_time_estimate_form" class="gt-mt-3" action="{{.Issue.Link}}/time_estimate">
|
||||
{{$.CsrfTokenHtml}}
|
||||
<div class="ui input fluid">
|
||||
<input name="time_estimate" placeholder='{{.locale.Tr "repo.issues.add_time_estimate"}}' value="{{($.Issue.TimeEstimateToStr)}}" type="text" >
|
||||
<input name="time_estimate" placeholder='{{.locale.Tr "repo.issues.add_time_estimate"}}' value="{{($.timeutil.TimeEstimateToStr .timeutil.TimeEstimate)}}" type="text" >
|
||||
</div>
|
||||
<button class="ui fluid button green tooltip gt-mt-3">
|
||||
{{.locale.Tr "repo.issues.save"}}
|
||||
|
|
Loading…
Reference in New Issue