Skip to content

Commit 1c3341b

Browse files
author
Stephane Leclercq
committed
feat(project) list/add/edit/rm project variables
1 parent 2ed6f06 commit 1c3341b

File tree

6 files changed

+286
-10
lines changed

6 files changed

+286
-10
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ go-gitlab-client is a simple client written in golang to consume gitlab API.
1313
* list projects
1414
* get single project
1515
* remove project
16-
16+
* list project variables
17+
* add/get/edit/rm project variable
1718
*
1819
### Repositories [gitlab api doc](http://doc.gitlab.com/ce/api/repositories.html)
1920
* list repository branches

examples/projects/main.go

Lines changed: 105 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")
@@ -53,6 +54,15 @@ func main() {
5354
var sort string
5455
flag.StringVar(&sort, "sort", "", "Specify merge request sort")
5556

57+
var operation string
58+
flag.StringVar(&operation, "o", "", "Specify operation")
59+
60+
var key string
61+
flag.StringVar(&key, "key", "", "Specify key")
62+
63+
var value string
64+
flag.StringVar(&value, "value", "", "Specify value")
65+
5666
flag.Usage = func() {
5767
fmt.Printf("Usage:\n")
5868
flag.PrintDefaults()
@@ -198,5 +208,99 @@ func main() {
198208
fmt.Printf("> [%d] %s (+%d) by %s on %s.\n", mr.Id, mr.Title, mr.Upvotes, mr.Author.Name, mr.CreatedAt)
199209
}
200210

211+
case "variables":
212+
213+
if id == "" {
214+
flag.Usage()
215+
return
216+
}
217+
218+
if operation == "" {
219+
fmt.Println("Fetching project variables...")
220+
221+
variables, err := gitlab.ProjectVariables(id)
222+
if err != nil {
223+
fmt.Println(err.Error())
224+
return
225+
}
226+
227+
for _, variable := range variables {
228+
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
229+
}
230+
return
231+
}
232+
233+
switch operation {
234+
case "get":
235+
fmt.Println("Fetching project variable...")
236+
if key == "" {
237+
flag.Usage()
238+
return
239+
}
240+
241+
variable, err := gitlab.ProjectVariable(id, key)
242+
if err != nil {
243+
fmt.Println(err.Error())
244+
return
245+
}
246+
247+
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
248+
249+
case "add":
250+
fmt.Println("Add project variable...")
251+
if key == "" || value == "" {
252+
flag.Usage()
253+
return
254+
}
255+
256+
req := gogitlab.Variable{
257+
Key: key,
258+
Value: value,
259+
}
260+
261+
variable, err := gitlab.AddProjectVariable(id, &req)
262+
if err != nil {
263+
fmt.Println(err.Error())
264+
return
265+
}
266+
267+
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
268+
269+
case "edit":
270+
fmt.Println("Edit project variable...")
271+
if key == "" || value == "" {
272+
flag.Usage()
273+
return
274+
}
275+
276+
req := gogitlab.Variable{
277+
Key: key,
278+
Value: value,
279+
}
280+
281+
variable, err := gitlab.UpdateProjectVariable(id, &req)
282+
if err != nil {
283+
fmt.Println(err.Error())
284+
return
285+
}
286+
287+
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
288+
289+
case "rm":
290+
fmt.Println("Delete project variable...")
291+
if key == "" {
292+
flag.Usage()
293+
return
294+
}
295+
296+
variable, err := gitlab.DeleteProjectVariable(id, key)
297+
if err != nil {
298+
fmt.Println(err.Error())
299+
return
300+
}
301+
302+
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
303+
304+
}
201305
}
202306
}

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"`
@@ -152,3 +159,93 @@ func (g *Gitlab) ProjectMembers(id string) ([]*Member, error) {
152159

153160
return members, err
154161
}
162+
163+
/*
164+
Lists all variables of a project
165+
*/
166+
func (g *Gitlab) ProjectVariables(id string) ([]*Variable, error) {
167+
url, opaque := g.ResourceUrlRaw(project_url_variables, map[string]string{":id": id})
168+
169+
var variables []*Variable
170+
171+
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
172+
if err == nil {
173+
err = json.Unmarshal(contents, &variables)
174+
}
175+
176+
return variables, err
177+
}
178+
179+
/*
180+
Shows a project variable
181+
*/
182+
func (g *Gitlab) ProjectVariable(id string, key string) (*Variable, error) {
183+
url, opaque := g.ResourceUrlRaw(project_url_variable, map[string]string{":id": id, ":variable_key": key})
184+
185+
var result *Variable
186+
187+
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
188+
if err == nil {
189+
err = json.Unmarshal(contents, &result)
190+
}
191+
192+
return result, err
193+
}
194+
195+
/*
196+
Adds a project variable
197+
*/
198+
func (g *Gitlab) AddProjectVariable(id string, variable *Variable) (*Variable, error) {
199+
url, opaque := g.ResourceUrlRaw(project_url_variables, map[string]string{":id": id})
200+
201+
encodedRequest, err := json.Marshal(variable)
202+
if err != nil {
203+
return nil, err
204+
}
205+
206+
var result *Variable
207+
208+
contents, err := g.buildAndExecRequestRaw("POST", url, opaque, encodedRequest)
209+
if err == nil {
210+
err = json.Unmarshal(contents, &result)
211+
}
212+
213+
return result, err
214+
}
215+
216+
/*
217+
Updates a project variable
218+
*/
219+
func (g *Gitlab) UpdateProjectVariable(id string, variable *Variable) (*Variable, error) {
220+
url := g.ResourceUrl(project_url_variable, map[string]string{":id": id, ":variable_key": variable.Key})
221+
222+
encodedRequest, err := json.Marshal(variable)
223+
if err != nil {
224+
return nil, err
225+
}
226+
var result *Variable
227+
228+
contents, err := g.buildAndExecRequest("PUT", url, encodedRequest)
229+
230+
if err == nil {
231+
err = json.Unmarshal(contents, &result)
232+
}
233+
234+
return result, err
235+
}
236+
237+
/*
238+
Deletes a project variable
239+
*/
240+
func (g *Gitlab) DeleteProjectVariable(id string, key string) (*Variable, error) {
241+
url, opaque := g.ResourceUrlRaw(project_url_variable, map[string]string{":id": id, ":variable_key": key})
242+
243+
var result *Variable
244+
245+
contents, err := g.buildAndExecRequestRaw("DELETE", url, opaque, nil)
246+
if err == nil {
247+
err = json.Unmarshal(contents, &result)
248+
}
249+
250+
return result, err
251+
}

projects_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,63 @@ func TestRemoveProject(t *testing.T) {
4444
assert.Equal(t, err, nil)
4545
assert.Equal(t, result, true)
4646
}
47+
48+
func TestListVariables(t *testing.T) {
49+
ts, gitlab := Stub("stubs/projects/variables/index.json")
50+
51+
variables, err := gitlab.ProjectVariables("1")
52+
53+
assert.Equal(t, err, nil)
54+
assert.Equal(t, len(variables), 2)
55+
defer ts.Close()
56+
}
57+
58+
func TestGetVariable(t *testing.T) {
59+
ts, gitlab := Stub("stubs/projects/variables/show.json")
60+
61+
result, err := gitlab.ProjectVariable("1", "Somekey")
62+
63+
assert.NoError(t, err)
64+
assert.Equal(t, result.Key, "somekey")
65+
assert.Equal(t, result.Value, "somevalue")
66+
defer ts.Close()
67+
}
68+
69+
func TestAddVariable(t *testing.T) {
70+
ts, gitlab := Stub("stubs/projects/variables/show.json")
71+
req := Variable{
72+
Key: "somekey",
73+
Value: "somevalue",
74+
}
75+
result, err := gitlab.AddProjectVariable("1", &req)
76+
77+
assert.NoError(t, err)
78+
assert.Equal(t, result.Key, "somekey")
79+
assert.Equal(t, result.Value, "somevalue")
80+
defer ts.Close()
81+
}
82+
83+
func TestUpdateVariable(t *testing.T) {
84+
ts, gitlab := Stub("stubs/projects/variables/show.json")
85+
req := Variable{
86+
Key: "somekey",
87+
Value: "somevalue",
88+
}
89+
result, err := gitlab.UpdateProjectVariable("1", &req)
90+
91+
assert.NoError(t, err)
92+
assert.Equal(t, result.Key, "somekey")
93+
assert.Equal(t, result.Value, "somevalue")
94+
defer ts.Close()
95+
}
96+
97+
func TestDeleteVariable(t *testing.T) {
98+
ts, gitlab := Stub("stubs/projects/variables/show.json")
99+
100+
result, err := gitlab.DeleteProjectVariable("1", "somekey")
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+
}
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)