From 352e99aa226200eec375168772999f80a8168652 Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Tue, 30 Dec 2025 12:35:02 +0900 Subject: [PATCH] appview/{db,state}: handle index out-of-bound for punchcards Change-Id: lwwtkzxrqksyvovtkzylpvvrqmmpwmsu The date compare should be done with years. Also, previous puchcard didn't work except for December because `idx` can be negative while iterating the punchcard. Not sure why most account pages aren't affected Signed-off-by: Seongmin Lee --- appview/db/profile.go | 53 +++++++++++++++++----------------------- appview/state/profile.go | 7 +++--- 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/appview/db/profile.go b/appview/db/profile.go index 0184b7ec..39e0f8c9 100644 --- a/appview/db/profile.go +++ b/appview/db/profile.go @@ -16,11 +16,17 @@ import ( const TimeframeMonths = 7 +func MonthsApart(from, to time.Time) int { + fromYear, fromMonth, _ := from.Date() + toYear, toMonth, _ := to.Date() + return (toYear-fromYear)*12 + int(toMonth-fromMonth) +} + func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, error) { timeline := models.ProfileTimeline{ ByMonth: make([]models.ByMonth, TimeframeMonths), } - currentMonth := time.Now().Month() + now := time.Now() timeframe := fmt.Sprintf("-%d months", TimeframeMonths) pulls, err := GetPullsByOwnerDid(e, forDid, timeframe) @@ -30,17 +36,12 @@ func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, erro // group pulls by month for _, pull := range pulls { - pullMonth := pull.Created.Month() + idx := MonthsApart(pull.Created, now) - if currentMonth-pullMonth >= TimeframeMonths { - // shouldn't happen; but times are weird - continue + if 0 <= idx && idx < TimeframeMonths { + items := &timeline.ByMonth[idx].PullEvents.Items + *items = append(*items, &pull) } - - idx := currentMonth - pullMonth - items := &timeline.ByMonth[idx].PullEvents.Items - - *items = append(*items, &pull) } issues, err := GetIssues( @@ -53,17 +54,12 @@ func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, erro } for _, issue := range issues { - issueMonth := issue.Created.Month() + idx := MonthsApart(issue.Created, now) - if currentMonth-issueMonth >= TimeframeMonths { - // shouldn't happen; but times are weird - continue + if 0 <= idx && idx < TimeframeMonths { + items := &timeline.ByMonth[idx].IssueEvents.Items + *items = append(*items, &issue) } - - idx := currentMonth - issueMonth - items := &timeline.ByMonth[idx].IssueEvents.Items - - *items = append(*items, &issue) } repos, err := GetRepos(e, 0, orm.FilterEq("did", forDid)) @@ -81,20 +77,15 @@ func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, erro } } - repoMonth := repo.Created.Month() + idx := MonthsApart(repo.Created, now) - if currentMonth-repoMonth >= TimeframeMonths { - // shouldn't happen; but times are weird - continue + if 0 <= idx && idx < TimeframeMonths { + items := &timeline.ByMonth[idx].RepoEvents + *items = append(*items, models.RepoEvent{ + Repo: &repo, + Source: sourceRepo, + }) } - - idx := currentMonth - repoMonth - - items := &timeline.ByMonth[idx].RepoEvents - *items = append(*items, models.RepoEvent{ - Repo: &repo, - Source: sourceRepo, - }) } return &timeline, nil diff --git a/appview/state/profile.go b/appview/state/profile.go index 400725ac..f52a024d 100644 --- a/appview/state/profile.go +++ b/appview/state/profile.go @@ -163,10 +163,11 @@ func (s *State) profileOverview(w http.ResponseWriter, r *http.Request) { } // populate commit counts in the timeline, using the punchcard - currentMonth := time.Now().Month() + now := time.Now() for _, p := range profile.Punchcard.Punches { - idx := currentMonth - p.Date.Month() - if int(idx) < len(timeline.ByMonth) { + idx := db.MonthsApart(p.Date, now) + + if 0 <= idx && idx < len(timeline.ByMonth) { timeline.ByMonth[idx].Commits += p.Count } } -- 2.43.0