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

Commit 6069191

Browse files
committed
Add runner service
1 parent fd3ac63 commit 6069191

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

users.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,3 +1408,63 @@ func (s *UsersService) DisableTwoFactor(user int, options ...RequestOptionFunc)
14081408
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
14091409
}
14101410
}
1411+
1412+
// CreateUserRunnerOptions represents the options available when creating a GitLab Runner
1413+
// using the new user-based flow.
1414+
//
1415+
// GitLab API docs:
1416+
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
1417+
type CreateUserRunnerOptions struct {
1418+
RunnerType string `json:"runner_type"`
1419+
GroupID int `json:"group_id"`
1420+
ProjectID int `json:"project_id"`
1421+
Description string `json:"description"`
1422+
Paused bool `json:"paused"`
1423+
Locked bool `json:"locked"`
1424+
RunUntagged bool `json:"run_untagged"`
1425+
TagList []string `json:"tag_list"`
1426+
AccessLevel string `json:"access_level"`
1427+
MaximumTimeout int `json:"maximum_timeout"`
1428+
}
1429+
1430+
// UserRunner represents the a GitLab runner instance created using the user-based flow
1431+
//
1432+
// GitLab API docs:
1433+
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
1434+
type UserRunner struct {
1435+
ID int `json:"id"`
1436+
Token string `json:"token"`
1437+
TokenExpiresAt string `json:"token_expires_at"`
1438+
RunnerType string `json:"runner_type"`
1439+
GroupID int `json:"group_id"`
1440+
ProjectID int `json:"project_id"`
1441+
Description string `json:"description"`
1442+
Paused bool `json:"paused"`
1443+
Locked bool `json:"locked"`
1444+
RunUntagged bool `json:"run_untagged"`
1445+
TagList []string `json:"tag_list"`
1446+
AccessLevel string `json:"access_level"`
1447+
MaximumTimeout int `json:"maximum_timeout"`
1448+
}
1449+
1450+
// GetUserMemberships retrieves a list of the user's memberships.
1451+
//
1452+
// GitLab API docs:
1453+
// https://docs.gitlab.com/ee/api/users.html#user-memberships
1454+
func (s *UsersService) CreateUserRunner(runnerOpts *CreateUserRunnerOptions, options ...RequestOptionFunc) (*UserRunner, *Response, error) {
1455+
// The user who owns the runner comes from the access token used to authorize the request.
1456+
u := "user/runners"
1457+
1458+
req, err := s.client.NewRequest(http.MethodPost, u, runnerOpts, options)
1459+
if err != nil {
1460+
return nil, nil, err
1461+
}
1462+
1463+
var r *UserRunner
1464+
resp, err := s.client.Do(req, &r)
1465+
if err != nil {
1466+
return nil, resp, err
1467+
}
1468+
1469+
return r, resp, nil
1470+
}

users_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,26 @@ func TestDisableUser2FA(t *testing.T) {
653653
t.Errorf("Users.DisableTwoFactor returned error: %v", err)
654654
}
655655
}
656+
657+
func TestCreateUserRunner(t *testing.T) {
658+
mux, client := setup(t)
659+
660+
path := fmt.Sprintf("/%suser/runners", apiVersionPath)
661+
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
662+
testMethod(t, r, http.MethodPost)
663+
w.WriteHeader(http.StatusCreated)
664+
w.Write([]byte(`{"id": 1234, "runner_type": "project_type"}`))
665+
})
666+
667+
createRunnerOpts := &CreateUserRunnerOptions{
668+
RunnerType: "project_type",
669+
ProjectID: 1,
670+
}
671+
672+
response, _, err := client.Users.CreateUserRunner(createRunnerOpts)
673+
if err != nil {
674+
t.Errorf("Users.CreateUserRunner returned an error: %v", err)
675+
}
676+
677+
require.Equal(t, "project_type", response.RunnerType)
678+
}

0 commit comments

Comments
 (0)