-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.go
More file actions
262 lines (202 loc) · 5.48 KB
/
Copy pathexecution.go
File metadata and controls
262 lines (202 loc) · 5.48 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package simver
import (
"context"
"fmt"
"regexp"
"slices"
"strconv"
"strings"
// exp sort
"github.com/rs/zerolog"
"golang.org/x/mod/semver"
)
type Execution interface {
PR() int
IsTargetingRoot() bool
IsMerge() bool
HeadCommitTags() Tags
HeadBranchTags() Tags
BaseBranchTags() Tags
RootBranchTags() Tags
ProvideRefs() RefProvider
}
const baseTag = "v0.1.0"
func Calculate(ctx context.Context, ex Execution) *Calculation {
mrlt := MostRecentLiveTag(ex)
mrrt := MostRecentReservedTag(ex)
mmrt := MyMostRecentTag(ex)
maxlr := MaxLiveOrReservedTag(mrlt, mrrt)
mmrbn := MyMostRecentBuildNumber(ex)
return &Calculation{
IsMerged: ex.IsMerge(),
MostRecentLiveTag: mrlt,
ForcePatch: ForcePatch(ctx, ex, mmrt),
Skip: Skip(ctx, ex, mmrt),
MyMostRecentTag: mmrt,
MyMostRecentBuild: mmrbn,
PR: ex.PR(),
NextValidTag: GetNextValidTag(ctx, ex.IsTargetingRoot(), maxlr),
}
}
type MRLT string // most recent live tag
type MRRT string // most recent reserved tag
type NVT string // next valid tag
type MMRT string // my most recent tag
type MMRBN int // my most recent build number
type MRT string // my reserved tag
type MAXLR string // max live or reserved tag
type MAXMR string // max my reserved tag
type LST string // assumed last full decorated tag
func MaxLiveOrReservedTag(mrlt MRLT, mrrt MRRT) MAXLR {
return MAXLR(Max(mrlt, mrrt))
}
func MaxMyOrReservedTag(mrrt MRRT, mmrt MMRT) MAXMR {
return MAXMR(Max(mrrt, mmrt))
}
func BumpPatch[S ~string](arg S) S {
maj := semver.MajorMinor(string(arg))
patch := strings.Split(strings.TrimPrefix(string(arg), maj), "-")[0]
patch = strings.TrimPrefix(patch, ".")
if patch == "" {
patch = "0"
}
patchnum, err := strconv.Atoi(patch)
if err != nil {
panic("patchnum is not a number somehow: " + patch)
}
patchnum++
return S(fmt.Sprintf("%s.%d", maj, patchnum))
}
func Skip(ctx context.Context, ee Execution, mmrt MMRT) bool {
reg := regexp.MustCompile(fmt.Sprintf(`^%s$`, mmrt))
// head commit tags matching mmrt
hct := ee.HeadCommitTags().SemversMatching(func(s string) bool {
return reg.MatchString(s)
})
return len(hct) > 0
}
func ForcePatch(ctx context.Context, ee Execution, mmrt MMRT) bool {
// if our head branch has a
reg := regexp.MustCompile(fmt.Sprintf(`^%s$`, mmrt))
// head branch tags matching mmrt
hbt := ee.HeadBranchTags().SemversMatching(func(s string) bool {
return reg.MatchString(s)
})
return len(hbt) > 0
}
func MostRecentLiveTag(e Execution) MRLT {
reg := regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
highest := e.BaseBranchTags().SemversMatching(func(s string) bool {
return reg.MatchString(s)
})
if len(highest) == 0 {
return ""
}
return MRLT(strings.Split(semver.Canonical(highest[len(highest)-1]), "-")[0])
}
func MyMostRecentTag(e Execution) MMRT {
reg := regexp.MustCompile(`^v\d+\.\d+\.\d+.*$`)
highest := e.HeadBranchTags().SemversMatching(func(s string) bool {
if strings.Contains(s, "-reserved") || strings.Contains(s, "-base") {
return false
}
return reg.MatchString(s)
})
if len(highest) == 0 {
return ""
}
return MMRT(strings.Split(semver.Canonical(highest[len(highest)-1]), "-")[0])
}
func MostRecentReservedTag(e Execution) MRRT {
reg := regexp.MustCompile(`^v\d+\.\d+\.\d+-reserved$`)
highest := e.RootBranchTags().SemversMatching(func(s string) bool {
return reg.MatchString(s)
})
if len(highest) == 0 {
return ""
}
return MRRT(strings.Split(semver.Canonical(highest[len(highest)-1]), "-")[0])
}
func MyMostRecentBuildNumber(e Execution) MMRBN {
reg := regexp.MustCompile(fmt.Sprintf(`^.*-pr%d\+\d+$`, e.PR()))
highest := e.HeadBranchTags().SemversMatching(func(s string) bool {
return reg.MatchString(s)
})
if len(highest) == 0 {
return 0
}
slices.SortFunc(highest, func(a, b string) int {
// because we know the regex matches, we know the split will be len 2
// and the second element will be a valid number
ai, _ := strconv.Atoi(strings.Split(a, "+")[1])
bi, _ := strconv.Atoi(strings.Split(b, "+")[1])
return bi - ai
})
high := highest[0]
// get the build number
split := strings.Split(high, "+")
if len(split) != 2 {
return 0
}
n, err := strconv.Atoi(split[1])
if err != nil {
return 0
}
return MMRBN(n)
}
func Max[A ~string, B ~string](a A, b B) string {
var max string
if a == "" || b == "" {
if a != "" {
max = string(a)
} else if b != "" {
max = string(b)
} else {
max = baseTag
}
} else {
// only compare if both exist
if semver.Compare(string(b), string(a)) > 0 {
max = string(b)
} else {
max = string(a)
}
}
return max
}
func GetNextValidTag(ctx context.Context, minor bool, maxt MAXLR) NVT {
var max string
if maxt == "" {
max = baseTag
} else {
max = string(maxt)
}
maj := semver.Major(max) + "."
majmin := semver.MajorMinor(max)
patch := strings.Split(strings.TrimPrefix(max, majmin+"."), "-")[0]
min := strings.TrimPrefix(majmin, maj)
minornum, err := strconv.Atoi(min)
if err != nil {
panic("minornum is not a number somehow: " + min)
}
patchnum, err := strconv.Atoi(patch)
if err != nil {
panic("patchnum is not a number somehow: " + patch)
}
if minor {
minornum++
patchnum = 0
} else {
patchnum++
}
zerolog.Ctx(ctx).Debug().
Str("max", max).
Str("maj", maj).
Str("majmin", majmin).
Str("patch", patch).
Str("min", min).
Int("minornum", minornum).
Int("patchnum", patchnum).
Msg("calculated next valid tag")
return NVT(fmt.Sprintf("%s.%d.%d", semver.Major(max), minornum, patchnum))
}