-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate.go
137 lines (117 loc) · 3.18 KB
/
calculate.go
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
package simver
import (
"context"
"fmt"
"github.com/rs/zerolog"
"golang.org/x/mod/semver"
)
type Calculation struct {
MyMostRecentTag MMRT
MostRecentLiveTag MRLT
MyMostRecentBuild MMRBN
PR int
NextValidTag NVT
IsMerged bool
ForcePatch bool
Skip bool
}
type CalculationOutput struct {
BaseTags []string
HeadTags []string
RootTags []string
MergeTags []string
}
func (me *CalculationOutput) CurrentBuildTag(opts RefProvider) (string, string) {
if len(me.HeadTags) == 0 {
if len(me.MergeTags) == 0 {
return "", ""
} else {
return me.MergeTags[0], opts.Merge()
}
}
return me.HeadTags[0], opts.Head()
}
func (out *CalculationOutput) ApplyRefs(opts RefProvider) Tags {
tags := make(Tags, 0)
for _, tag := range out.BaseTags {
tags = append(tags, Tag{Name: tag, Ref: opts.Base()})
}
for _, tag := range out.HeadTags {
tags = append(tags, Tag{Name: tag, Ref: opts.Head()})
}
for _, tag := range out.RootTags {
tags = append(tags, Tag{Name: tag, Ref: opts.Root()})
}
for _, tag := range out.MergeTags {
tags = append(tags, Tag{Name: tag, Ref: opts.Merge()})
}
return tags
}
func (me *Calculation) CalculateNewTagsRaw(ctx context.Context) *CalculationOutput {
out := &CalculationOutput{
BaseTags: []string{},
HeadTags: []string{},
RootTags: []string{},
MergeTags: []string{},
}
if me.Skip {
zerolog.Ctx(ctx).Debug().Any("calculation", me).Msg("Skipping calculation")
return out
}
nvt := string(me.NextValidTag)
mmrt := string(me.MyMostRecentTag)
mrlt := string(me.MostRecentLiveTag)
// first we check to see if mrlt exists, if not we set it to the base
if mrlt == "" {
mrlt = baseTag
}
// mmrt and mrlt will always be the same on the first pr build
// matching := mmrt == mrlt && me.MyMostRecentBuild != 0
validMmrt := false
// first we validate that mmrt is still valid, which means it is greater than or equal to mrlt
if mmrt != "" && semver.Compare(mmrt, mrlt) > 0 {
validMmrt = true
}
if mmrt != "" && semver.Compare(mmrt, mrlt) == 0 && me.MyMostRecentBuild != 0 {
validMmrt = false
nvt = BumpPatch(mmrt)
} else if !me.IsMerged {
if me.MyMostRecentBuild == 0 {
validMmrt = false
} else if me.ForcePatch {
nvt = BumpPatch(mmrt)
validMmrt = false
}
}
// if mmrt is invalid, then we need to reserve a new mmrt (which is the same as nvt)
if !validMmrt {
mmrt = nvt
// pr will be 0 if this is not merged and is a push to the root branch
if me.PR != 0 && !me.IsMerged {
out.RootTags = append(out.RootTags, mmrt+"-reserved")
out.BaseTags = append(out.BaseTags, mmrt+fmt.Sprintf("-pr%d+base", me.PR))
}
}
if me.IsMerged {
// if !matching {
out.MergeTags = append(out.MergeTags, mmrt)
// }
} else {
if me.PR == 0 {
out.HeadTags = append(out.HeadTags, mmrt)
} else {
out.HeadTags = append(out.HeadTags, mmrt+fmt.Sprintf("-pr%d+%d", me.PR, int(me.MyMostRecentBuild)+1))
}
}
zerolog.Ctx(ctx).Debug().
Any("calculation", me).
Any("output", out).
Str("mmrt", mmrt).
Str("mrlt", mrlt).
Str("nvt", nvt).
Str("pr", fmt.Sprintf("%d", me.PR)).
Bool("isMerge", me.IsMerged).
Bool("forcePatch", me.ForcePatch).
Msg("CalculateNewTagsRaw")
return out
}