Skip to content

Commit

Permalink
Bring the PR inline with the rest of the package
Browse files Browse the repository at this point in the history
  • Loading branch information
svanharmelen committed Aug 4, 2023
1 parent 25e4cfa commit f4834ae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
51 changes: 23 additions & 28 deletions users.go
Expand Up @@ -1409,51 +1409,46 @@ func (s *UsersService) DisableTwoFactor(user int, options ...RequestOptionFunc)
}
}

// CreateUserRunnerOptions represents the options available when creating a GitLab Runner
// using the new user-based flow.
// UserRunner represents a GitLab runner linked to the current user.
//
// 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"`
MaintenanceNote string `json:"maintenance_note"`
type UserRunner struct {
ID int `json:"id"`
Token string `json:"token"`
TokenExpiresAt *time.Time `json:"token_expires_at"`
}

// UserRunner represents the a GitLab runner instance created using the user-based flow
// CreateUserRunnerOptions represents the available CreateUserRunner() options.
//
// 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"`
type CreateUserRunnerOptions struct {
RunnerType *string `url:"runner_type,omitempty" json:"runner_type,omitempty"`
GroupID *int `url:"group_id,omitempty" json:"group_id,omitempty"`
ProjectID *int `url:"project_id,omitempty" json:"project_id,omitempty"`
Description *string `url:"description,omitempty" json:"description,omitempty"`
Paused *bool `url:"paused,omitempty" json:"paused,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"`
AccessLevel *string `url:"access_level,omitempty" json:"access_level,omitempty"`
MaximumTimeout *int `url:"maximum_timeout,omitempty" json:"maximum_timeout,omitempty"`
MaintenanceNote *string `url:"maintenance_note,omitempty" json:"maintenance_note,omitempty"`
}

// CreateUserRunner creates a new runner using the user-based flow and returns the authentication
// token.
// CreateUserRunner creates a runner linked to the current user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
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)
func (s *UsersService) CreateUserRunner(opts *CreateUserRunnerOptions, options ...RequestOptionFunc) (*UserRunner, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "user/runners", opts, options)
if err != nil {
return nil, nil, err
}

var r *UserRunner
resp, err := s.client.Do(req, &r)
r := new(UserRunner)
resp, err := s.client.Do(req, r)
if err != nil {
return nil, resp, err
}
Expand Down
16 changes: 10 additions & 6 deletions users_test.go
Expand Up @@ -616,8 +616,7 @@ func TestGetSingleSSHKeyForUser(t *testing.T) {
"title": "Public key",
"key": "ssh-rsa AAAA...",
"created_at": "2014-08-01T14:47:39.080Z"
}
`)
}`)
})

sshKey, _, err := client.Users.GetSSHKeyForUser(1, 1)
Expand Down Expand Up @@ -661,12 +660,17 @@ func TestCreateUserRunner(t *testing.T) {
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"id": 1234, "token": "glrt-1234567890ABCD", "token_expires_at":null}`))
w.Write([]byte(`
{
"id": 1234,
"token": "glrt-1234567890ABCD",
"token_expires_at":null
}`))
})

createRunnerOpts := &CreateUserRunnerOptions{
RunnerType: "project_type",
ProjectID: 1,
ProjectID: Int(1),
RunnerType: String("project_type"),
}

response, _, err := client.Users.CreateUserRunner(createRunnerOpts)
Expand All @@ -676,5 +680,5 @@ func TestCreateUserRunner(t *testing.T) {

require.Equal(t, 1234, response.ID)
require.Equal(t, "glrt-1234567890ABCD", response.Token)
require.Equal(t, "", response.TokenExpiresAt)
require.Equal(t, (*time.Time)(nil), response.TokenExpiresAt)
}

0 comments on commit f4834ae

Please sign in to comment.