-
Notifications
You must be signed in to change notification settings - Fork 0
/
labels.go
80 lines (73 loc) · 1.73 KB
/
labels.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
package main
import (
"strings"
"golang.org/x/exp/maps"
)
const (
labelNone = "semver:none"
labelPatch = "semver:patch"
labelMinor = "semver:minor"
labelBreaking = "semver:breaking"
labelStable = "semver:stable"
labelPrerelease = "semver:prerelease"
)
var labelLevels = map[string]changeLevel{
labelBreaking: changeLevelMajor,
labelMinor: changeLevelMinor,
labelPatch: changeLevelPatch,
labelNone: changeLevelNone,
}
func normalizeAliases(aliases map[string]string) map[string]string {
clone := maps.Clone(aliases)
for k, v := range clone {
clone[strings.ToLower(k)] = v
}
return clone
}
// checkPrereleaseLabel returns true if the label is a prerelease label and the prerelease prefix (the part after the final colon)
func checkPrereleaseLabel(label string, aliases map[string]string) (pre bool, prefix string) {
downcased := strings.ToLower(label)
if downcased == labelPrerelease {
return true, ""
}
preLabel := ""
if strings.HasPrefix(downcased, labelPrerelease+":") {
preLabel = labelPrerelease + ":"
} else {
for alias, target := range aliases {
alias = strings.ToLower(alias)
if target != labelPrerelease {
continue
}
if downcased == alias {
return true, ""
}
if strings.HasPrefix(label, alias+":") {
preLabel = alias + ":"
break
}
}
}
if preLabel == "" {
return false, ""
}
return true, strings.TrimPrefix(label, preLabel)
}
func ResolveLabel(label string, aliases map[string]string) string {
label = strings.ToLower(label)
_, ok := labelLevels[label]
if ok {
return label
}
if label == labelStable {
return label
}
if aliases == nil {
return ""
}
v := normalizeAliases(aliases)[label]
if v == labelPrerelease {
v = ""
}
return v
}