Commit 2aec56c8 authored by David's avatar David Committed by Sander van Harmelen

add support for project variables (#112)

parent a1c611d1
...@@ -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
} }
......
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)
}
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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment