Skip to content

Commit 886d085

Browse files
author
Stephane Leclercq
committed
feat(project) list/add/edit/rm project variables
1 parent 53eb83b commit 886d085

File tree

6 files changed

+285
-10
lines changed

6 files changed

+285
-10
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ go-gitlab-client is a simple client written in golang to consume gitlab API.
1111
*
1212
### Projects [gitlab api doc](http://doc.gitlab.com/ce/api/projects.html)
1313
* list projects
14-
* add/get/edit/rm single project
14+
* get single project
15+
* remove project
16+
* list project variables
17+
* add/get/edit/rm project variable
1518
*
1619
### Repositories [gitlab api doc](http://doc.gitlab.com/ce/api/repositories.html)
1720
* list repository branches

examples/projects/main.go

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ func main() {
3939
" > -m hooks -id PROJECT_ID\n"+
4040
" > -m branches -id PROJECT_ID\n"+
4141
" > -m team -id PROJECT_ID\n"+
42-
" > -m merge_requests -id PROJECT_ID [-state <all|merged|opened|closed>] [-order <created_at|updated_at>] [-sort <asc|desc>]")
42+
" > -m merge_requests -id PROJECT_ID [-state <all|merged|opened|closed>] [-order <created_at|updated_at>] [-sort <asc|desc>]\n"+
43+
" > -m variables -id PROJECT_ID [-o <add|get|edit|rm>] [-key VARIABLE_KEY] [-value VARIABLE_VALUE]")
4344

4445
var id string
4546
flag.StringVar(&id, "id", "", "Specify repository id")
@@ -56,6 +57,12 @@ func main() {
5657
var operation string
5758
flag.StringVar(&operation, "o", "", "Specify operation")
5859

60+
var key string
61+
flag.StringVar(&key, "key", "", "Specify key")
62+
63+
var value string
64+
flag.StringVar(&value, "value", "", "Specify value")
65+
5966
var desc string
6067
flag.StringVar(&desc, "desc", "", "Specify description")
6168

@@ -223,5 +230,99 @@ func main() {
223230
fmt.Printf("> [%d] %s (+%d) by %s on %s.\n", mr.Id, mr.Title, mr.Upvotes, mr.Author.Name, mr.CreatedAt)
224231
}
225232

233+
case "variables":
234+
235+
if id == "" {
236+
flag.Usage()
237+
return
238+
}
239+
240+
if operation == "" {
241+
fmt.Println("Fetching project variables...")
242+
243+
variables, err := gitlab.ProjectVariables(id)
244+
if err != nil {
245+
fmt.Println(err.Error())
246+
return
247+
}
248+
249+
for _, variable := range variables {
250+
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
251+
}
252+
return
253+
}
254+
255+
switch operation {
256+
case "get":
257+
fmt.Println("Fetching project variable...")
258+
if key == "" {
259+
flag.Usage()
260+
return
261+
}
262+
263+
variable, err := gitlab.ProjectVariable(id, key)
264+
if err != nil {
265+
fmt.Println(err.Error())
266+
return
267+
}
268+
269+
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
270+
271+
case "add":
272+
fmt.Println("Add project variable...")
273+
if key == "" || value == "" {
274+
flag.Usage()
275+
return
276+
}
277+
278+
req := gogitlab.Variable{
279+
Key: key,
280+
Value: value,
281+
}
282+
283+
variable, err := gitlab.AddProjectVariable(id, &req)
284+
if err != nil {
285+
fmt.Println(err.Error())
286+
return
287+
}
288+
289+
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
290+
291+
case "edit":
292+
fmt.Println("Edit project variable...")
293+
if key == "" || value == "" {
294+
flag.Usage()
295+
return
296+
}
297+
298+
req := gogitlab.Variable{
299+
Key: key,
300+
Value: value,
301+
}
302+
303+
variable, err := gitlab.UpdateProjectVariable(id, &req)
304+
if err != nil {
305+
fmt.Println(err.Error())
306+
return
307+
}
308+
309+
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
310+
311+
case "rm":
312+
fmt.Println("Delete project variable...")
313+
if key == "" {
314+
flag.Usage()
315+
return
316+
}
317+
318+
variable, err := gitlab.DeleteProjectVariable(id, key)
319+
if err != nil {
320+
fmt.Println(err.Error())
321+
return
322+
}
323+
324+
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
325+
326+
}
226327
}
227328
}

projects.go

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import (
66
)
77

88
const (
9-
projects_url = "/projects" // Get a list of projects owned by the authenticated user
10-
projects_all = "/projects/all" // Get a list of all GitLab projects (admin only)
11-
projects_search_url = "/projects/search/:query" // Search for projects by name
12-
project_url = "/projects/:id" // Get a specific project, identified by project ID or NAME
13-
project_url_events = "/projects/:id/events" // Get project events
14-
project_url_branches = "/projects/:id/repository/branches" // Lists all branches of a project
15-
project_url_members = "/projects/:id/members" // List project team members
16-
project_url_member = "/projects/:id/members/:user_id" // Get project team member
9+
projects_url = "/projects" // Get a list of projects owned by the authenticated user
10+
projects_all = "/projects/all" // Get a list of all GitLab projects (admin only)
11+
projects_search_url = "/projects/search/:query" // Search for projects by name
12+
project_url = "/projects/:id" // Get a specific project, identified by project ID or NAME
13+
project_url_events = "/projects/:id/events" // Get project events
14+
project_url_branches = "/projects/:id/repository/branches" // Lists all branches of a project
15+
project_url_members = "/projects/:id/members" // List project team members
16+
project_url_member = "/projects/:id/members/:user_id" // Get project team member
17+
project_url_variables = "/projects/:id/variables" // List project variables or add one
18+
project_url_variable = "/projects/:id/variables/:variable_key" // Get or Update project variable
1719
)
1820

1921
type Member struct {
@@ -36,6 +38,11 @@ type Namespace struct {
3638
Updated_At string
3739
}
3840

41+
type Variable struct {
42+
Key string `json:"key"`
43+
Value string `json:"value"`
44+
}
45+
3946
// A gitlab project
4047
type Project struct {
4148
Id int `json:"id,omitempty"`
@@ -180,3 +187,93 @@ func (g *Gitlab) ProjectMembers(id string) ([]*Member, error) {
180187

181188
return members, err
182189
}
190+
191+
/*
192+
Lists all variables of a project
193+
*/
194+
func (g *Gitlab) ProjectVariables(id string) ([]*Variable, error) {
195+
url, opaque := g.ResourceUrlRaw(project_url_variables, map[string]string{":id": id})
196+
197+
var variables []*Variable
198+
199+
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
200+
if err == nil {
201+
err = json.Unmarshal(contents, &variables)
202+
}
203+
204+
return variables, err
205+
}
206+
207+
/*
208+
Shows a project variable
209+
*/
210+
func (g *Gitlab) ProjectVariable(id string, key string) (*Variable, error) {
211+
url, opaque := g.ResourceUrlRaw(project_url_variable, map[string]string{":id": id, ":variable_key": key})
212+
213+
var result *Variable
214+
215+
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
216+
if err == nil {
217+
err = json.Unmarshal(contents, &result)
218+
}
219+
220+
return result, err
221+
}
222+
223+
/*
224+
Adds a project variable
225+
*/
226+
func (g *Gitlab) AddProjectVariable(id string, variable *Variable) (*Variable, error) {
227+
url, opaque := g.ResourceUrlRaw(project_url_variables, map[string]string{":id": id})
228+
229+
encodedRequest, err := json.Marshal(variable)
230+
if err != nil {
231+
return nil, err
232+
}
233+
234+
var result *Variable
235+
236+
contents, err := g.buildAndExecRequestRaw("POST", url, opaque, encodedRequest)
237+
if err == nil {
238+
err = json.Unmarshal(contents, &result)
239+
}
240+
241+
return result, err
242+
}
243+
244+
/*
245+
Updates a project variable
246+
*/
247+
func (g *Gitlab) UpdateProjectVariable(id string, variable *Variable) (*Variable, error) {
248+
url := g.ResourceUrl(project_url_variable, map[string]string{":id": id, ":variable_key": variable.Key})
249+
250+
encodedRequest, err := json.Marshal(variable)
251+
if err != nil {
252+
return nil, err
253+
}
254+
var result *Variable
255+
256+
contents, err := g.buildAndExecRequest("PUT", url, encodedRequest)
257+
258+
if err == nil {
259+
err = json.Unmarshal(contents, &result)
260+
}
261+
262+
return result, err
263+
}
264+
265+
/*
266+
Deletes a project variable
267+
*/
268+
func (g *Gitlab) DeleteProjectVariable(id string, key string) (*Variable, error) {
269+
url, opaque := g.ResourceUrlRaw(project_url_variable, map[string]string{":id": id, ":variable_key": key})
270+
271+
var result *Variable
272+
273+
contents, err := g.buildAndExecRequestRaw("DELETE", url, opaque, nil)
274+
if err == nil {
275+
err = json.Unmarshal(contents, &result)
276+
}
277+
278+
return result, err
279+
}

projects_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,63 @@ func TestRemoveProject(t *testing.T) {
5555
assert.Equal(t, err, nil)
5656
assert.Equal(t, result, true)
5757
}
58+
59+
func TestListVariables(t *testing.T) {
60+
ts, gitlab := Stub("stubs/projects/variables/index.json")
61+
62+
variables, err := gitlab.ProjectVariables("1")
63+
64+
assert.Equal(t, err, nil)
65+
assert.Equal(t, len(variables), 2)
66+
defer ts.Close()
67+
}
68+
69+
func TestGetVariable(t *testing.T) {
70+
ts, gitlab := Stub("stubs/projects/variables/show.json")
71+
72+
result, err := gitlab.ProjectVariable("1", "Somekey")
73+
74+
assert.NoError(t, err)
75+
assert.Equal(t, result.Key, "somekey")
76+
assert.Equal(t, result.Value, "somevalue")
77+
defer ts.Close()
78+
}
79+
80+
func TestAddVariable(t *testing.T) {
81+
ts, gitlab := Stub("stubs/projects/variables/show.json")
82+
req := Variable{
83+
Key: "somekey",
84+
Value: "somevalue",
85+
}
86+
result, err := gitlab.AddProjectVariable("1", &req)
87+
88+
assert.NoError(t, err)
89+
assert.Equal(t, result.Key, "somekey")
90+
assert.Equal(t, result.Value, "somevalue")
91+
defer ts.Close()
92+
}
93+
94+
func TestUpdateVariable(t *testing.T) {
95+
ts, gitlab := Stub("stubs/projects/variables/show.json")
96+
req := Variable{
97+
Key: "somekey",
98+
Value: "somevalue",
99+
}
100+
result, err := gitlab.UpdateProjectVariable("1", &req)
101+
102+
assert.NoError(t, err)
103+
assert.Equal(t, result.Key, "somekey")
104+
assert.Equal(t, result.Value, "somevalue")
105+
defer ts.Close()
106+
}
107+
108+
func TestDeleteVariable(t *testing.T) {
109+
ts, gitlab := Stub("stubs/projects/variables/show.json")
110+
111+
result, err := gitlab.DeleteProjectVariable("1", "somekey")
112+
113+
assert.NoError(t, err)
114+
assert.Equal(t, result.Key, "somekey")
115+
assert.Equal(t, result.Value, "somevalue")
116+
defer ts.Close()
117+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"key": "somekey",
4+
"value": "somevalue"
5+
},
6+
{
7+
"key": "someotherkey",
8+
"value": "someothervalue"
9+
}
10+
]

stubs/projects/variables/show.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"key": "somekey",
3+
"value": "somevalue"
4+
}

0 commit comments

Comments
 (0)