Skip to content

Commit 3815a10

Browse files
committed
Bug fixes and tests for modules/base
Also address other TODOs
1 parent f8d94cb commit 3815a10

File tree

4 files changed

+261
-36
lines changed

4 files changed

+261
-36
lines changed

models/issue.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,10 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
915915
}
916916

917917
if len(opts.Labels) > 0 && opts.Labels != "0" {
918-
labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
918+
labelIDs, err := base.StringsToInt64s(strings.Split(opts.Labels, ","))
919+
if err != nil {
920+
return nil, err
921+
}
919922
if len(labelIDs) > 0 {
920923
sess.
921924
Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
@@ -1171,10 +1174,11 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
11711174
And("is_pull = ?", opts.IsPull)
11721175

11731176
if len(opts.Labels) > 0 && opts.Labels != "0" {
1174-
labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
1175-
if len(labelIDs) > 0 {
1176-
sess.
1177-
Join("INNER", "issue_label", "issue.id = issue_id").
1177+
labelIDs, err := base.StringsToInt64s(strings.Split(opts.Labels, ","))
1178+
if err != nil {
1179+
log.Warn("Malformed Labels argument: %s", opts.Labels)
1180+
} else if len(labelIDs) > 0 {
1181+
sess.Join("INNER", "issue_label", "issue.id = issue_id").
11781182
In("label_id", labelIDs)
11791183
}
11801184
}

modules/base/tool.go

+31-14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func DetectEncoding(content []byte) (string, error) {
5656
}
5757

5858
result, err := chardet.NewTextDetector().DetectBest(content)
59+
if err != nil {
60+
return "", err
61+
}
5962
if result.Charset != "UTF-8" && len(setting.Repository.AnsiCharset) > 0 {
6063
log.Debug("Using default AnsiCharset: %s", setting.Repository.AnsiCharset)
6164
return setting.Repository.AnsiCharset, err
@@ -256,19 +259,25 @@ func computeTimeDiff(diff int64) (int64, string) {
256259
diffStr = "1 year"
257260
default:
258261
diffStr = fmt.Sprintf("%d years", diff/Year)
259-
diff = 0
262+
diff -= (diff / Year) * Year
260263
}
261264
return diff, diffStr
262265
}
263266

264267
// TimeSincePro calculates the time interval and generate full user-friendly string.
265268
func TimeSincePro(then time.Time) string {
266-
now := time.Now()
269+
return timeSincePro(then, time.Now())
270+
}
271+
272+
func timeSincePro(then, now time.Time) string {
267273
diff := now.Unix() - then.Unix()
268274

269275
if then.After(now) {
270276
return "future"
271277
}
278+
if diff == 0 {
279+
return "now"
280+
}
272281

273282
var timeStr, diffStr string
274283
for {
@@ -282,9 +291,7 @@ func TimeSincePro(then time.Time) string {
282291
return strings.TrimPrefix(timeStr, ", ")
283292
}
284293

285-
func timeSince(then time.Time, lang string) string {
286-
now := time.Now()
287-
294+
func timeSince(then, now time.Time, lang string) string {
288295
lbl := i18n.Tr(lang, "tool.ago")
289296
diff := now.Unix() - then.Unix()
290297
if then.After(now) {
@@ -295,7 +302,7 @@ func timeSince(then time.Time, lang string) string {
295302
switch {
296303
case diff <= 0:
297304
return i18n.Tr(lang, "tool.now")
298-
case diff <= 2:
305+
case diff <= 1:
299306
return i18n.Tr(lang, "tool.1s", lbl)
300307
case diff < 1*Minute:
301308
return i18n.Tr(lang, "tool.seconds", diff, lbl)
@@ -334,12 +341,18 @@ func timeSince(then time.Time, lang string) string {
334341

335342
// RawTimeSince retrieves i18n key of time since t
336343
func RawTimeSince(t time.Time, lang string) string {
337-
return timeSince(t, lang)
344+
return timeSince(t, time.Now(), lang)
338345
}
339346

340347
// TimeSince calculates the time interval and generate user-friendly string.
341-
func TimeSince(t time.Time, lang string) template.HTML {
342-
return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`, t.Format(setting.TimeFormat), timeSince(t, lang)))
348+
func TimeSince(then time.Time, lang string) template.HTML {
349+
return htmlTimeSince(then, time.Now(), lang)
350+
}
351+
352+
func htmlTimeSince(then, now time.Time, lang string) template.HTML {
353+
return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`,
354+
then.Format(setting.TimeFormat),
355+
timeSince(then, now, lang)))
343356
}
344357

345358
// Storage space size types
@@ -424,10 +437,10 @@ func Subtract(left interface{}, right interface{}) interface{} {
424437
case int64:
425438
rright = right.(int64)
426439
case float32:
427-
fright = float64(left.(float32))
440+
fright = float64(right.(float32))
428441
isInt = false
429442
case float64:
430-
fleft = left.(float64)
443+
fright = right.(float64)
431444
isInt = false
432445
}
433446

@@ -459,12 +472,16 @@ func TruncateString(str string, limit int) string {
459472
}
460473

461474
// StringsToInt64s converts a slice of string to a slice of int64.
462-
func StringsToInt64s(strs []string) []int64 {
475+
func StringsToInt64s(strs []string) ([]int64, error) {
463476
ints := make([]int64, len(strs))
464477
for i := range strs {
465-
ints[i] = com.StrTo(strs[i]).MustInt64()
478+
n, err := com.StrTo(strs[i]).Int64()
479+
if err != nil {
480+
return ints, err
481+
}
482+
ints[i] = n
466483
}
467-
return ints
484+
return ints, nil
468485
}
469486

470487
// Int64sToStrings converts a slice of int64 to a slice of string.

0 commit comments

Comments
 (0)