Skip to content

Commit 294366c

Browse files
committed
fix #67: improve suggest command to support edge case with the last Sunday
1 parent a33aefc commit 294366c

File tree

6 files changed

+87
-45
lines changed

6 files changed

+87
-45
lines changed

internal/command/github/contribution.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ func Contribution(cnf *config.Tool) *cobra.Command {
360360
date, err = time.Parse(time.RFC3339Month, input)
361361
case len(time.RFC3339Day):
362362
date, err = time.Parse(time.RFC3339Day, input)
363-
date = time.TruncateToWeek(date)
364363
default:
365364
err = fmt.Errorf("unsupported format")
366365
}
@@ -370,7 +369,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
370369
}
371370

372371
// data provisioning
373-
start := time.TruncateToWeek(date) // Monday
372+
start := time.TruncateToWeek(date)
374373
scope := time.NewRange(
375374
start.Add(-2*time.Week-time.Day), // buffer from left side with Sunday
376375
time.Now().UTC(),

internal/command/github/view/diff.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,17 @@ func Diff(
2727
{Align: simpletable.AlignLeft, Text: "Day / Week"},
2828
},
2929
}
30-
for i, week := range data {
31-
if shiftIsNeeded(i, week.Report) {
32-
continue
33-
}
30+
for _, week := range data {
3431
table.Header.Cells = append(table.Header.Cells, &simpletable.Cell{
3532
Align: simpletable.AlignCenter,
3633
Text: fmt.Sprintf("#%d", week.Number),
3734
})
3835
}
3936

40-
// shift Sunday to the right
41-
row := make([]*simpletable.Cell, 0, 4)
42-
row = append(row, &simpletable.Cell{Text: time.Sunday.String()})
43-
for i := range data {
44-
if shiftIsNeeded(i, data[i].Report) {
45-
continue
46-
}
47-
// TODO:unclear explain
48-
if i == 0 {
49-
row = append(row, &simpletable.Cell{Align: simpletable.AlignCenter, Text: "-"})
50-
continue
51-
}
52-
txt := "-"
53-
if count := data[i-1].Report[time.Sunday]; count != 0 {
54-
txt = fmt.Sprintf("%+d", count)
55-
}
56-
row = append(row, &simpletable.Cell{Align: simpletable.AlignCenter, Text: txt})
57-
}
58-
table.Body.Cells = append(table.Body.Cells, row)
59-
60-
for i := time.Monday; i <= time.Saturday; i++ {
61-
row = make([]*simpletable.Cell, 0, 4)
37+
for i := time.Sunday; i <= time.Saturday; i++ {
38+
row := make([]*simpletable.Cell, 0, len(data)+1)
6239
row = append(row, &simpletable.Cell{Text: i.String()})
63-
for j, week := range data {
64-
if shiftIsNeeded(j, week.Report) {
65-
continue
66-
}
40+
for _, week := range data {
6741
txt := "-"
6842
if count := week.Report[i]; count != 0 {
6943
txt = fmt.Sprintf("%+d", count)

internal/command/github/view/helpers.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,28 @@ func prepare(heatmap contribution.HeatMap) []WeekReport {
8888
report = append(report, row)
8989
}
9090

91-
return report
92-
}
91+
// shift Sunday to the right and cleanup empty weeks
92+
last := len(report) - 1
93+
if last > -1 {
94+
_, week := heatmap.To().Add(time.Week).ISOWeek()
95+
report = append(report, WeekReport{
96+
Number: week,
97+
Report: make(map[time.Weekday]int),
98+
})
99+
for i := last + 1; i > 0; i-- {
100+
if count, present := report[i-1].Report[time.Sunday]; present {
101+
report[i].Report[time.Sunday] = count
102+
delete(report[i-1].Report, time.Sunday)
103+
}
104+
}
105+
}
106+
cleaned := make([]WeekReport, 0, len(report))
107+
for _, row := range report {
108+
if len(row.Report) > 0 {
109+
cleaned = append(cleaned, row)
110+
}
111+
}
112+
report = cleaned
93113

94-
// If it's a first-week report with a single entry for Sunday,
95-
// we skip it completely.
96-
//
97-
// It's because GitHub shows the contribution chart started on Sunday
98-
// of the previous week. For that reason we have to shift it to the right
99-
// and compensate `.Shift(-time.Day)` call for the scope.
100-
func shiftIsNeeded(idx int, report map[time.Weekday]int) bool {
101-
_, is := report[time.Sunday]
102-
return idx == 0 && len(report) == 1 && is
114+
return report
103115
}

internal/command/github/view/suggest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func Suggest(
3636
suggestion = fmt.Sprintf("%dd", option.Suggest.Day.Sub(now)/xtime.Day)
3737
} else {
3838
day := xtime.CopyClock(now, option.Suggest.Day).In(time.Local)
39-
suggestion = fmt.Sprintf("%s", day.Format(time.RFC3339))
39+
suggestion = day.Format(time.RFC3339)
4040
}
4141
if option.Short {
4242
printer.Println(suggestion)

internal/pkg/time/range.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ func TruncateToDay(t time.Time) time.Time {
164164
}
165165

166166
func TruncateToWeek(t time.Time) time.Time {
167-
return TruncateToDay(t).Add(Day * time.Duration(time.Monday-t.Weekday()))
167+
day := t.Weekday()
168+
if day == time.Sunday {
169+
day = 7
170+
}
171+
return TruncateToDay(t).Add(Day * time.Duration(time.Monday-day))
168172
}
169173

170174
func TruncateToMonth(t time.Time) time.Time {

internal/pkg/time/range_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package time_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
9+
. "go.octolab.org/toolset/maintainer/internal/pkg/time"
10+
)
11+
12+
func TestTruncateToWeek(t *testing.T) {
13+
tests := map[string]struct {
14+
day time.Time
15+
want time.Time
16+
}{
17+
time.Monday.String(): {
18+
time.Date(2022, 07, 4, 0, 0, 0, 0, time.UTC),
19+
time.Date(2022, 07, 4, 0, 0, 0, 0, time.UTC),
20+
},
21+
time.Tuesday.String(): {
22+
time.Date(2022, 7, 5, 0, 0, 0, 0, time.UTC),
23+
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
24+
},
25+
time.Wednesday.String(): {
26+
time.Date(2022, 7, 6, 0, 0, 0, 0, time.UTC),
27+
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
28+
},
29+
time.Thursday.String(): {
30+
time.Date(2022, 7, 7, 0, 0, 0, 0, time.UTC),
31+
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
32+
},
33+
time.Friday.String(): {
34+
time.Date(2022, 7, 5, 0, 0, 0, 0, time.UTC),
35+
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
36+
},
37+
time.Saturday.String(): {
38+
time.Date(2022, 7, 9, 0, 0, 0, 0, time.UTC),
39+
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
40+
},
41+
time.Sunday.String(): {
42+
time.Date(2022, 7, 10, 0, 0, 0, 0, time.UTC),
43+
time.Date(2022, 7, 4, 0, 0, 0, 0, time.UTC),
44+
},
45+
}
46+
47+
for name, test := range tests {
48+
t.Run(name, func(t *testing.T) {
49+
got := TruncateToWeek(test.day)
50+
assert.Equal(t, test.want, got)
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)