-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathlabels_test.go
174 lines (147 loc) · 4.48 KB
/
labels_test.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
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
package gitlab
import (
"testing"
"path"
"runtime"
"reflect"
gogitlab "github.com/xanzy/go-gitlab"
)
func TestLabels_UpdateWithRegex(t *testing.T) {
before(t)
proj := createProject(t, "temporary-update-labels-", "Temporary repository to update labels into")
defer deleteProject(t, proj)
addLabel(t, proj, "category#label", "#000000", "First label")
addLabel(t, proj, "misc#anotherlabel", "#ff0000", "Second label")
// update with regex
name := "(.+)#(.+)"
newName := "${1}/${2}"
if err := GitLabClient.Labels.UpdateWithRegex(proj.ID, &gogitlab.UpdateLabelOptions{
Name: &name,
NewName: &newName,
}); err != nil {
t.Fatal(err)
}
labelsExist(t, proj, []*gogitlab.Label{
&gogitlab.Label{"category/label", "#000000", "First label", 0, 0, 0},
&gogitlab.Label{"misc/anotherlabel", "#ff0000", "Second label", 0, 0, 0},
})
// update again without regex
name = "category/label"
newName = "category-label"
if err := GitLabClient.Labels.UpdateWithRegex(proj.ID, &gogitlab.UpdateLabelOptions{
Name: &name,
NewName: &newName,
}); err != nil {
t.Fatal(err)
}
labelsExist(t, proj, []*gogitlab.Label{
&gogitlab.Label{"category-label", "#000000", "First label", 0, 0, 0},
&gogitlab.Label{"misc/anotherlabel", "#ff0000", "Second label", 0, 0, 0},
})
// update color
name = "^misc"
col := "#ff7863"
if err := GitLabClient.Labels.UpdateWithRegex(proj.ID, &gogitlab.UpdateLabelOptions{
Name: &name,
Color: &col,
}); err != nil {
t.Fatal(err)
}
labelsExist(t, proj, []*gogitlab.Label{
&gogitlab.Label{"misc/anotherlabel", "#ff7863", "Second label", 0, 0, 0},
})
}
func TestLabels_DeleteWithRegex(t *testing.T) {
before(t)
proj := createProject(t, "temporary-delete-labels-from-", "Temporary repository to delete labels from")
defer deleteProject(t, proj)
addLabel(t, proj, "test-label", "#000000", "Test label description")
if err := GitLabClient.Labels.DeleteWithRegex(proj.ID, ""); err != nil {
t.Fatal(err)
}
if labels := getLabels(t, proj.ID); len(labels) > 0 {
t.Fatalf("labels still exist after supposedly deleting them all: %v", labels)
}
}
func TestLabels_CopyGlobalLabelsTo(t *testing.T) {
before(t)
proj := createProject(t, "temporary-copy-globals-to-", "Temporary repository to copy global labels to")
defer deleteProject(t, proj)
globalLabels := getLabels(t, proj.ID)
if err := GitLabClient.Labels.DeleteWithRegex(proj.ID, ""); err != nil {
t.Fatal(err)
}
if err := GitLabClient.Labels.CopyGlobalLabelsTo(proj.ID); err != nil {
t.Fatal(err)
}
labels := getLabels(t, proj.ID)
if len(labels) != len(globalLabels) {
t.Fatalf("different number of labels\nglobalLabels: %v\nrepoLabels: %v", globalLabels, labels)
}
for i, label := range labels {
global := globalLabels[i]
if label.Name != global.Name || label.Color != global.Color {
t.Fatalf("labels are different\nglobalLabels: %v\nrepoLabels: %v", globalLabels, labels)
}
}
}
// Helper functions:
func getLabels(tb testing.TB, pid interface{}) []*gogitlab.Label {
labels, _, err := GitLabClient.Labels.ListLabels(pid)
if err != nil {
// The failure happens at wherever we were called, not here
_, file, line, ok := runtime.Caller(1)
if !ok {
tb.Fatalf("Unable to get caller")
}
tb.Fatalf("%s:%v %v", path.Base(file), line, err)
}
return labels
}
func addLabel(tb testing.TB, proj *gogitlab.Project, name, color, description string) *gogitlab.Label {
l, _, err := GitLabClient.Labels.CreateLabel(proj.ID, &gogitlab.CreateLabelOptions{
Name: &name,
Color: &color,
Description: &description,
})
if err != nil {
// The failure happens at wherever we were called, not here
_, file, line, ok := runtime.Caller(1)
if !ok {
tb.Fatalf("Unable to get caller")
}
tb.Fatalf("%s:%v %v", path.Base(file), line, err)
}
return l
}
func labelsExist(tb testing.TB, proj *gogitlab.Project, expected []*gogitlab.Label) {
labels := getLabels(tb, proj.ID)
for _, exp := range expected {
found := false
for _, l := range labels {
e := *exp
if exp.Color == "" {
e.Color = l.Color
}
if exp.Description == "" {
e.Description = l.Description
}
if exp.OpenIssuesCount == 0 {
e.OpenIssuesCount = l.OpenIssuesCount
}
if exp.ClosedIssuesCount == 0 {
e.ClosedIssuesCount = l.ClosedIssuesCount
}
if exp.OpenMergeRequestsCount == 0 {
e.OpenMergeRequestsCount = l.OpenMergeRequestsCount
}
if reflect.DeepEqual(&e, l) {
found = true
break
}
}
if !found {
tb.Fatalf("label %v doesn't exist in %v", exp, labels)
}
}
}