-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_label_flags.go
65 lines (58 loc) · 2.01 KB
/
type_label_flags.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
package main
import (
"flag"
"strings"
"github.com/google/go-github/v43/github"
)
type TypeLabelFlags struct {
FeatureTypeLabels, ImprovementTypeLabels, BugTypeLabels []string
featureTypeLabelsString, improvementTypeLabelsString, bugTypeLabelsString string
}
func NewTypeLabelFlags(flags *flag.FlagSet) *TypeLabelFlags {
f := &TypeLabelFlags{}
// defaults dont work here because its too difficult to omit args in action based on input
flags.StringVar(&f.featureTypeLabelsString, "feature-type-labels", "", "A comma separated list of labels to consider as features")
flags.StringVar(&f.improvementTypeLabelsString, "improvement-type-labels", "", "A comma separated list of labels to consider as improvements")
flags.StringVar(&f.bugTypeLabelsString, "bug-type-labels", "", "A comma separated list of labels to consider as bugs")
return f
}
func (f *TypeLabelFlags) Parse() {
f.FeatureTypeLabels = parseLabelsStringFlag(f.featureTypeLabelsString)
f.ImprovementTypeLabels = parseLabelsStringFlag(f.improvementTypeLabelsString)
f.BugTypeLabels = parseLabelsStringFlag(f.bugTypeLabelsString)
}
func (f *TypeLabelFlags) GetNoteTypeFromLabels(prLabels []*github.Label) (noteType string) {
for _, lbl := range prLabels {
for _, label := range f.FeatureTypeLabels {
if strings.EqualFold(*lbl.Name, label) {
// type::improvement is a secondary label and co-exists with type::feature,
// so we don't break when we find type::feature label.
if label == "type::feature" {
noteType = "feature"
} else {
return "feature"
}
}
}
for _, label := range f.ImprovementTypeLabels {
if strings.EqualFold(*lbl.Name, label) {
return "improvement"
}
}
for _, label := range f.BugTypeLabels {
if strings.EqualFold(*lbl.Name, label) {
return "bug"
}
}
}
return
}
func parseLabelsStringFlag(str string) (labels []string) {
for _, label := range strings.Split(str, ",") {
clean := strings.TrimSpace(label)
if clean != "" {
labels = append(labels, clean)
}
}
return
}