Skip to content

Commit d04d143

Browse files
committed
Update go-gitlab
Fixes #4
1 parent 6fb7872 commit d04d143

File tree

10 files changed

+87
-70
lines changed

10 files changed

+87
-70
lines changed

cmd/label_copy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ In the later case it will use --url, without its path.`,
5050

5151
if from == nil {
5252
// we need to copy the global labels
53-
if err := to.Client.Labels.CopyGlobalLabelsTo(*to.Project.ID); err != nil {
53+
if err := to.Client.Labels.CopyGlobalLabelsTo(to.Project.ID); err != nil {
5454
fmt.Fprintf(os.Stderr, "error: '%s': %v\n",
55-
*to.Project.PathWithNamespace, err)
55+
to.Project.PathWithNamespace, err)
5656
os.Exit(1)
5757
}
5858
} else {
5959
// we need to copy labels from one project to another
60-
if err := to.Client.Labels.CopyLabels(*from.Project.ID, *to.Project.ID); err != nil {
60+
if err := to.Client.Labels.CopyLabels(from.Project.ID, to.Project.ID); err != nil {
6161
fmt.Fprintf(os.Stderr, "error: '%s' to '%s': %v\n",
62-
*from.Project.PathWithNamespace, *to.Project.PathWithNamespace, err)
62+
from.Project.PathWithNamespace, to.Project.PathWithNamespace, err)
6363
os.Exit(1)
6464
}
6565
}

cmd/label_delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ labels that match. If ommitted, all repository labels will be deleted.`,
2929
os.Exit(1)
3030
}
3131

32-
if err := to.Client.Labels.DeleteWithRegex(*to.Project.ID, regexpLabel); err != nil {
32+
if err := to.Client.Labels.DeleteWithRegex(to.Project.ID, regexpLabel); err != nil {
3333
fmt.Fprintf(os.Stderr, "error: %v\n", err.Error())
3434
os.Exit(1)
3535
}

cmd/label_update.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
var matchLabel string
1212
var replaceLabel string
1313
var colorLabel string
14+
var descriptionLabel string
1415

1516
var labelUpdateCmd = &cobra.Command{
1617
Use: "update",
@@ -31,10 +32,11 @@ name. At least one of --replace or --color is required to update the label(s).`,
3132
os.Exit(1)
3233
}
3334

34-
if err := to.Client.Labels.UpdateWithRegex(*to.Project.ID, &gogitlab.UpdateLabelOptions{
35-
Name: matchLabel,
36-
NewName: replaceLabel,
37-
Color: colorLabel,
35+
if err := to.Client.Labels.UpdateWithRegex(to.Project.ID, &gogitlab.UpdateLabelOptions{
36+
Name: &matchLabel,
37+
NewName: &replaceLabel,
38+
Color: &colorLabel,
39+
Description: &descriptionLabel,
3840
}); err != nil {
3941
fmt.Fprintf(os.Stderr, "error: %v\n", err.Error())
4042
os.Exit(1)
@@ -48,4 +50,5 @@ func init() {
4850
labelUpdateCmd.Flags().StringVar(&matchLabel, "match", "", "Label name to match, as a Go regex (https://golang.org/pkg/regexp/syntax)")
4951
labelUpdateCmd.Flags().StringVar(&replaceLabel, "replace", "", "Label name replacement (https://golang.org/pkg/regexp/#Regexp.FindAllString)")
5052
labelUpdateCmd.Flags().StringVar(&colorLabel, "color", "", "Label color (e.g. '#000000')")
53+
labelUpdateCmd.Flags().StringVar(&descriptionLabel, "description", "", "Label description")
5154
}

gitlab/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func NewClientForUser(uri *url.URL, user, pass string) (*Client, error) {
5656
// getTokenForUser returns the token for the given user.
5757
func (c *Client) getTokenForUser(user, pass string) (string, error) {
5858
sess, _, err := c.Client.Session.GetSession(&gogitlab.GetSessionOptions{
59-
Login: user,
60-
Password: pass,
59+
Login: &user,
60+
Password: &pass,
6161
})
6262
if err != nil {
6363
return "", err

gitlab/globals_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,10 @@ func before(tb testing.TB) {
4141
// creates a minimal gitlab project with a random string appended
4242
// to the given name.
4343
func createProject(tb testing.TB, name, desc string) *gogitlab.Project {
44+
n := name + RandomString(4)
4445
proj, _, err := GitLabClient.Projects.CreateProject(&gogitlab.CreateProjectOptions{
45-
Name: name + RandomString(4),
46-
Description: desc,
47-
IssuesEnabled: false,
48-
MergeRequestsEnabled: false,
49-
WikiEnabled: false,
50-
SnippetsEnabled: false,
51-
Public: false,
46+
Name: &n,
47+
Description: &desc,
5248
})
5349
if err != nil {
5450
// The failure happens at wherever we were called, not here
@@ -62,7 +58,7 @@ func createProject(tb testing.TB, name, desc string) *gogitlab.Project {
6258
}
6359

6460
func deleteProject(tb testing.TB, proj *gogitlab.Project) {
65-
if _, err := GitLabClient.Projects.DeleteProject(*proj.ID); err != nil {
61+
if _, err := GitLabClient.Projects.DeleteProject(proj.ID); err != nil {
6662
// The failure happens at wherever we were called, not here
6763
_, file, line, ok := runtime.Caller(1)
6864
if !ok {

gitlab/labels.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Labels struct {
2525
//
2626
// If at least one label fails to update, it will return an error.
2727
func (srv *Labels) UpdateWithRegex(pid interface{}, opts *gogitlab.UpdateLabelOptions) error {
28-
re, err := regexp.Compile(opts.Name)
28+
re, err := regexp.Compile(*opts.Name)
2929
if err != nil {
3030
return fmt.Errorf("'%s' is not a valid Go regexp: %v\n"+
3131
"See https://golang.org/pkg/regexp/syntax/", opts.Name, err)
@@ -38,12 +38,14 @@ func (srv *Labels) UpdateWithRegex(pid interface{}, opts *gogitlab.UpdateLabelOp
3838
var errs []string
3939
for _, label := range labels {
4040
if re.MatchString(label.Name) {
41-
opts.Name = label.Name
42-
if repl != "" {
43-
opts.NewName = re.ReplaceAllString(label.Name, repl)
41+
opts.Name = &label.Name
42+
var newName string
43+
if repl != nil && *repl != "" {
44+
newName = re.ReplaceAllString(label.Name, *repl)
4445
} else {
45-
opts.NewName = ""
46+
newName = ""
4647
}
48+
opts.NewName = &newName
4749
if _, _, err := srv.UpdateLabel(pid, opts); err != nil {
4850
errs = append(errs, fmt.Sprintf("'%s' failed to update: %v", label.Name, err))
4951
}
@@ -69,7 +71,7 @@ func (srv *Labels) DeleteWithRegex(pid interface{}, pattern string) error {
6971
}
7072
for _, label := range labels {
7173
if pattern == "" || re.MatchString(label.Name) {
72-
_, err := srv.DeleteLabel(pid, &gogitlab.DeleteLabelOptions{Name: label.Name})
74+
_, err := srv.DeleteLabel(pid, &gogitlab.DeleteLabelOptions{Name: &label.Name})
7375
if err != nil {
7476
return err
7577
}
@@ -85,25 +87,22 @@ func (srv *Labels) DeleteWithRegex(pid interface{}, pattern string) error {
8587
//
8688
// If at least one label fails to copy, it will return an error.
8789
func (srv *Labels) CopyGlobalLabelsTo(pid interface{}) error {
90+
name := "temporary-copy-globals-from-" + RandomString(4)
91+
desc := "Temporary repository to copy global labels from"
8892
proj, _, err := srv.client.Projects.CreateProject(&gogitlab.CreateProjectOptions{
89-
Name: "temporary-copy-globals-from-" + RandomString(4),
90-
Description: "Temporary repository to copy global labels from",
91-
IssuesEnabled: false,
92-
MergeRequestsEnabled: false,
93-
WikiEnabled: false,
94-
SnippetsEnabled: false,
95-
Public: false,
93+
Name: &name,
94+
Description: &desc,
9695
})
9796
if err != nil {
9897
return err
9998
}
10099
defer func() {
101-
if _, err := srv.client.Projects.DeleteProject(*proj.ID); err != nil {
100+
if _, err := srv.client.Projects.DeleteProject(proj.ID); err != nil {
102101
fmt.Fprintln(os.Stderr, err)
103102
}
104103
}()
105104

106-
return srv.CopyLabels(*proj.ID, pid)
105+
return srv.CopyLabels(proj.ID, pid)
107106
}
108107

109108
// CopyLabels copies the labels from a project into another one,
@@ -118,8 +117,8 @@ func (srv *Labels) CopyLabels(from, to interface{}) error {
118117
var errs []string
119118
for _, label := range labels {
120119
if _, _, err := srv.CreateLabel(to, &gogitlab.CreateLabelOptions{
121-
Name: label.Name,
122-
Color: label.Color,
120+
Name: &label.Name,
121+
Color: &label.Color,
123122
}); err != nil {
124123
errs = append(errs, fmt.Sprintf("'%s' failed to create: %v", label.Name, err))
125124
}

gitlab/labels_test.go

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,51 @@ func TestLabels_UpdateWithRegex(t *testing.T) {
1717
proj := createProject(t, "temporary-update-labels-", "Temporary repository to update labels into")
1818
defer deleteProject(t, proj)
1919

20-
addLabel(t, proj, "category#label", "#000000")
21-
addLabel(t, proj, "misc#anotherlabel", "#ff0000")
20+
addLabel(t, proj, "category#label", "#000000", "First label")
21+
addLabel(t, proj, "misc#anotherlabel", "#ff0000", "Second label")
2222

2323
// update with regex
24-
if err := GitLabClient.Labels.UpdateWithRegex(*proj.ID, &gogitlab.UpdateLabelOptions{
25-
Name: "(.+)#(.+)",
26-
NewName: "${1}/${2}",
24+
name := "(.+)#(.+)"
25+
newName := "${1}/${2}"
26+
if err := GitLabClient.Labels.UpdateWithRegex(proj.ID, &gogitlab.UpdateLabelOptions{
27+
Name: &name,
28+
NewName: &newName,
2729
}); err != nil {
2830
t.Fatal(err)
2931
}
3032

3133
labelsExist(t, proj, []*gogitlab.Label{
32-
&gogitlab.Label{"category/label", ""},
33-
&gogitlab.Label{"misc/anotherlabel", ""},
34+
&gogitlab.Label{"category/label", "#000000", "First label", 0, 0, 0},
35+
&gogitlab.Label{"misc/anotherlabel", "#ff0000", "Second label", 0, 0, 0},
3436
})
3537

3638
// update again without regex
37-
if err := GitLabClient.Labels.UpdateWithRegex(*proj.ID, &gogitlab.UpdateLabelOptions{
38-
Name: "category/label",
39-
NewName: "category-label",
39+
name = "category/label"
40+
newName = "category-label"
41+
if err := GitLabClient.Labels.UpdateWithRegex(proj.ID, &gogitlab.UpdateLabelOptions{
42+
Name: &name,
43+
NewName: &newName,
4044
}); err != nil {
4145
t.Fatal(err)
4246
}
4347

4448
labelsExist(t, proj, []*gogitlab.Label{
45-
&gogitlab.Label{"category-label", ""},
46-
&gogitlab.Label{"misc/anotherlabel", ""},
49+
&gogitlab.Label{"category-label", "#000000", "First label", 0, 0, 0},
50+
&gogitlab.Label{"misc/anotherlabel", "#ff0000", "Second label", 0, 0, 0},
4751
})
4852

4953
// update color
50-
if err := GitLabClient.Labels.UpdateWithRegex(*proj.ID, &gogitlab.UpdateLabelOptions{
51-
Name: "^misc",
52-
Color: "#ff7863",
54+
name = "^misc"
55+
col := "#ff7863"
56+
if err := GitLabClient.Labels.UpdateWithRegex(proj.ID, &gogitlab.UpdateLabelOptions{
57+
Name: &name,
58+
Color: &col,
5359
}); err != nil {
5460
t.Fatal(err)
5561
}
5662

5763
labelsExist(t, proj, []*gogitlab.Label{
58-
&gogitlab.Label{"misc/anotherlabel", "#ff7863"},
64+
&gogitlab.Label{"misc/anotherlabel", "#ff7863", "Second label", 0, 0, 0},
5965
})
6066
}
6167

@@ -65,12 +71,12 @@ func TestLabels_DeleteWithRegex(t *testing.T) {
6571
proj := createProject(t, "temporary-delete-labels-from-", "Temporary repository to delete labels from")
6672
defer deleteProject(t, proj)
6773

68-
addLabel(t, proj, "test-label", "#000000")
74+
addLabel(t, proj, "test-label", "#000000", "Test label description")
6975

70-
if err := GitLabClient.Labels.DeleteWithRegex(*proj.ID, ""); err != nil {
76+
if err := GitLabClient.Labels.DeleteWithRegex(proj.ID, ""); err != nil {
7177
t.Fatal(err)
7278
}
73-
if labels := getLabels(t, *proj.ID); len(labels) > 0 {
79+
if labels := getLabels(t, proj.ID); len(labels) > 0 {
7480
t.Fatalf("labels still exist after supposedly deleting them all: %v", labels)
7581
}
7682
}
@@ -81,17 +87,17 @@ func TestLabels_CopyGlobalLabelsTo(t *testing.T) {
8187
proj := createProject(t, "temporary-copy-globals-to-", "Temporary repository to copy global labels to")
8288
defer deleteProject(t, proj)
8389

84-
globalLabels := getLabels(t, *proj.ID)
90+
globalLabels := getLabels(t, proj.ID)
8591

86-
if err := GitLabClient.Labels.DeleteWithRegex(*proj.ID, ""); err != nil {
92+
if err := GitLabClient.Labels.DeleteWithRegex(proj.ID, ""); err != nil {
8793
t.Fatal(err)
8894
}
8995

90-
if err := GitLabClient.Labels.CopyGlobalLabelsTo(*proj.ID); err != nil {
96+
if err := GitLabClient.Labels.CopyGlobalLabelsTo(proj.ID); err != nil {
9197
t.Fatal(err)
9298
}
9399

94-
labels := getLabels(t, *proj.ID)
100+
labels := getLabels(t, proj.ID)
95101
if len(labels) != len(globalLabels) {
96102
t.Fatalf("different number of labels\nglobalLabels: %v\nrepoLabels: %v", globalLabels, labels)
97103
}
@@ -118,10 +124,11 @@ func getLabels(tb testing.TB, pid interface{}) []*gogitlab.Label {
118124
return labels
119125
}
120126

121-
func addLabel(tb testing.TB, proj *gogitlab.Project, name, color string) *gogitlab.Label {
122-
l, _, err := GitLabClient.Labels.CreateLabel(*proj.ID, &gogitlab.CreateLabelOptions{
123-
Name: name,
124-
Color: color,
127+
func addLabel(tb testing.TB, proj *gogitlab.Project, name, color, description string) *gogitlab.Label {
128+
l, _, err := GitLabClient.Labels.CreateLabel(proj.ID, &gogitlab.CreateLabelOptions{
129+
Name: &name,
130+
Color: &color,
131+
Description: &description,
125132
})
126133
if err != nil {
127134
// The failure happens at wherever we were called, not here
@@ -135,14 +142,26 @@ func addLabel(tb testing.TB, proj *gogitlab.Project, name, color string) *gogitl
135142
}
136143

137144
func labelsExist(tb testing.TB, proj *gogitlab.Project, expected []*gogitlab.Label) {
138-
labels := getLabels(tb, *proj.ID)
145+
labels := getLabels(tb, proj.ID)
139146
for _, exp := range expected {
140147
found := false
141148
for _, l := range labels {
142149
e := *exp
143150
if exp.Color == "" {
144151
e.Color = l.Color
145152
}
153+
if exp.Description == "" {
154+
e.Description = l.Description
155+
}
156+
if exp.OpenIssuesCount == 0 {
157+
e.OpenIssuesCount = l.OpenIssuesCount
158+
}
159+
if exp.ClosedIssuesCount == 0 {
160+
e.ClosedIssuesCount = l.ClosedIssuesCount
161+
}
162+
if exp.OpenMergeRequestsCount == 0 {
163+
e.OpenMergeRequestsCount = l.OpenMergeRequestsCount
164+
}
146165
if reflect.DeepEqual(&e, l) {
147166
found = true
148167
break

gitlab/projects.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (srv *Projects) ByPath(path string) (proj *gogitlab.Project, err error) {
2929
return nil, fmt.Errorf("incorrect path: %v", path)
3030
}
3131
findProject := func(p *gogitlab.Project) bool {
32-
if *p.PathWithNamespace == path {
32+
if p.PathWithNamespace == path {
3333
proj = p
3434
return true
3535
}

gitlab/projects_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ func TestProjects_ByPath(t *testing.T) {
1212
path string
1313
}
1414
tests := []*_test{
15-
&_test{*proj.PathWithNamespace},
15+
&_test{proj.PathWithNamespace},
1616
}
1717
for _, test := range tests {
1818
proj, err := GitLabClient.Projects.ByPath(test.path)
1919
if err != nil {
2020
t.Fatal(err)
2121
}
22-
if *proj.PathWithNamespace != test.path {
23-
t.Errorf("expecting '%s', got '%s'\n", test.path, *proj.PathWithNamespace)
22+
if proj.PathWithNamespace != test.path {
23+
t.Errorf("expecting '%s', got '%s'\n", test.path, proj.PathWithNamespace)
2424
}
2525
}
2626
tests = []*_test{
@@ -29,7 +29,7 @@ func TestProjects_ByPath(t *testing.T) {
2929
for _, test := range tests {
3030
proj, err := GitLabClient.Projects.ByPath(test.path)
3131
if _, ok := err.(*NotFound); !ok {
32-
t.Fatalf("expecting not found, got: %s", *proj.PathWithNamespace)
32+
t.Fatalf("expecting not found, got: %s", proj.PathWithNamespace)
3333
}
3434
}
3535
}

0 commit comments

Comments
 (0)