Verified Commit d4755b79 authored by Aayush's avatar Aayush 💬 Committed by GitLab
Browse files

feat: add new endpoint for fetching all the runner manager information

Changelog: Improvements
parent 637e16a7
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -63,6 +63,34 @@ const (
		"locked": false
	}`

	// exampleListRunnerManagersResponse provides fixture for ListRunnerManagers tests.
	exampleListRunnerManagersResponse = `[
		{
			"id": 1,
			"system_id": "s_89e5e9956577",
			"version": "16.11.1",
			"revision": "535ced5f",
			"platform": "linux",
			"architecture": "amd64",
			"created_at": "2024-06-09T11:12:02.507Z",
			"contacted_at": "2024-06-09T06:30:09.355Z",
			"ip_address": "127.0.0.1",
			"status": "offline"
		},
		{
			"id": 2,
			"system_id": "runner-2",
			"version": "16.11.0",
			"revision": "91a27b2a",
			"platform": "linux",
			"architecture": "amd64",
			"created_at": "2024-06-09T09:12:02.507Z",
			"contacted_at": "2024-06-09T06:30:09.355Z",
			"ip_address": "127.0.0.1",
			"status": "offline"
		}
	]`

	// exampleEventUserName provides a fixture for a event user's name.
	exampleEventUserName = "John Smith"

+33 −0
Original line number Diff line number Diff line
@@ -55,6 +55,11 @@ type (
		// GitLab API docs:
		// https://docs.gitlab.com/api/runners/#list-jobs-processed-by-a-runner
		ListRunnerJobs(rid any, opt *ListRunnerJobsOptions, options ...RequestOptionFunc) ([]*Job, *Response, error)
		// ListRunnerManagers lists all the managers of a runner.
		//
		// GitLab API docs:
		// https://docs.gitlab.com/api/runners/#list-all-runners-managers
		ListRunnerManagers(rid any, options ...RequestOptionFunc) ([]*RunnerManager, *Response, error)
		// ListProjectRunners gets a list of runners accessible by the authenticated user.
		//
		// GitLab API docs:
@@ -208,6 +213,22 @@ type RunnerDetailsGroup struct {
	WebURL string `json:"web_url"`
}

// RunnerManager represents a GitLab CI runner manager.
//
// GitLab API docs: https://docs.gitlab.com/api/runners/#list-all-runners-managers
type RunnerManager struct {
	ID           int64      `json:"id"`
	SystemID     string     `json:"system_id"`
	Version      string     `json:"version"`
	Revision     string     `json:"revision"`
	Platform     string     `json:"platform"`
	Architecture string     `json:"architecture"`
	CreatedAt    *time.Time `json:"created_at"`
	ContactedAt  *time.Time `json:"contacted_at"`
	IPAddress    string     `json:"ip_address"`
	Status       string     `json:"status"`
}

// ListRunnersOptions represents the available ListRunners() options.
//
// GitLab API docs:
@@ -328,6 +349,18 @@ func (s *RunnersService) ListRunnerJobs(rid any, opt *ListRunnerJobsOptions, opt
	return res, resp, nil
}

func (s *RunnersService) ListRunnerManagers(rid any, options ...RequestOptionFunc) ([]*RunnerManager, *Response, error) {
	res, resp, err := do[[]*RunnerManager](s.client,
		withMethod(http.MethodGet),
		withPath("runners/%s/managers", RunnerID{rid}),
		withRequestOpts(options...),
	)
	if err != nil {
		return nil, resp, err
	}
	return res, resp, nil
}

// ListProjectRunnersOptions represents the available ListProjectRunners()
// options.
//
+52 −0
Original line number Diff line number Diff line
@@ -39,6 +39,58 @@ func TestDisableRunner(t *testing.T) {
	require.NoError(t, err)
}

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

	// GIVEN a runner ID and a mocked API that returns runner managers
	mux.HandleFunc("/api/v4/runners/1/managers", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, exampleListRunnerManagersResponse)
	})

	// WHEN ListRunnerManagers is called for that runner
	managers, resp, err := client.Runners.ListRunnerManagers(1)

	// THEN no error is returned and the response contains the expected managers
	require.NoError(t, err)
	require.NotNil(t, resp)
	assert.Len(t, managers, 2)

	createdAt1 := time.Date(2024, time.June, 9, 11, 12, 2, 507000000, time.UTC)
	contactedAt1 := time.Date(2024, time.June, 9, 6, 30, 9, 355000000, time.UTC)
	createdAt2 := time.Date(2024, time.June, 9, 9, 12, 2, 507000000, time.UTC)
	contactedAt2 := time.Date(2024, time.June, 9, 6, 30, 9, 355000000, time.UTC)

	want := []*RunnerManager{
		{
			ID:           1,
			SystemID:     "s_89e5e9956577",
			Version:      "16.11.1",
			Revision:     "535ced5f",
			Platform:     "linux",
			Architecture: "amd64",
			CreatedAt:    &createdAt1,
			ContactedAt:  &contactedAt1,
			IPAddress:    "127.0.0.1",
			Status:       "offline",
		},
		{
			ID:           2,
			SystemID:     "runner-2",
			Version:      "16.11.0",
			Revision:     "91a27b2a",
			Platform:     "linux",
			Architecture: "amd64",
			CreatedAt:    &createdAt2,
			ContactedAt:  &contactedAt2,
			IPAddress:    "127.0.0.1",
			Status:       "offline",
		},
	}
	assert.Equal(t, want, managers)
}

func TestListRunnersJobs(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)
+45 −0
Original line number Diff line number Diff line
@@ -441,6 +441,51 @@ func (c *MockRunnersServiceInterfaceListRunnerJobsCall) DoAndReturn(f func(any,
	return c
}

// ListRunnerManagers mocks base method.
func (m *MockRunnersServiceInterface) ListRunnerManagers(rid any, options ...gitlab.RequestOptionFunc) ([]*gitlab.RunnerManager, *gitlab.Response, error) {
	m.ctrl.T.Helper()
	varargs := []any{rid}
	for _, a := range options {
		varargs = append(varargs, a)
	}
	ret := m.ctrl.Call(m, "ListRunnerManagers", varargs...)
	ret0, _ := ret[0].([]*gitlab.RunnerManager)
	ret1, _ := ret[1].(*gitlab.Response)
	ret2, _ := ret[2].(error)
	return ret0, ret1, ret2
}

// ListRunnerManagers indicates an expected call of ListRunnerManagers.
func (mr *MockRunnersServiceInterfaceMockRecorder) ListRunnerManagers(rid any, options ...any) *MockRunnersServiceInterfaceListRunnerManagersCall {
	mr.mock.ctrl.T.Helper()
	varargs := append([]any{rid}, options...)
	call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRunnerManagers", reflect.TypeOf((*MockRunnersServiceInterface)(nil).ListRunnerManagers), varargs...)
	return &MockRunnersServiceInterfaceListRunnerManagersCall{Call: call}
}

// MockRunnersServiceInterfaceListRunnerManagersCall wrap *gomock.Call
type MockRunnersServiceInterfaceListRunnerManagersCall struct {
	*gomock.Call
}

// Return rewrite *gomock.Call.Return
func (c *MockRunnersServiceInterfaceListRunnerManagersCall) Return(arg0 []*gitlab.RunnerManager, arg1 *gitlab.Response, arg2 error) *MockRunnersServiceInterfaceListRunnerManagersCall {
	c.Call = c.Call.Return(arg0, arg1, arg2)
	return c
}

// Do rewrite *gomock.Call.Do
func (c *MockRunnersServiceInterfaceListRunnerManagersCall) Do(f func(any, ...gitlab.RequestOptionFunc) ([]*gitlab.RunnerManager, *gitlab.Response, error)) *MockRunnersServiceInterfaceListRunnerManagersCall {
	c.Call = c.Call.Do(f)
	return c
}

// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockRunnersServiceInterfaceListRunnerManagersCall) DoAndReturn(f func(any, ...gitlab.RequestOptionFunc) ([]*gitlab.RunnerManager, *gitlab.Response, error)) *MockRunnersServiceInterfaceListRunnerManagersCall {
	c.Call = c.Call.DoAndReturn(f)
	return c
}

// ListRunners mocks base method.
func (m *MockRunnersServiceInterface) ListRunners(opt *gitlab.ListRunnersOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Runner, *gitlab.Response, error) {
	m.ctrl.T.Helper()