Skip to content

Commit 2a72824

Browse files
committed
Add support for label operations on projects
This change implements the label operations sub-API for projects that was introduced around Gerrit 3.1.x.
1 parent 7bbec04 commit 2a72824

File tree

2 files changed

+154
-2
lines changed

2 files changed

+154
-2
lines changed

projects.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ type ProjectsService struct {
1717
//
1818
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#label-type-info
1919
type LabelTypeInfo struct {
20-
Values map[string]string `json:"values",omitempty`
21-
DefaultValue int `json:"default_value",omitempty`
20+
Values map[string]string `json:"values,omitempty"`
21+
DefaultValue int `json:"default_value,omitempty"`
2222
}
2323

2424
// ProjectInfo entity contains information about a project.

projects_label.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package gerrit
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
)
8+
9+
type LabelDefinitionInfo struct {
10+
Name string `json:"name,omitempty"`
11+
Description string `json:"description,omitempty"`
12+
ProjectName string `json:"project_name,omitempty"`
13+
Function string `json:"function,omitempty"`
14+
Values map[string]string `json:"values,omitempty"`
15+
DefaultValue int `json:"default_value,omitempty"`
16+
Branches []string `json:"branches,omitempty"`
17+
CanOverride bool `json:"can_override,omitempty"`
18+
CopyCondition string `json:"copy_condition,omitempty"`
19+
AllowPostSubmit bool `json:"allow_post_submit,omitempty"`
20+
IgnoreSelfApproval bool `json:"ignore_self_approval,omitempty"`
21+
}
22+
23+
type LabelDefinitionInput struct {
24+
CommitMessage string `json:"commit_message,omitempty"`
25+
Name string `json:"name,omitempty"`
26+
Description string `json:"descriptioan,omitempty"`
27+
Function string `json:"function,omitempty"`
28+
Values map[string]string `json:"values,omitempty"`
29+
DefaultValue int `json:"default_value,omitempty"`
30+
Branches []string `json:"branches,omitempty"`
31+
CanOverride bool `json:"can_override,omitempty"`
32+
CopyCondition string `json:"copy_condition,omitempty"`
33+
UnsetCopyCondition bool `json:"unset_copy_condition,omitempty"`
34+
AllowPostSubmit bool `json:"allow_post_submit,omitempty"`
35+
IgnoreSelfApproval bool `json:"ignore_self_approval,omitempty"`
36+
}
37+
38+
type DeleteLabelInput struct {
39+
CommitMessage string `json:"commit_message,omitempty"`
40+
}
41+
42+
type BatchLabelInput struct {
43+
CommitMessage string `json:"commit_message,omitempty"`
44+
Delete []string `json:"delete,omitempty"`
45+
Create []LabelDefinitionInput `json:"create,omitempty"`
46+
Update []LabelDefinitionInput `json:"update,omitempty"`
47+
}
48+
49+
// ListLabels lists the labels for a project
50+
//
51+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#list-labels
52+
func (s *ProjectsService) ListLabels(ctx context.Context, projectName string) (*[]LabelDefinitionInfo, *Response, error) {
53+
u := fmt.Sprintf("projects/%s/labels/", url.QueryEscape(projectName))
54+
55+
req, err := s.client.NewRequest(ctx, "GET", u, nil)
56+
if err != nil {
57+
return nil, nil, err
58+
}
59+
60+
v := new([]LabelDefinitionInfo)
61+
resp, err := s.client.Do(req, v)
62+
if err != nil {
63+
return nil, resp, err
64+
}
65+
66+
return v, resp, err
67+
}
68+
69+
// GetLabel gets the definition of a label associated with a project
70+
//
71+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-label
72+
func (s *ProjectsService) GetLabel(ctx context.Context, projectName, labelName string) (*LabelDefinitionInfo, *Response, error) {
73+
u := fmt.Sprintf("projects/%s/labels/%s", url.QueryEscape(projectName), url.QueryEscape(labelName))
74+
75+
req, err := s.client.NewRequest(ctx, "GET", u, nil)
76+
if err != nil {
77+
return nil, nil, err
78+
}
79+
80+
v := new(LabelDefinitionInfo)
81+
resp, err := s.client.Do(req, v)
82+
if err != nil {
83+
return nil, resp, err
84+
}
85+
86+
return v, resp, err
87+
}
88+
89+
// CreateLabel creates a label type for an associated project
90+
//
91+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#create-label
92+
func (s *ProjectsService) CreateLabel(ctx context.Context, projectName string, input *LabelDefinitionInput) (*LabelDefinitionInfo, *Response, error) {
93+
// this uses SetLabel internally because it appears to use the same method
94+
// on the same URL as SetLabel
95+
return s.SetLabel(ctx, projectName, input)
96+
}
97+
98+
// SetLabel sets the definition of a label for an associated project
99+
//
100+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#set-label
101+
func (s *ProjectsService) SetLabel(ctx context.Context, projectName string, input *LabelDefinitionInput) (*LabelDefinitionInfo, *Response, error) {
102+
u := fmt.Sprintf("projects/%s/labels/%s", url.QueryEscape(projectName), url.QueryEscape(input.Name))
103+
104+
req, err := s.client.NewRequest(ctx, "PUT", u, input)
105+
if err != nil {
106+
return nil, nil, err
107+
}
108+
109+
v := new(LabelDefinitionInfo)
110+
resp, err := s.client.Do(req, v)
111+
if err != nil {
112+
return nil, resp, err
113+
}
114+
115+
return v, resp, err
116+
}
117+
118+
// DeleteLabel deletes a label definition for an associated project
119+
//
120+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#delete-label
121+
func (s *ProjectsService) DeleteLabel(ctx context.Context, projectName, labelName string, input *DeleteLabelInput) (*Response, error) {
122+
u := fmt.Sprintf("projects/%s/labels/%s", url.QueryEscape(projectName), url.QueryEscape(labelName))
123+
return s.client.DeleteRequest(ctx, u, input)
124+
}
125+
126+
// Batch update labels
127+
//
128+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#batch-update-labels
129+
func (s *ProjectsService) BatchUpdateLabels(ctx context.Context, projectName string, input *BatchLabelInput) (*Response, error) {
130+
u := fmt.Sprintf("projects/%s/labels/", url.QueryEscape(projectName))
131+
132+
req, err := s.client.NewRequest(ctx, "POST", u, input)
133+
if err != nil {
134+
return nil, err
135+
}
136+
137+
return s.client.Do(req, nil)
138+
}
139+
140+
// Create labels change for review
141+
//
142+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#create-labels-change
143+
func (s *ProjectsService) CreateLabelsChange(ctx context.Context, projectName string, input *BatchLabelInput) (*Response, error) {
144+
u := fmt.Sprintf("projects/%s/labels:review", url.QueryEscape(projectName))
145+
146+
req, err := s.client.NewRequest(ctx, "POST", u, input)
147+
if err != nil {
148+
return nil, err
149+
}
150+
151+
return s.client.Do(req, nil)
152+
}

0 commit comments

Comments
 (0)