Commit 47c8763d authored by jorcau's avatar jorcau Committed by Sander van Harmelen

Update runners (#454)

* Update runners: add maximum_timeout option

* Update runners: add new methods and tests

* Update runners: fix typo on token
parent 93c0833f
...@@ -41,6 +41,7 @@ type Runner struct { ...@@ -41,6 +41,7 @@ type Runner struct {
Name string `json:"name"` Name string `json:"name"`
Online bool `json:"online"` Online bool `json:"online"`
Status string `json:"status"` Status string `json:"status"`
Token string `json:"token"`
} }
// RunnerDetails represents the GitLab CI runner details. // RunnerDetails represents the GitLab CI runner details.
...@@ -52,11 +53,11 @@ type RunnerDetails struct { ...@@ -52,11 +53,11 @@ type RunnerDetails struct {
Description string `json:"description"` Description string `json:"description"`
ID int `json:"id"` ID int `json:"id"`
IsShared bool `json:"is_shared"` IsShared bool `json:"is_shared"`
ContactedAt *time.Time `json:"contacted_at,omitempty"` ContactedAt *time.Time `json:"contacted_at"`
Name string `json:"name"` Name string `json:"name"`
Online bool `json:"online"` Online bool `json:"online"`
Status string `json:"status"` Status string `json:"status"`
Platform string `json:"platform,omitempty"` Platform string `json:"platform"`
Projects []struct { Projects []struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
...@@ -64,11 +65,12 @@ type RunnerDetails struct { ...@@ -64,11 +65,12 @@ type RunnerDetails struct {
Path string `json:"path"` Path string `json:"path"`
PathWithNamespace string `json:"path_with_namespace"` PathWithNamespace string `json:"path_with_namespace"`
} `json:"projects"` } `json:"projects"`
Token string `json:"Token"` Token string `json:"token"`
Revision string `json:"revision,omitempty"` Revision string `json:"revision"`
TagList []string `json:"tag_list"` TagList []string `json:"tag_list"`
Version string `json:"version,omitempty"` Version string `json:"version"`
AccessLevel string `json:"access_level"` AccessLevel string `json:"access_level"`
MaximumTimeout int `json:"maximum_timeout"`
} }
// ListRunnersOptions represents the available ListRunners() options. // ListRunnersOptions represents the available ListRunners() options.
...@@ -149,12 +151,13 @@ func (s *RunnersService) GetRunnerDetails(rid interface{}, options ...OptionFunc ...@@ -149,12 +151,13 @@ func (s *RunnersService) GetRunnerDetails(rid interface{}, options ...OptionFunc
// GitLab API docs: // GitLab API docs:
// https://docs.gitlab.com/ce/api/runners.html#update-runner-39-s-details // https://docs.gitlab.com/ce/api/runners.html#update-runner-39-s-details
type UpdateRunnerDetailsOptions struct { type UpdateRunnerDetailsOptions struct {
Description *string `url:"description,omitempty" json:"description,omitempty"` Description *string `url:"description,omitempty" json:"description,omitempty"`
Active *bool `url:"active,omitempty" json:"active,omitempty"` Active *bool `url:"active,omitempty" json:"active,omitempty"`
TagList []string `url:"tag_list[],omitempty" json:"tag_list,omitempty"` TagList []string `url:"tag_list[],omitempty" json:"tag_list,omitempty"`
RunUntagged *bool `url:"run_untagged,omitempty" json:"run_untagged,omitempty"` RunUntagged *bool `url:"run_untagged,omitempty" json:"run_untagged,omitempty"`
Locked *bool `url:"locked,omitempty" json:"locked,omitempty"` Locked *bool `url:"locked,omitempty" json:"locked,omitempty"`
AccessLevel *string `url:"access_level,omitempty" json:"access_level,omitempty"` AccessLevel *string `url:"access_level,omitempty" json:"access_level,omitempty"`
MaximumTimeout *int `url:"maximum_timeout,omitempty" json:"maximum_timeout,omitempty"`
} }
// UpdateRunnerDetails updates details for a given runner. // UpdateRunnerDetails updates details for a given runner.
...@@ -324,3 +327,82 @@ func (s *RunnersService) DisableProjectRunner(pid interface{}, rid interface{}, ...@@ -324,3 +327,82 @@ func (s *RunnersService) DisableProjectRunner(pid interface{}, rid interface{},
return s.client.Do(req, nil) return s.client.Do(req, nil)
} }
// RegisterNewRunnerOptions represents the available RegisterNewRunner()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/runners.html#register-a-new-runner
type RegisterNewRunnerOptions struct {
Token *string `url:"token" json:"token"`
Description *string `url:"description,omitempty" json:"description,omitempty"`
Info *string `url:"info,omitempty" json:"info,omitempty"`
Active *bool `url:"active,omitempty" json:"active,omitempty"`
Locked *bool `url:"locked,omitempty" json:"locked,omitempty"`
RunUntagged *bool `url:"run_untagged,omitempty" json:"run_untagged,omitempty"`
TagList []string `url:"tag_list[],omitempty" json:"tag_list,omitempty"`
MaximumTimeout *int `url:"maximum_timeout,omitempty" json:"maximum_timeout,omitempty"`
}
// RegisterNewRunner registers a new Runner for the instance.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/runners.html#register-a-new-runner
func (s *RunnersService) RegisterNewRunner(opt *RegisterNewRunnerOptions, options ...OptionFunc) (*Runner, *Response, error) {
req, err := s.client.NewRequest("POST", "runners", opt, options)
if err != nil {
return nil, nil, err
}
var r *Runner
resp, err := s.client.Do(req, &r)
if err != nil {
return nil, resp, err
}
return r, resp, err
}
// DeleteRegisteredRunnerOptions represents the available
// DeleteRegisteredRunner() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/runners.html#delete-a-registered-runner
type DeleteRegisteredRunnerOptions struct {
Token *string `url:"token" json:"token"`
}
// DeleteRegisteredRunner registers a new Runner for the instance.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/runners.html#delete-a-registered-runner
func (s *RunnersService) DeleteRegisteredRunner(opt *DeleteRegisteredRunnerOptions, options ...OptionFunc) (*Response, error) {
req, err := s.client.NewRequest("DELETE", "runners", opt, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// VerifyRegisteredRunnerOptions represents the available
// VerifyRegisteredRunner() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/runners.html#verify-authentication-for-a-registered-runner
type VerifyRegisteredRunnerOptions struct {
Token *string `url:"token" json:"token"`
}
// VerifyRegisteredRunner registers a new Runner for the instance.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/runners.html#verify-authentication-for-a-registered-runner
func (s *RunnersService) VerifyRegisteredRunner(opt *VerifyRegisteredRunnerOptions, options ...OptionFunc) (*Response, error) {
req, err := s.client.NewRequest("POST", "runners/verify", opt, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
...@@ -103,7 +103,8 @@ const exampleDetailRsp = `{ ...@@ -103,7 +103,8 @@ const exampleDetailRsp = `{
"mysql" "mysql"
], ],
"version": null, "version": null,
"access_level": "ref_protected" "access_level": "ref_protected",
"maximum_timeout": 3600
}` }`
func TestUpdateRunnersDetails(t *testing.T) { func TestUpdateRunnersDetails(t *testing.T) {
...@@ -158,11 +159,109 @@ func expectedParsedDetails() *RunnerDetails { ...@@ -158,11 +159,109 @@ func expectedParsedDetails() *RunnerDetails {
PathWithNamespace string `json:"path_with_namespace"` PathWithNamespace string `json:"path_with_namespace"`
}{ID: 1, Name: "GitLab Community Edition", NameWithNamespace: "GitLab.org / GitLab Community Edition", Path: "gitlab-ce", PathWithNamespace: "gitlab-org/gitlab-ce"} }{ID: 1, Name: "GitLab Community Edition", NameWithNamespace: "GitLab.org / GitLab Community Edition", Path: "gitlab-ce", PathWithNamespace: "gitlab-org/gitlab-ce"}
timestamp, _ := time.Parse("2006-01-02T15:04:05.000Z", "2016-01-25T16:39:48.066Z") timestamp, _ := time.Parse("2006-01-02T15:04:05.000Z", "2016-01-25T16:39:48.066Z")
return &RunnerDetails{Active: true, Description: "test-1-20150125-test", ID: 6, IsShared: false, ContactedAt: &timestamp, Online: true, Status: "online", Token: "205086a8e3b9a2b818ffac9b89d102", TagList: []string{"ruby", "mysql"}, AccessLevel: "ref_protected", Projects: []struct { return &RunnerDetails{
ID int `json:"id"` Active: true,
Name string `json:"name"` Description: "test-1-20150125-test",
NameWithNamespace string `json:"name_with_namespace"` ID: 6,
Path string `json:"path"` IsShared: false,
PathWithNamespace string `json:"path_with_namespace"` ContactedAt: &timestamp,
}{proj}} Online: true,
Status: "online",
Token: "205086a8e3b9a2b818ffac9b89d102",
TagList: []string{"ruby", "mysql"},
AccessLevel: "ref_protected",
Projects: []struct {
ID int `json:"id"`
Name string `json:"name"`
NameWithNamespace string `json:"name_with_namespace"`
Path string `json:"path"`
PathWithNamespace string `json:"path_with_namespace"`
}{proj},
MaximumTimeout: 3600,
}
}
// helper function returning expected result for string: &exampleRegisterNewRunner
func expectedParsedNewRunner() *Runner {
return &Runner{
ID: 12345,
Token: "6337ff461c94fd3fa32ba3b1ff4125",
}
}
const exampleRegisterNewRunner = `{
"id": 12345,
"token": "6337ff461c94fd3fa32ba3b1ff4125"
}`
func TestRegisterNewRunner(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/runners", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, exampleRegisterNewRunner)
})
opt := &RegisterNewRunnerOptions{}
runner, resp, err := client.Runners.RegisterNewRunner(opt, nil)
if err != nil {
t.Fatalf("Runners.RegisterNewRunner returns an error: %v", err)
}
want := expectedParsedNewRunner()
if !reflect.DeepEqual(want, runner) {
t.Errorf("Runners.RegisterNewRunner returned %+v, want %+v", runner, want)
}
wantCode := 201
if !reflect.DeepEqual(wantCode, resp.StatusCode) {
t.Errorf("Runners.DeleteRegisteredRunner returned status code %+v, want %+v", resp.StatusCode, wantCode)
}
}
func TestDeleteRegisteredRunner(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/runners", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent)
})
opt := &DeleteRegisteredRunnerOptions{}
resp, err := client.Runners.DeleteRegisteredRunner(opt, nil)
if err != nil {
t.Fatalf("Runners.DeleteRegisteredRunner returns an error: %v", err)
}
want := 204
if !reflect.DeepEqual(want, resp.StatusCode) {
t.Errorf("Runners.DeleteRegisteredRunner returned returned status code %+v, want %+v", resp.StatusCode, want)
}
}
func TestVerifyRegisteredRunner(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/runners/verify", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.WriteHeader(http.StatusOK)
})
opt := &VerifyRegisteredRunnerOptions{}
resp, err := client.Runners.VerifyRegisteredRunner(opt, nil)
if err != nil {
t.Fatalf("Runners.VerifyRegisteredRunner returns an error: %v", err)
}
want := 200
if !reflect.DeepEqual(want, resp.StatusCode) {
t.Errorf("Runners.VerifyRegisteredRunner returned returned status code %+v, want %+v", resp.StatusCode, 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