forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 84
/
labels.go
56 lines (50 loc) · 1.38 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
package labels
import (
"fmt"
"sort"
"strings"
"github.com/jenkins-x/go-scm/scm"
)
const (
addLabel = "/jx-label "
removeLabel = " remove"
emptyString = ""
)
// ConvertLabelComments converts comments to labels for git providers which don't support native labels
func ConvertLabelComments(cs []*scm.Comment) ([]*scm.Label, error) {
sort.SliceStable(cs, func(i, j int) bool {
return cs[i].Created.UnixNano() < cs[j].Created.UnixNano()
})
m := make(map[string]bool)
for _, com := range cs {
if strings.HasPrefix(com.Body, addLabel) {
t := strings.ReplaceAll(com.Body, addLabel, emptyString)
if strings.HasSuffix(t, removeLabel) {
t = strings.ReplaceAll(t, removeLabel, emptyString)
m[t] = true
} else {
m[t] = false
}
}
}
var ls []*scm.Label
for l, i := range m {
if i == false {
ls = append(ls, &scm.Label{Name: l})
}
}
return ls, nil
}
// CreateLabelAddComment creates a label comment for git providers which don't support labels with comment with /jx-label <name>
func CreateLabelAddComment(label string) *scm.CommentInput {
return &scm.CommentInput{
Body: fmt.Sprintf("%s%s", addLabel, label),
}
}
// CreateLabelRemoveComment adds a comment with /jx-label <name> remove
func CreateLabelRemoveComment(label string) *scm.CommentInput {
input := &scm.CommentInput{
Body: fmt.Sprintf("%s%s%s", addLabel, label, removeLabel),
}
return input
}