Repository navigation
Expand file tree
/
Copy pathtrain.go
More file actions
173 lines (162 loc) · 4.73 KB
/
Copy pathtrain.go
File metadata and controls
173 lines (162 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"sort"
"strconv"
"strings"
"time"
)
const trainStaleDays = 14
type TrainItem struct {
PR *TrackedPR `json:"pr"`
Lane string `json:"lane"`
Step string `json:"step"`
Stale bool `json:"stale"`
OpenDays int `json:"openDays"`
IdleDays int `json:"idleDays"`
}
type trainLaneInfo struct{ Key, Label, Icon, Tone string }
// Return a value so callers cannot change the lane order or vocabulary.
func trainLanes() [7]trainLaneInfo {
return [7]trainLaneInfo{
{"close", "Close: already in base", "check-circle", "muted"},
{"merge", "Merge", "merge", "ok"},
{"rebase", "Rebase", "branch", "bad"},
{"fix", "Fix", "x-circle", "bad"},
{"review", "Review", "person-clock", "warn"},
{"decide", "Decide", "dotted-circle", "muted"},
{"wait", "Waiting", "clock", "info"},
}
}
func trainLaneIndex(key string) int {
for i, lane := range trainLanes() {
if lane.Key == key {
return i
}
}
return len(trainLanes())
}
func trainLane(pr *TrackedPR, now time.Time, staleDays int) TrainItem {
if staleDays <= 0 {
staleDays = trainStaleDays
}
days := func(at time.Time) int {
if at.IsZero() || at.After(now) {
return 0
}
return int(now.Sub(at) / (24 * time.Hour))
}
last := pr.LastCommit
if last.IsZero() {
last = pr.CreatedAt
}
item := TrainItem{PR: pr, OpenDays: days(pr.CreatedAt), IdleDays: days(last)}
item.Stale = item.IdleDays >= staleDays
base := pr.BaseRef
target := "origin/" + base
if base == "" {
base = "the base branch"
target = base
}
local := pr.Local
why := strconv.Quote(pr.Why)
switch {
case local != nil && local.Checked && local.Superseded:
item.Lane = "close"
item.Step = "Every change is already in " + base + "; confirm and close without deleting the branch"
case pr.State == "Ready to merge":
item.Lane = "merge"
item.Step = "Merge into " + base + " respecting branch protection because it is ready to merge"
case pr.State == "Conflicts" || pr.State == "Behind base" || (local != nil && len(local.Conflicts) > 0):
item.Lane = "rebase"
item.Step = "Rebase onto " + target + " because the branch needs updating"
if local != nil && len(local.Conflicts) > 0 {
item.Step += "; resolve conflicts in " + strings.Join(local.Conflicts[:min(3, len(local.Conflicts))], ", ")
}
case pr.State == "Checks failing" || pr.State == "Changes requested":
item.Lane = "fix"
item.Step = "Fix the reported issues: " + why
case pr.State == "Awaiting review":
item.Lane = "review"
waiting := pr.Why
if len(pr.Waiting) > 0 {
waiting = "waiting on " + strings.Join(pr.Waiting, ", ")
}
if waiting == "" {
waiting = "approval is not confirmed"
}
if item.Stale {
item.Step = fmt.Sprintf("Idle %dd: confirm it is still wanted, then re-request review (%s)", item.IdleDays, waiting)
} else {
item.Step = "Request review: " + waiting
}
if local != nil && local.OverlapN > 0 {
item.Step += fmt.Sprintf("; %s changed %d of its files since it branched, so it needs a re-review", base, local.OverlapN)
}
case pr.State == "Draft":
item.Lane = "decide"
item.Step = "Finish this draft or close it because it is not ready for review"
default:
item.Lane = "wait"
item.Step = "Wait for the blocker to clear: " + why
}
if item.Stale && item.Lane != "review" {
item.Step += fmt.Sprintf("; idle %dd, confirm it is still wanted", item.IdleDays)
}
if (item.Lane == "merge" || item.Lane == "review" || item.Lane == "decide") && (local == nil || !local.Checked) {
note := "no matching local repository"
if local != nil {
note = local.Note
if note == "" {
note = "the local check is unavailable"
}
}
item.Step += " (not validated locally: " + note + ")"
}
item.Step += "."
return item
}
func mergeTrain(prs []*TrackedPR, now time.Time, staleDays int) []TrainItem {
items := make([]TrainItem, 0, len(prs))
for _, pr := range prs {
items = append(items, trainLane(pr, now, staleDays))
}
sort.SliceStable(items, func(i, j int) bool {
a, b := items[i], items[j]
if a.Lane != b.Lane {
return trainLaneIndex(a.Lane) < trainLaneIndex(b.Lane)
}
if a.Stale != b.Stale {
return a.Stale
}
return a.PR.CreatedAt.Before(b.PR.CreatedAt)
})
return items
}
type trainSection struct {
trainLaneInfo
Items []TrainItem
}
func trainSections(items []TrainItem) []trainSection {
var sections []trainSection
for _, lane := range trainLanes() {
section := trainSection{trainLaneInfo: lane}
for _, item := range items {
if item.Lane == lane.Key {
section.Items = append(section.Items, item)
}
}
if len(section.Items) > 0 {
sections = append(sections, section)
}
}
return sections
}
func trainActionCount(items []TrainItem) (n int) {
for _, item := range items {
if item.Lane != "wait" {
n++
}
}
return
}