Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Add runner service
Browse files Browse the repository at this point in the history
  • Loading branch information
RicePatrick committed Aug 4, 2023
1 parent fd3ac63 commit 6069191
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
60 changes: 60 additions & 0 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,3 +1408,63 @@ func (s *UsersService) DisableTwoFactor(user int, options ...RequestOptionFunc)
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}

// CreateUserRunnerOptions represents the options available when creating a GitLab Runner
// using the new user-based flow.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
type CreateUserRunnerOptions struct {
RunnerType string `json:"runner_type"`
GroupID int `json:"group_id"`
ProjectID int `json:"project_id"`
Description string `json:"description"`
Paused bool `json:"paused"`
Locked bool `json:"locked"`
RunUntagged bool `json:"run_untagged"`
TagList []string `json:"tag_list"`
AccessLevel string `json:"access_level"`
MaximumTimeout int `json:"maximum_timeout"`
}

// UserRunner represents the a GitLab runner instance created using the user-based flow
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
type UserRunner struct {
ID int `json:"id"`
Token string `json:"token"`
TokenExpiresAt string `json:"token_expires_at"`
RunnerType string `json:"runner_type"`
GroupID int `json:"group_id"`
ProjectID int `json:"project_id"`
Description string `json:"description"`
Paused bool `json:"paused"`
Locked bool `json:"locked"`
RunUntagged bool `json:"run_untagged"`
TagList []string `json:"tag_list"`
AccessLevel string `json:"access_level"`
MaximumTimeout int `json:"maximum_timeout"`
}

// GetUserMemberships retrieves a list of the user's memberships.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#user-memberships
func (s *UsersService) CreateUserRunner(runnerOpts *CreateUserRunnerOptions, options ...RequestOptionFunc) (*UserRunner, *Response, error) {
// The user who owns the runner comes from the access token used to authorize the request.
u := "user/runners"

req, err := s.client.NewRequest(http.MethodPost, u, runnerOpts, options)
if err != nil {
return nil, nil, err
}

var r *UserRunner
resp, err := s.client.Do(req, &r)
if err != nil {
return nil, resp, err
}

return r, resp, nil
}
23 changes: 23 additions & 0 deletions users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,26 @@ func TestDisableUser2FA(t *testing.T) {
t.Errorf("Users.DisableTwoFactor returned error: %v", err)
}
}

func TestCreateUserRunner(t *testing.T) {
mux, client := setup(t)

path := fmt.Sprintf("/%suser/runners", apiVersionPath)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"id": 1234, "runner_type": "project_type"}`))
})

createRunnerOpts := &CreateUserRunnerOptions{
RunnerType: "project_type",
ProjectID: 1,
}

response, _, err := client.Users.CreateUserRunner(createRunnerOpts)
if err != nil {
t.Errorf("Users.CreateUserRunner returned an error: %v", err)
}

require.Equal(t, "project_type", response.RunnerType)
}

0 comments on commit 6069191

Please sign in to comment.