Unverified Commit fcd8d439 authored by Sander van Harmelen's avatar Sander van Harmelen Committed by GitHub

Merge pull request #653 from xanzy/svh/f-var-types

Make a better distinction between variables
parents e585e4e8 7537b744
......@@ -20,10 +20,6 @@ incompatible changes that were needed to fully support the V4 Gitlab API.
This API client package covers most of the existing Gitlab API calls and is updated regularly
to add new and/or missing endpoints. Currently the following services are supported:
- [ ] Discussions (threaded comments)
- [ ] Epic Issues
- [ ] Epics
- [ ] Geo Nodes
- [x] Award Emojis
- [x] Branches
- [x] Broadcast Messages
......@@ -32,9 +28,13 @@ to add new and/or missing endpoints. Currently the following services are suppor
- [x] Custom Attributes
- [x] Deploy Keys
- [x] Deployments
- [ ] Discussions (threaded comments)
- [x] Environments
- [ ] Epic Issues
- [ ] Epics
- [x] Events
- [x] Feature Flags
- [ ] Geo Nodes
- [x] GitLab CI Config Templates
- [x] Gitignores Templates
- [x] Group Access Requests
......
package gitlab
import (
"fmt"
)
// BuildVariablesService 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 BuildVariablesService struct {
client *Client
}
// BuildVariable represents a variable available for each build of the given project
//
// Gitlab API Docs : https://docs.gitlab.com/ce/api/build_variables.html
type BuildVariable struct {
Key string `json:"key"`
Value string `json:"value"`
Protected bool `json:"protected"`
}
func (v BuildVariable) String() string {
return Stringify(v)
}
// ListBuildVariablesOptions are the parameters to ListBuildVariables()
//
// Gitlab API Docs:
// https://docs.gitlab.com/ce/api/build_variables.html#list-project-variables
type ListBuildVariablesOptions ListOptions
// ListBuildVariables 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 *BuildVariablesService) ListBuildVariables(pid interface{}, opts *ListBuildVariablesOptions, options ...OptionFunc) ([]*BuildVariable, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/variables", pathEscape(project))
req, err := s.client.NewRequest("GET", u, opts, options)
if err != nil {
return nil, nil, err
}
var v []*BuildVariable
resp, err := s.client.Do(req, &v)
if err != nil {
return nil, resp, err
}
return v, resp, err
}
// GetBuildVariable 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 *BuildVariablesService) GetBuildVariable(pid interface{}, key string, options ...OptionFunc) (*BuildVariable, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), key)
req, err := s.client.NewRequest("GET", u, nil, options)
if err != nil {
return nil, nil, err
}
v := new(BuildVariable)
resp, err := s.client.Do(req, v)
if err != nil {
return nil, resp, err
}
return v, resp, err
}
// CreateBuildVariableOptions are the parameters to CreateBuildVariable()
//
// Gitlab API Docs:
// https://docs.gitlab.com/ce/api/build_variables.html#create-variable
type CreateBuildVariableOptions struct {
Key *string `url:"key" json:"key"`
Value *string `url:"value" json:"value"`
Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
}
// CreateBuildVariable creates a variable for a given project
//
// Gitlab API Docs:
// https://docs.gitlab.com/ce/api/build_variables.html#create-variable
func (s *BuildVariablesService) CreateBuildVariable(pid interface{}, opt *CreateBuildVariableOptions, options ...OptionFunc) (*BuildVariable, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/variables", pathEscape(project))
req, err := s.client.NewRequest("POST", u, opt, options)
if err != nil {
return nil, nil, err
}
v := new(BuildVariable)
resp, err := s.client.Do(req, v)
if err != nil {
return nil, resp, err
}
return v, resp, err
}
// UpdateBuildVariableOptions are the parameters to UpdateBuildVariable()
//
// Gitlab API Docs:
// https://docs.gitlab.com/ce/api/build_variables.html#update-variable
type UpdateBuildVariableOptions struct {
Key *string `url:"key" json:"key"`
Value *string `url:"value" json:"value"`
Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
}
// UpdateBuildVariable 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 *BuildVariablesService) UpdateBuildVariable(pid interface{}, key string, opt *UpdateBuildVariableOptions, options ...OptionFunc) (*BuildVariable, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), key)
req, err := s.client.NewRequest("PUT", u, opt, options)
if err != nil {
return nil, nil, err
}
v := new(BuildVariable)
resp, err := s.client.Do(req, v)
if err != nil {
return nil, resp, err
}
return v, resp, err
}
// RemoveBuildVariable 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 *BuildVariablesService) RemoveBuildVariable(pid interface{}, key string, options ...OptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), key)
req, err := s.client.NewRequest("DELETE", u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
package gitlab
import (
"fmt"
"net/http"
"reflect"
"testing"
)
const (
myKey = "MY_KEY"
myValue = "MY_VALUE"
myKey2 = "MY_KEY2"
myValue2 = "MY_VALUE2"
myNewValue = "MY_NEW_VALUE"
)
func TestListBuildVariables(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/variables", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprintf(w,
`[{"key":"%s","value":"%s"},{"key":"%s","value":"%s"}]`, myKey, myValue, myKey2, myValue2)
})
variables, _, err := client.BuildVariables.ListBuildVariables(1, nil)
if err != nil {
t.Errorf("ListBuildVariables returned error: %v", err)
}
want := []*BuildVariable{{Key: myKey, Value: myValue}, {Key: myKey2, Value: myValue2}}
if !reflect.DeepEqual(want, variables) {
t.Errorf("ListBuildVariables returned %+v, want %+v", variables, want)
}
}
func TestGetBuildVariable(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/variables/"+myKey, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprintf(w, `{"key":"%s","value":"%s"}`, myKey, myValue)
})
variable, _, err := client.BuildVariables.GetBuildVariable(1, myKey)
if err != nil {
t.Errorf("GetBuildVariable returned error: %v", err)
}
want := &BuildVariable{Key: myKey, Value: myValue}
if !reflect.DeepEqual(want, variable) {
t.Errorf("GetBuildVariable returned %+v, want %+v", variable, want)
}
}
func TestCreateBuildVariable(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/variables", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprintf(w, `{"key":"%s","value":"%s", "protected": false}`, myKey, myValue)
})
opt := &CreateBuildVariableOptions{String(myKey), String(myValue), Bool(false)}
variable, _, err := client.BuildVariables.CreateBuildVariable(1, opt)
if err != nil {
t.Errorf("CreateBuildVariable returned error: %v", err)
}
want := &BuildVariable{Key: myKey, Value: myValue, Protected: false}
if !reflect.DeepEqual(want, variable) {
t.Errorf("CreateBuildVariable returned %+v, want %+v", variable, want)
}
}
func TestUpdateBuildVariable(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/variables/"+myKey, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
fmt.Fprintf(w, `{"key":"%s","value":"%s", "protected": false}`, myKey, myNewValue)
})
opt := &UpdateBuildVariableOptions{String(myKey), String(myNewValue), Bool(false)}
variable, _, err := client.BuildVariables.UpdateBuildVariable(1, myKey, opt)
if err != nil {
t.Errorf("UpdateBuildVariable returned error: %v", err)
}
want := &BuildVariable{Key: myKey, Value: myNewValue, Protected: false}
if !reflect.DeepEqual(want, variable) {
t.Errorf("UpdateBuildVariable returned %+v, want %+v", variable, want)
}
}
func TestRemoveBuildVariable(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/variables/"+myKey, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
})
_, err := client.BuildVariables.RemoveBuildVariable(1, myKey)
if err != nil {
t.Errorf("RemoveBuildVariable returned error: %v", err)
}
}
......@@ -204,7 +204,7 @@ var notificationLevelTypes = map[string]NotificationLevelValue{
// GitLab API docs: https://docs.gitlab.com/ce/api/
type VisibilityValue string
// List of available visibility levels
// List of available visibility levels.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/
const (
......@@ -213,6 +213,19 @@ const (
PublicVisibility VisibilityValue = "public"
)
// VariableTypeValue represents a variable type within GitLab.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/
type VariableTypeValue string
// List of available variable types.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/
const (
EnvVariableType VariableTypeValue = "env_var"
FileVariableType VariableTypeValue = "file"
)
// MergeMethodValue represents a project merge type within GitLab.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/projects.html#project-merge-method
......@@ -291,7 +304,6 @@ type Client struct {
Boards *IssueBoardsService
Branches *BranchesService
BroadcastMessage *BroadcastMessagesService
BuildVariables *BuildVariablesService
CIYMLTemplate *CIYMLTemplatesService
Commits *CommitsService
ContainerRegistry *ContainerRegistryService
......@@ -441,7 +453,6 @@ func newClient(httpClient *http.Client) *Client {
c.Boards = &IssueBoardsService{client: c}
c.Branches = &BranchesService{client: c}
c.BroadcastMessage = &BroadcastMessagesService{client: c}
c.BuildVariables = &BuildVariablesService{client: c}
c.CIYMLTemplate = &CIYMLTemplatesService{client: c}
c.Commits = &CommitsService{client: c}
c.ContainerRegistry = &ContainerRegistryService{client: c}
......
......@@ -35,9 +35,10 @@ type GroupVariablesService struct {
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html
type GroupVariable struct {
Key string `json:"key"`
Value string `json:"value"`
Protected bool `json:"protected"`
Key string `json:"key"`
Value string `json:"value"`
VariableType VariableTypeValue `json:"variable_type"`
Protected bool `json:"protected"`
}
func (v GroupVariable) String() string {
......@@ -94,11 +95,23 @@ func (s *GroupVariablesService) GetVariable(gid interface{}, key string, options
return v, resp, err
}
// CreateGroupVariableOptions represents the available CreateVariable()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
type CreateGroupVariableOptions struct {
Key *string `url:"key,omitempty" json:"key,omitempty"`
Value *string `url:"value,omitempty" json:"value,omitempty"`
VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
}
// CreateVariable creates a new group variable.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
func (s *GroupVariablesService) CreateVariable(gid interface{}, opt *CreateVariableOptions, options ...OptionFunc) (*GroupVariable, *Response, error) {
func (s *GroupVariablesService) CreateVariable(gid interface{}, opt *CreateGroupVariableOptions, options ...OptionFunc) (*GroupVariable, *Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, nil, err
......@@ -119,12 +132,23 @@ func (s *GroupVariablesService) CreateVariable(gid interface{}, opt *CreateVaria
return v, resp, err
}
// UpdateGroupVariableOptions represents the available UpdateVariable()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
type UpdateGroupVariableOptions struct {
Value *string `url:"value,omitempty" json:"value,omitempty"`
VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
}
// UpdateVariable updates the position of an existing
// group issue board list.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
func (s *GroupVariablesService) UpdateVariable(gid interface{}, key string, opt *UpdateVariableOptions, options ...OptionFunc) (*GroupVariable, *Response, error) {
func (s *GroupVariablesService) UpdateVariable(gid interface{}, key string, opt *UpdateGroupVariableOptions, options ...OptionFunc) (*GroupVariable, *Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, nil, err
......
......@@ -35,11 +35,12 @@ type ProjectVariablesService struct {
// GitLab API docs:
// https://docs.gitlab.com/ee/api/project_level_variables.html
type ProjectVariable struct {
Key string `json:"key"`
Value string `json:"value"`
Protected bool `json:"protected"`
Masked bool `json:"masked"`
EnvironmentScope string `json:"environment_scope"`
Key string `json:"key"`
Value string `json:"value"`
VariableType VariableTypeValue `json:"variable_type"`
Protected bool `json:"protected"`
Masked bool `json:"masked"`
EnvironmentScope string `json:"environment_scope"`
}
func (v ProjectVariable) String() string {
......@@ -96,24 +97,25 @@ func (s *ProjectVariablesService) GetVariable(pid interface{}, key string, optio
return v, resp, err
}
// CreateVariableOptions represents the available
// CreateVariable() options.
// CreateProjectVariableOptions represents the available CreateVariable()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/project_level_variables.html#create-variable
type CreateVariableOptions struct {
Key *string `url:"key,omitempty" json:"key,omitempty"`
Value *string `url:"value,omitempty" json:"value,omitempty"`
Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
Masked *bool `url:"masked,omitempty" json:"masked,omitempty"`
EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
type CreateProjectVariableOptions struct {
Key *string `url:"key,omitempty" json:"key,omitempty"`
Value *string `url:"value,omitempty" json:"value,omitempty"`
VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
Masked *bool `url:"masked,omitempty" json:"masked,omitempty"`
EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
}
// CreateVariable creates a new project variable.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/project_level_variables.html#create-variable
func (s *ProjectVariablesService) CreateVariable(pid interface{}, opt *CreateVariableOptions, options ...OptionFunc) (*ProjectVariable, *Response, error) {
func (s *ProjectVariablesService) CreateVariable(pid interface{}, opt *CreateProjectVariableOptions, options ...OptionFunc) (*ProjectVariable, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
......@@ -134,23 +136,24 @@ func (s *ProjectVariablesService) CreateVariable(pid interface{}, opt *CreateVar
return v, resp, err
}
// UpdateVariableOptions represents the available
// UpdateVariable() options.
// UpdateProjectVariableOptions represents the available UpdateVariable()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/project_level_variables.html#update-variable
type UpdateVariableOptions struct {
Value *string `url:"value,omitempty" json:"value,omitempty"`
Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
Masked *bool `url:"masked,omitempty" json:"masked,omitempty"`
EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
type UpdateProjectVariableOptions struct {
Value *string `url:"value,omitempty" json:"value,omitempty"`
VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
Masked *bool `url:"masked,omitempty" json:"masked,omitempty"`
EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
}
// UpdateVariable updates a project's variable
// UpdateVariable updates a project's variable.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/project_level_variables.html#update-variable
func (s *ProjectVariablesService) UpdateVariable(pid interface{}, key string, opt *UpdateVariableOptions, options ...OptionFunc) (*ProjectVariable, *Response, error) {
func (s *ProjectVariablesService) UpdateVariable(pid interface{}, key string, opt *UpdateProjectVariableOptions, options ...OptionFunc) (*ProjectVariable, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
......
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