Commit bdd315a8 authored by Sander van Harmelen's avatar Sander van Harmelen

Fixes #181

parent a136d2e2
......@@ -17,8 +17,9 @@ type BuildVariablesService struct {
//
// Gitlab API Docs : https://docs.gitlab.com/ce/api/build_variables.html
type BuildVariable struct {
Key string `json:"key"`
Value string `json:"value"`
Key string `json:"key"`
Value string `json:"value"`
Protected bool `json:"protected"`
}
func (v BuildVariable) String() string {
......@@ -83,18 +84,28 @@ func (s *BuildVariablesService) GetBuildVariable(pid interface{}, key string, op
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" json:"protected"`
}
// 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{}, key, value string, options ...OptionFunc) (*BuildVariable, *Response, error) {
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", url.QueryEscape(project))
req, err := s.client.NewRequest("POST", u, BuildVariable{key, value}, options)
req, err := s.client.NewRequest("POST", u, opt, options)
if err != nil {
return nil, nil, err
}
......@@ -108,19 +119,29 @@ func (s *BuildVariablesService) CreateBuildVariable(pid interface{}, key, value
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" json:"protected"`
}
// 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, value string, options ...OptionFunc) (*BuildVariable, *Response, error) {
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", url.QueryEscape(project), key)
req, err := s.client.NewRequest("PUT", u, BuildVariable{key, value}, options)
req, err := s.client.NewRequest("PUT", u, opt, options)
if err != nil {
return nil, nil, err
}
......
......@@ -62,19 +62,16 @@ func TestCreateBuildVariable(t *testing.T) {
mux.HandleFunc("/projects/1/variables", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testJSONBody(t, r, values{
"key": myKey,
"value": myValue,
})
fmt.Fprintf(w, `{"key":"%s","value":"%s"}`, myKey, myValue)
fmt.Fprintf(w, `{"key":"%s","value":"%s", "protected": false}`, myKey, myValue)
})
variable, _, err := client.BuildVariables.CreateBuildVariable(1, 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}
want := &BuildVariable{Key: myKey, Value: myValue, Protected: false}
if !reflect.DeepEqual(want, variable) {
t.Errorf("CreateBuildVariable returned %+v, want %+v", variable, want)
}
......@@ -86,19 +83,16 @@ func TestUpdateBuildVariable(t *testing.T) {
mux.HandleFunc("/projects/1/variables/"+myKey, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testJSONBody(t, r, values{
"key": myKey,
"value": myNewValue,
})
fmt.Fprintf(w, `{"key":"%s","value":"%s"}`, myKey, myNewValue)
fmt.Fprintf(w, `{"key":"%s","value":"%s", "protected": false}`, myKey, myNewValue)
})
variable, _, err := client.BuildVariables.UpdateBuildVariable(1, 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}
want := &BuildVariable{Key: myKey, Value: myNewValue, Protected: false}
if !reflect.DeepEqual(want, variable) {
t.Errorf("UpdateBuildVariable returned %+v, want %+v", variable, want)
}
......
......@@ -118,8 +118,8 @@ const (
// CommitAction represents a single file action within a commit.
type CommitAction struct {
Action FileAction `url:"action" json:"action,omitempty"`
FilePath string `url:"file_path" json:"file_path,omitempty"`
Action FileAction `url:"action" json:"action"`
FilePath string `url:"file_path" json:"file_path"`
PreviousPath string `url:"previous_path,omitempty" json:"previous_path,omitempty"`
Content string `url:"content,omitempty" json:"content,omitempty"`
Encoding string `url:"encoding,omitempty" json:"encoding,omitempty"`
......@@ -130,9 +130,9 @@ type CommitAction struct {
// GitLab API docs:
// https://gitlab.com/gitlab-org/gitlab-ce/blob/8-16-stable/doc/api/commits.md#create-a-commit-with-multiple-files-and-actions
type CreateCommitOptions struct {
BranchName *string `url:"branch_name" json:"branch_name,omitempty"`
CommitMessage *string `url:"commit_message" json:"commit_message,omitempty"`
Actions []*CommitAction `url:"actions" json:"actions,omitempty"`
BranchName *string `url:"branch_name" json:"branch_name"`
CommitMessage *string `url:"commit_message" json:"commit_message"`
Actions []*CommitAction `url:"actions" json:"actions"`
AuthorEmail *string `url:"author_email,omitempty" json:"author_email,omitempty"`
AuthorName *string `url:"author_name,omitempty" json:"author_name,omitempty"`
}
......
......@@ -13,12 +13,6 @@ func TestGetCommitStatuses(t *testing.T) {
mux.HandleFunc("/projects/1/repository/commits/b0b3a907f41409829b307a28b82fdbd552ee5a27/statuses", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"ref": "master",
"stage": "test",
"name": "ci/jenkins",
"all": "true",
})
fmt.Fprint(w, `[{"id":1}]`)
})
......@@ -41,14 +35,6 @@ func TestSetCommitStatus(t *testing.T) {
mux.HandleFunc("/projects/1/statuses/b0b3a907f41409829b307a28b82fdbd552ee5a27", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testJSONBody(t, r, values{
"state": "running",
"context": "",
"ref": "master",
"name": "ci/jenkins",
"target_url": "http://abc",
"description": "build",
})
fmt.Fprint(w, `{"id":1}`)
})
......
......@@ -2,12 +2,9 @@ package gitlab
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
)
......@@ -46,41 +43,6 @@ func testMethod(t *testing.T, r *http.Request, want string) {
}
}
type values map[string]string
func testFormValues(t *testing.T, r *http.Request, values values) {
want := url.Values{}
for k, v := range values {
want.Add(k, v)
}
err := r.ParseForm()
if err != nil {
t.Errorf("Error parsing form: %v", err)
}
if got := r.Form; !reflect.DeepEqual(got, want) {
t.Errorf("Request parameters: %v, want %v", got, want)
}
}
func testJSONBody(t *testing.T, r *http.Request, want values) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
var got values
err = json.Unmarshal(b, &got)
if err != nil {
t.Errorf("Error unmarshalling request body: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Request parameters: %v, want %v", got, want)
}
}
func TestNewClient(t *testing.T) {
c := NewClient(nil, "")
......
......@@ -53,7 +53,6 @@ func TestCreatePipeline(t *testing.T) {
mux.HandleFunc("/projects/1/pipeline", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testJSONBody(t, r, values{"ref": "master"})
fmt.Fprint(w, `{"id":1, "status":"pending"}`)
})
......
......@@ -13,16 +13,6 @@ func TestListProjects(t *testing.T) {
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
"per_page": "3",
"archived": "true",
"order_by": "name",
"sort": "asc",
"search": "query",
"simple": "true",
"visibility": "public",
})
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
......@@ -53,17 +43,6 @@ func TestListOwnedProjects(t *testing.T) {
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
"per_page": "3",
"archived": "true",
"order_by": "name",
"sort": "asc",
"search": "query",
"simple": "true",
"owned": "true",
"visibility": "public",
})
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
......@@ -95,17 +74,6 @@ func TestListStarredProjects(t *testing.T) {
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
"per_page": "3",
"archived": "true",
"order_by": "name",
"sort": "asc",
"search": "query",
"simple": "true",
"starred": "true",
"visibility": "public",
})
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
......@@ -178,10 +146,6 @@ func TestCreateProject(t *testing.T) {
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testJSONBody(t, r, values{
"name": "n",
})
fmt.Fprint(w, `{"id":1}`)
})
......
......@@ -152,7 +152,7 @@ func (s *ServicesService) DeleteHipChatService(pid interface{}, options ...Optio
type SetDroneCIServiceOptions struct {
Token *string `url:"token" json:"token" `
DroneURL *string `url:"drone_url" json:"drone_url"`
EnableSSLVerification *string `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
EnableSSLVerification *bool `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
}
// SetDroneCIService sets Drone CI service for a project.
......@@ -197,7 +197,7 @@ func (s *ServicesService) DeleteDroneCIService(pid interface{}, options ...Optio
type DroneCIServiceProperties struct {
Token *string `url:"token" json:"token"`
DroneURL *string `url:"drone_url" json:"drone_url"`
EnableSSLVerification *string `url:"enable_ssl_verification" json:"enable_ssl_verification"`
EnableSSLVerification *bool `url:"enable_ssl_verification" json:"enable_ssl_verification"`
}
// DroneCIService represents Drone CI service settings.
......
......@@ -13,14 +13,9 @@ func TestSetDroneCIService(t *testing.T) {
mux.HandleFunc("/projects/1/services/drone-ci", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testJSONBody(t, r, values{
"token": "t",
"drone_url": "u",
"enable_ssl_verification": "true",
})
})
opt := &SetDroneCIServiceOptions{String("t"), String("u"), String("true")}
opt := &SetDroneCIServiceOptions{String("t"), String("u"), Bool(true)}
_, err := client.Services.SetDroneCIService(1, opt)
if err != nil {
......
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