refactor(issues): move issue event ordering into the domain
- commit
- ec131d8f5ecbe0d10c74573eac4008aac1913ff2
- author
- brice · · 2026-10-22 18:26 UTC
- committer
- brice · · 2026-10-22 18:41 UTC
- parent
- ccabeff
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
2 files · +14 -18
M internal/adapters/kvstore/issue_links.go +1 -18
| @@ -178,24 +178,7 @@ func (l *IssueLinks) EventsOfMergeRequest(ctx context.Context, repo domain.RepoN | |||
| 178 | 178 | } | |
| 179 | 179 | out = append(out, ev) | |
| 180 | 180 | } | |
| 181 | - | slices.SortFunc(out, func(a, b domain.IssueEvent) int { | |
| 182 | - | if c := a.CreatedAt.Compare(b.CreatedAt); c != 0 { | |
| 183 | - | return c | |
| 184 | - | } | |
| 185 | - | if a.Issue < b.Issue { | |
| 186 | - | return -1 | |
| 187 | - | } | |
| 188 | - | if a.Issue > b.Issue { | |
| 189 | - | return 1 | |
| 190 | - | } | |
| 191 | - | if a.ID < b.ID { | |
| 192 | - | return -1 | |
| 193 | - | } | |
| 194 | - | if a.ID > b.ID { | |
| 195 | - | return 1 | |
| 196 | - | } | |
| 197 | - | return 0 | |
| 198 | - | }) | |
| 181 | + | slices.SortFunc(out, domain.CompareIssueEvents) | |
| 199 | 182 | if len(out) > MaxIssueEvents { | |
| 200 | 183 | out = out[:MaxIssueEvents] | |
| 201 | 184 | } | |
| @@ -178,24 +178,7 @@ func (l *IssueLinks) EventsOfMergeRequest(ctx context.Context, repo domain.RepoN | |||||
| 178 | } | 178 | } | ||
| 179 | out = append(out, ev) | 179 | out = append(out, ev) | ||
| 180 | } | 180 | } | ||
| 181 | - | slices.SortFunc(out, func(a, b domain.IssueEvent) int { | 181 | + | slices.SortFunc(out, domain.CompareIssueEvents) |
| 182 | - | if c := a.CreatedAt.Compare(b.CreatedAt); c != 0 { | |||
| 183 | - | return c | |||
| 184 | - | } | |||
| 185 | - | if a.Issue < b.Issue { | |||
| 186 | - | return -1 | |||
| 187 | - | } | |||
| 188 | - | if a.Issue > b.Issue { | |||
| 189 | - | return 1 | |||
| 190 | - | } | |||
| 191 | - | if a.ID < b.ID { | |||
| 192 | - | return -1 | |||
| 193 | - | } | |||
| 194 | - | if a.ID > b.ID { | |||
| 195 | - | return 1 | |||
| 196 | - | } | |||
| 197 | - | return 0 | |||
| 198 | - | }) | |||
| 199 | if len(out) > MaxIssueEvents { | 182 | if len(out) > MaxIssueEvents { | ||
| 200 | out = out[:MaxIssueEvents] | 183 | out = out[:MaxIssueEvents] | ||
| 201 | } | 184 | } | ||
M internal/domain/issue.go +13 -0
| @@ -3,6 +3,7 @@ | |||
| 3 | 3 | package domain | |
| 4 | 4 | ||
| 5 | 5 | import ( | |
| 6 | + | "cmp" | |
| 6 | 7 | "fmt" | |
| 7 | 8 | "time" | |
| 8 | 9 | ) | |
| @@ -139,3 +140,15 @@ type IssueEvent struct { | |||
| 139 | 140 | Actor string | |
| 140 | 141 | CreatedAt time.Time | |
| 141 | 142 | } | |
| 143 | + | ||
| 144 | + | // CompareIssueEvents orders events by time, then issue number, then id. It | |
| 145 | + | // suits slices.SortFunc. | |
| 146 | + | func CompareIssueEvents(a, b IssueEvent) int { | |
| 147 | + | if c := a.CreatedAt.Compare(b.CreatedAt); c != 0 { | |
| 148 | + | return c | |
| 149 | + | } | |
| 150 | + | if c := cmp.Compare(a.Issue, b.Issue); c != 0 { | |
| 151 | + | return c | |
| 152 | + | } | |
| 153 | + | return cmp.Compare(a.ID, b.ID) | |
| 154 | + | } | |
| @@ -3,6 +3,7 @@ | |||||
| 3 | package domain | 3 | package domain | ||
| 4 | 4 | ||||
| 5 | import ( | 5 | import ( | ||
| 6 | + | "cmp" | |||
| 6 | "fmt" | 7 | "fmt" | ||
| 7 | "time" | 8 | "time" | ||
| 8 | ) | 9 | ) | ||
| @@ -139,3 +140,15 @@ type IssueEvent struct { | |||||
| 139 | Actor string | 140 | Actor string | ||
| 140 | CreatedAt time.Time | 141 | CreatedAt time.Time | ||
| 141 | } | 142 | } | ||
| 143 | + | ||||
| 144 | + | // CompareIssueEvents orders events by time, then issue number, then id. It | |||
| 145 | + | // suits slices.SortFunc. | |||
| 146 | + | func CompareIssueEvents(a, b IssueEvent) int { | |||
| 147 | + | if c := a.CreatedAt.Compare(b.CreatedAt); c != 0 { | |||
| 148 | + | return c | |||
| 149 | + | } | |||
| 150 | + | if c := cmp.Compare(a.Issue, b.Issue); c != 0 { | |||
| 151 | + | return c | |||
| 152 | + | } | |||
| 153 | + | return cmp.Compare(a.ID, b.ID) | |||
| 154 | + | } | |||