Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
go-gitlab
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
go-gitlab
Commits
2aec56c8
Commit
2aec56c8
authored
Dec 28, 2016
by
David
Committed by
Sander van Harmelen
Dec 28, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add support for project variables (#112)
parent
a1c611d1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
247 additions
and
0 deletions
+247
-0
gitlab.go
gitlab.go
+2
-0
variables.go
variables.go
+147
-0
variables_test.go
variables_test.go
+98
-0
No files found.
gitlab.go
View file @
2aec56c8
...
@@ -136,6 +136,7 @@ type Client struct {
...
@@ -136,6 +136,7 @@ type Client struct {
SystemHooks
*
SystemHooksService
SystemHooks
*
SystemHooksService
Tags
*
TagsService
Tags
*
TagsService
Users
*
UsersService
Users
*
UsersService
Variables
*
VariablesService
}
}
// ListOptions specifies the optional parameters to various List methods that
// ListOptions specifies the optional parameters to various List methods that
...
@@ -194,6 +195,7 @@ func newClient(httpClient *http.Client, tokenType tokenType, token string) *Clie
...
@@ -194,6 +195,7 @@ func newClient(httpClient *http.Client, tokenType tokenType, token string) *Clie
c
.
SystemHooks
=
&
SystemHooksService
{
client
:
c
}
c
.
SystemHooks
=
&
SystemHooksService
{
client
:
c
}
c
.
Tags
=
&
TagsService
{
client
:
c
}
c
.
Tags
=
&
TagsService
{
client
:
c
}
c
.
Users
=
&
UsersService
{
client
:
c
}
c
.
Users
=
&
UsersService
{
client
:
c
}
c
.
Variables
=
&
VariablesService
{
client
:
c
}
return
c
return
c
}
}
...
...
variables.go
0 → 100644
View file @
2aec56c8
package
gitlab
import
(
"fmt"
"net/url"
)
// VariablesService handles communication with the project variables related methods
// of the Gitlab API
//
// Gitlab API Docs : https://docs.gitlab.com/ce/api/build_variables.html
type
VariablesService
struct
{
client
*
Client
}
//Variable represents a variable available for each build of the given project
//
// Gitlab API Docs : https://docs.gitlab.com/ce/api/build_variables.html
type
Variable
struct
{
Key
string
`json:"key"`
Value
string
`json:"value"`
}
// ListVariables gets the a list of project variables in a project
//
// Gitlab API Docs:
// https://docs.gitlab.com/ce/api/build_variables.html#list-project-variables
func
(
s
*
VariablesService
)
ListVariables
(
pid
interface
{})
([]
*
Variable
,
*
Response
,
error
)
{
project
,
err
:=
parseID
(
pid
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
u
:=
fmt
.
Sprintf
(
"projects/%s/variables"
,
url
.
QueryEscape
(
project
))
req
,
err
:=
s
.
client
.
NewRequest
(
"GET"
,
u
,
nil
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
var
variables
[]
*
Variable
resp
,
err
:=
s
.
client
.
Do
(
req
,
&
variables
)
if
err
!=
nil
{
return
nil
,
resp
,
err
}
return
variables
,
resp
,
err
}
// GetSingleVariable gets a single project variable of a project
//
// Gitlab API Docs:
// https://docs.gitlab.com/ce/api/build_variables.html#show-variable-details
func
(
s
*
VariablesService
)
GetSingleVariable
(
pid
interface
{},
variableKey
string
)
(
*
Variable
,
*
Response
,
error
)
{
project
,
err
:=
parseID
(
pid
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
u
:=
fmt
.
Sprintf
(
"projects/%s/variables/%s"
,
url
.
QueryEscape
(
project
),
variableKey
)
req
,
err
:=
s
.
client
.
NewRequest
(
"GET"
,
u
,
nil
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
variable
:=
new
(
Variable
)
resp
,
err
:=
s
.
client
.
Do
(
req
,
variable
)
if
err
!=
nil
{
return
nil
,
resp
,
err
}
return
variable
,
resp
,
err
}
// CreateVariable creates a variable for a given project
//
// Gitlab API Docs:
// https://docs.gitlab.com/ce/api/build_variables.html#create-variable
func
(
s
*
VariablesService
)
CreateVariable
(
pid
interface
{},
variable
Variable
)
(
*
Variable
,
*
Response
,
error
)
{
project
,
err
:=
parseID
(
pid
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
u
:=
fmt
.
Sprintf
(
"projects/%s/variables"
,
url
.
QueryEscape
(
project
))
req
,
err
:=
s
.
client
.
NewRequest
(
"POST"
,
u
,
variable
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
createdVariable
:=
new
(
Variable
)
resp
,
err
:=
s
.
client
.
Do
(
req
,
createdVariable
)
if
err
!=
nil
{
return
nil
,
resp
,
err
}
return
createdVariable
,
resp
,
err
}
// UpdateVariable updates an existing project variable
// The variable key must exist
//
// Gitlab API Docs:
// https://docs.gitlab.com/ce/api/build_variables.html#update-variable
func
(
s
*
VariablesService
)
UpdateVariable
(
pid
interface
{},
variableKey
string
,
variable
Variable
)
(
*
Variable
,
*
Response
,
error
)
{
project
,
err
:=
parseID
(
pid
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
u
:=
fmt
.
Sprintf
(
"projects/%s/variables/%s"
,
url
.
QueryEscape
(
project
),
variableKey
)
req
,
err
:=
s
.
client
.
NewRequest
(
"PUT"
,
u
,
variable
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
updatedVariable
:=
new
(
Variable
)
resp
,
err
:=
s
.
client
.
Do
(
req
,
updatedVariable
)
if
err
!=
nil
{
return
nil
,
resp
,
err
}
return
updatedVariable
,
resp
,
err
}
// RemoveVariable removes a project variable of a given project identified by its key
//
// Gitlab API Docs:
// https://docs.gitlab.com/ce/api/build_variables.html#remove-variable
func
(
s
*
VariablesService
)
RemoveVariable
(
pid
interface
{},
variableKey
string
)
(
*
Response
,
error
)
{
project
,
err
:=
parseID
(
pid
)
if
err
!=
nil
{
return
nil
,
err
}
u
:=
fmt
.
Sprintf
(
"projects/%s/variables/%s"
,
url
.
QueryEscape
(
project
),
variableKey
)
req
,
err
:=
s
.
client
.
NewRequest
(
"DELETE"
,
u
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
return
s
.
client
.
Do
(
req
,
nil
)
}
variables_test.go
0 → 100644
View file @
2aec56c8
package
gitlab
import
(
"fmt"
"net/http"
"reflect"
"testing"
)
func
TestListVariables
(
t
*
testing
.
T
)
{
mux
,
server
,
client
:=
setup
()
defer
teardown
(
server
)
mux
.
HandleFunc
(
"/projects/1/variables"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
testMethod
(
t
,
r
,
"GET"
)
fmt
.
Fprint
(
w
,
`[{"key":"MY_KEY", "value": "MY_VALUE"},{"key":"MY_KEY2", "value": "MY_VALUE2"}]`
)
})
variables
,
_
,
err
:=
client
.
Variables
.
ListVariables
(
1
)
if
err
!=
nil
{
t
.
Errorf
(
"Projects.ListVariables returned error: %v"
,
err
)
}
want
:=
[]
*
Variable
{{
Key
:
"MY_KEY"
,
Value
:
"MY_VALUE"
},
{
Key
:
"MY_KEY2"
,
Value
:
"MY_VALUE2"
}}
if
!
reflect
.
DeepEqual
(
want
,
variables
)
{
t
.
Errorf
(
"Projects.ListVariables returned %+v, want %+v"
,
variables
,
want
)
}
}
func
TestGetSingleVariable
(
t
*
testing
.
T
)
{
mux
,
server
,
client
:=
setup
()
defer
teardown
(
server
)
mux
.
HandleFunc
(
"/projects/1/variables/MY_KEY"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
testMethod
(
t
,
r
,
"GET"
)
fmt
.
Fprint
(
w
,
`{"key":"MY_KEY", "value": "MY_VALUE"}`
)
})
variable
,
_
,
err
:=
client
.
Variables
.
GetSingleVariable
(
1
,
"MY_KEY"
)
if
err
!=
nil
{
t
.
Errorf
(
"Projects.GetSingleVariable returned error: %v"
,
err
)
}
want
:=
&
Variable
{
Key
:
"MY_KEY"
,
Value
:
"MY_VALUE"
}
if
!
reflect
.
DeepEqual
(
want
,
variable
)
{
t
.
Errorf
(
"Projects.ListVariables returned %+v, want %+v"
,
variable
,
want
)
}
}
func
testCreateVariable
(
t
*
testing
.
T
)
{
mux
,
server
,
client
:=
setup
()
defer
teardown
(
server
)
mux
.
HandleFunc
(
"/projects/1/variables"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
testMethod
(
t
,
r
,
"POST"
)
testJsonBody
(
t
,
r
,
values
{
"key"
:
"MY_KEY"
,
"value"
:
"MY_VALUE"
,
})
fmt
.
Fprint
(
w
,
`{"key":"MY_KEY", "value": "MY_VALUE"}`
)
})
want
:=
&
Variable
{
Key
:
"MY_KEY"
,
Value
:
"MY_VALUE"
}
variable
,
_
,
err
:=
client
.
Variables
.
CreateVariable
(
1
,
*
want
)
if
err
!=
nil
{
t
.
Errorf
(
"Projects.CreateVariable returned error: %v"
,
err
)
}
if
!
reflect
.
DeepEqual
(
want
,
variable
)
{
t
.
Errorf
(
"Projects.CreateVariable returned %+v, want %+v"
,
variable
,
want
)
}
}
func
testUpdateVariable
(
t
*
testing
.
T
)
{
mux
,
server
,
client
:=
setup
()
defer
teardown
(
server
)
mux
.
HandleFunc
(
"/projects/1/variables/MY_KEY"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
testMethod
(
t
,
r
,
"PUT"
)
testJsonBody
(
t
,
r
,
values
{
"key"
:
"MY_KEY"
,
"value"
:
"MY_NEW_VALUE"
,
})
fmt
.
Fprint
(
w
,
`{"key":"MY_KEY", "value": "MY_NEW_VALUE"}`
)
})
want
:=
&
Variable
{
Key
:
"MY_KEY"
,
Value
:
"MY_NEW_VALUE"
}
variable
,
_
,
err
:=
client
.
Variables
.
UpdateVariable
(
1
,
"MY_KEY"
,
*
want
)
if
err
!=
nil
{
t
.
Errorf
(
"Projects.UpdateVariable returned error: %v"
,
err
)
}
if
!
reflect
.
DeepEqual
(
want
,
variable
)
{
t
.
Errorf
(
"Projects.UpdateVariable returned %+v, want %+v"
,
variable
,
want
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment