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

Commit 9c07494

Browse files
authored
Merge pull request #1769 from PatrickRice-KSC/add-new-runner-api-support
Add new runner api support
2 parents fd3ac63 + f4834ae commit 9c07494

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

users.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,3 +1408,50 @@ func (s *UsersService) DisableTwoFactor(user int, options ...RequestOptionFunc)
14081408
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
14091409
}
14101410
}
1411+
1412+
// UserRunner represents a GitLab runner linked to the current user.
1413+
//
1414+
// GitLab API docs:
1415+
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
1416+
type UserRunner struct {
1417+
ID int `json:"id"`
1418+
Token string `json:"token"`
1419+
TokenExpiresAt *time.Time `json:"token_expires_at"`
1420+
}
1421+
1422+
// CreateUserRunnerOptions represents the available CreateUserRunner() options.
1423+
//
1424+
// GitLab API docs:
1425+
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
1426+
type CreateUserRunnerOptions struct {
1427+
RunnerType *string `url:"runner_type,omitempty" json:"runner_type,omitempty"`
1428+
GroupID *int `url:"group_id,omitempty" json:"group_id,omitempty"`
1429+
ProjectID *int `url:"project_id,omitempty" json:"project_id,omitempty"`
1430+
Description *string `url:"description,omitempty" json:"description,omitempty"`
1431+
Paused *bool `url:"paused,omitempty" json:"paused,omitempty"`
1432+
Locked *bool `url:"locked,omitempty" json:"locked,omitempty"`
1433+
RunUntagged *bool `url:"run_untagged,omitempty" json:"run_untagged,omitempty"`
1434+
TagList *[]string `url:"tag_list,omitempty" json:"tag_list,omitempty"`
1435+
AccessLevel *string `url:"access_level,omitempty" json:"access_level,omitempty"`
1436+
MaximumTimeout *int `url:"maximum_timeout,omitempty" json:"maximum_timeout,omitempty"`
1437+
MaintenanceNote *string `url:"maintenance_note,omitempty" json:"maintenance_note,omitempty"`
1438+
}
1439+
1440+
// CreateUserRunner creates a runner linked to the current user.
1441+
//
1442+
// GitLab API docs:
1443+
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
1444+
func (s *UsersService) CreateUserRunner(opts *CreateUserRunnerOptions, options ...RequestOptionFunc) (*UserRunner, *Response, error) {
1445+
req, err := s.client.NewRequest(http.MethodPost, "user/runners", opts, options)
1446+
if err != nil {
1447+
return nil, nil, err
1448+
}
1449+
1450+
r := new(UserRunner)
1451+
resp, err := s.client.Do(req, r)
1452+
if err != nil {
1453+
return nil, resp, err
1454+
}
1455+
1456+
return r, resp, nil
1457+
}

users_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,7 @@ func TestGetSingleSSHKeyForUser(t *testing.T) {
616616
"title": "Public key",
617617
"key": "ssh-rsa AAAA...",
618618
"created_at": "2014-08-01T14:47:39.080Z"
619-
}
620-
`)
619+
}`)
621620
})
622621

623622
sshKey, _, err := client.Users.GetSSHKeyForUser(1, 1)
@@ -653,3 +652,33 @@ func TestDisableUser2FA(t *testing.T) {
653652
t.Errorf("Users.DisableTwoFactor returned error: %v", err)
654653
}
655654
}
655+
656+
func TestCreateUserRunner(t *testing.T) {
657+
mux, client := setup(t)
658+
659+
path := fmt.Sprintf("/%suser/runners", apiVersionPath)
660+
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
661+
testMethod(t, r, http.MethodPost)
662+
w.WriteHeader(http.StatusCreated)
663+
w.Write([]byte(`
664+
{
665+
"id": 1234,
666+
"token": "glrt-1234567890ABCD",
667+
"token_expires_at":null
668+
}`))
669+
})
670+
671+
createRunnerOpts := &CreateUserRunnerOptions{
672+
ProjectID: Int(1),
673+
RunnerType: String("project_type"),
674+
}
675+
676+
response, _, err := client.Users.CreateUserRunner(createRunnerOpts)
677+
if err != nil {
678+
t.Errorf("Users.CreateUserRunner returned an error: %v", err)
679+
}
680+
681+
require.Equal(t, 1234, response.ID)
682+
require.Equal(t, "glrt-1234567890ABCD", response.Token)
683+
require.Equal(t, (*time.Time)(nil), response.TokenExpiresAt)
684+
}

0 commit comments

Comments
 (0)