Skip to content

Commit

Permalink
Bug fixes and tests for modules/base (#442)
Browse files Browse the repository at this point in the history
Also address other TODOs
  • Loading branch information
ethantkoenig authored and lunny committed Dec 22, 2016
1 parent df7fa4e commit 4c89a9c
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 36 deletions.
14 changes: 9 additions & 5 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,10 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
}

if len(opts.Labels) > 0 && opts.Labels != "0" {
labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
labelIDs, err := base.StringsToInt64s(strings.Split(opts.Labels, ","))
if err != nil {
return nil, err
}
if len(labelIDs) > 0 {
sess.
Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
Expand Down Expand Up @@ -1171,10 +1174,11 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
And("is_pull = ?", opts.IsPull)

if len(opts.Labels) > 0 && opts.Labels != "0" {
labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
if len(labelIDs) > 0 {
sess.
Join("INNER", "issue_label", "issue.id = issue_id").
labelIDs, err := base.StringsToInt64s(strings.Split(opts.Labels, ","))
if err != nil {
log.Warn("Malformed Labels argument: %s", opts.Labels)
} else if len(labelIDs) > 0 {
sess.Join("INNER", "issue_label", "issue.id = issue_id").
In("label_id", labelIDs)
}
}
Expand Down
45 changes: 31 additions & 14 deletions modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func DetectEncoding(content []byte) (string, error) {
}

result, err := chardet.NewTextDetector().DetectBest(content)
if err != nil {
return "", err
}
if result.Charset != "UTF-8" && len(setting.Repository.AnsiCharset) > 0 {
log.Debug("Using default AnsiCharset: %s", setting.Repository.AnsiCharset)
return setting.Repository.AnsiCharset, err
Expand Down Expand Up @@ -256,19 +259,25 @@ func computeTimeDiff(diff int64) (int64, string) {
diffStr = "1 year"
default:
diffStr = fmt.Sprintf("%d years", diff/Year)
diff = 0
diff -= (diff / Year) * Year
}
return diff, diffStr
}

// TimeSincePro calculates the time interval and generate full user-friendly string.
func TimeSincePro(then time.Time) string {
now := time.Now()
return timeSincePro(then, time.Now())
}

func timeSincePro(then, now time.Time) string {
diff := now.Unix() - then.Unix()

if then.After(now) {
return "future"
}
if diff == 0 {
return "now"
}

var timeStr, diffStr string
for {
Expand All @@ -282,9 +291,7 @@ func TimeSincePro(then time.Time) string {
return strings.TrimPrefix(timeStr, ", ")
}

func timeSince(then time.Time, lang string) string {
now := time.Now()

func timeSince(then, now time.Time, lang string) string {
lbl := i18n.Tr(lang, "tool.ago")
diff := now.Unix() - then.Unix()
if then.After(now) {
Expand All @@ -295,7 +302,7 @@ func timeSince(then time.Time, lang string) string {
switch {
case diff <= 0:
return i18n.Tr(lang, "tool.now")
case diff <= 2:
case diff <= 1:
return i18n.Tr(lang, "tool.1s", lbl)
case diff < 1*Minute:
return i18n.Tr(lang, "tool.seconds", diff, lbl)
Expand Down Expand Up @@ -334,12 +341,18 @@ func timeSince(then time.Time, lang string) string {

// RawTimeSince retrieves i18n key of time since t
func RawTimeSince(t time.Time, lang string) string {
return timeSince(t, lang)
return timeSince(t, time.Now(), lang)
}

// TimeSince calculates the time interval and generate user-friendly string.
func TimeSince(t time.Time, lang string) template.HTML {
return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`, t.Format(setting.TimeFormat), timeSince(t, lang)))
func TimeSince(then time.Time, lang string) template.HTML {
return htmlTimeSince(then, time.Now(), lang)
}

func htmlTimeSince(then, now time.Time, lang string) template.HTML {
return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`,
then.Format(setting.TimeFormat),
timeSince(then, now, lang)))
}

// Storage space size types
Expand Down Expand Up @@ -424,10 +437,10 @@ func Subtract(left interface{}, right interface{}) interface{} {
case int64:
rright = right.(int64)
case float32:
fright = float64(left.(float32))
fright = float64(right.(float32))
isInt = false
case float64:
fleft = left.(float64)
fright = right.(float64)
isInt = false
}

Expand Down Expand Up @@ -459,12 +472,16 @@ func TruncateString(str string, limit int) string {
}

// StringsToInt64s converts a slice of string to a slice of int64.
func StringsToInt64s(strs []string) []int64 {
func StringsToInt64s(strs []string) ([]int64, error) {
ints := make([]int64, len(strs))
for i := range strs {
ints[i] = com.StrTo(strs[i]).MustInt64()
n, err := com.StrTo(strs[i]).Int64()
if err != nil {
return ints, err
}
ints[i] = n
}
return ints
return ints, nil
}

// Int64sToStrings converts a slice of int64 to a slice of string.
Expand Down
Loading

0 comments on commit 4c89a9c

Please sign in to comment.