Commit b13fcb79 authored by Prakash Divy's avatar Prakash Divy Committed by Timo Furrer
Browse files

feat(project_mirror): add ForceSyncProjectMirror

Changelog: Improvements
parent e4b651ff
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ type (
		AddProjectMirror(pid any, opt *AddProjectMirrorOptions, options ...RequestOptionFunc) (*ProjectMirror, *Response, error)
		EditProjectMirror(pid any, mirror int64, opt *EditProjectMirrorOptions, options ...RequestOptionFunc) (*ProjectMirror, *Response, error)
		DeleteProjectMirror(pid any, mirror int64, options ...RequestOptionFunc) (*Response, error)
		ForcePushMirrorUpdate(pid any, mirror int64, options ...RequestOptionFunc) (*Response, error)
	}

	// ProjectMirrorService handles communication with the project mirror
@@ -240,3 +241,16 @@ func (s *ProjectMirrorService) DeleteProjectMirror(pid any, mirror int64, option

	return s.client.Do(req, nil)
}

// ForcePushMirrorUpdate triggers a manual update for a project mirror.
//
// GitLab API docs:
// https://docs.gitlab.com/api/remote_mirrors/#force-push-mirror-update
func (s *ProjectMirrorService) ForcePushMirrorUpdate(pid any, mirror int64, options ...RequestOptionFunc) (*Response, error) {
	_, resp, err := do[none](s.client,
		withMethod(http.MethodPost),
		withPath("projects/%s/remote_mirrors/%d/sync", ProjectID{pid}, mirror),
		withRequestOpts(options...),
	)
	return resp, err
}
+82 −0
Original line number Diff line number Diff line
@@ -231,3 +231,85 @@ func TestProjectMirrorService_EditProjectMirror(t *testing.T) {
	require.Nil(t, pm)
	require.Equal(t, http.StatusNotFound, resp.StatusCode)
}

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

	// GIVEN a project ID and mirror ID
	mux.HandleFunc("/api/v4/projects/42/remote_mirrors/101486/sync", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPost)
		w.WriteHeader(http.StatusNoContent)
	})

	// WHEN ForcePushMirrorUpdate is called
	resp, err := client.ProjectMirrors.ForcePushMirrorUpdate(42, 101486, nil)

	// THEN the response should be successful with No Content status
	require.NoError(t, err)
	require.NotNil(t, resp)
	require.Equal(t, http.StatusNoContent, resp.StatusCode)
}

func TestProjectMirrorService_ForcePushMirrorUpdate_InvalidProjectID(t *testing.T) {
	t.Parallel()
	_, client := setup(t)

	// GIVEN an invalid project ID (float)
	// WHEN ForcePushMirrorUpdate is called
	resp, err := client.ProjectMirrors.ForcePushMirrorUpdate(42.01, 101486, nil)

	// THEN an error should be returned
	require.ErrorIs(t, err, ErrInvalidIDType)
	require.Nil(t, resp)
}

func TestProjectMirrorService_ForcePushMirrorUpdate_OptionError(t *testing.T) {
	t.Parallel()
	_, client := setup(t)

	// GIVEN a valid project ID and an error-returning option
	// WHEN ForcePushMirrorUpdate is called
	resp, err := client.ProjectMirrors.ForcePushMirrorUpdate(42, 101486, errorOption)

	// THEN an error should be returned
	require.ErrorIs(t, err, errRequestOptionFunc)
	require.Nil(t, resp)
}

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

	// GIVEN a non-existent project ID
	mux.HandleFunc("/api/v4/projects/43/remote_mirrors/101486/sync", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPost)
		w.WriteHeader(http.StatusNotFound)
	})

	// WHEN ForcePushMirrorUpdate is called
	resp, err := client.ProjectMirrors.ForcePushMirrorUpdate(43, 101486, nil)

	// THEN an 404 error should be returned
	require.Error(t, err)
	require.Equal(t, http.StatusNotFound, resp.StatusCode)
}

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

	// GIVEN a project ID and mirror ID
	mux.HandleFunc("/api/v4/projects/42/remote_mirrors/101487/sync", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPost)
		w.WriteHeader(http.StatusForbidden)
	})

	// WHEN ForcePushMirrorUpdate is called and returns 403
	resp, err := client.ProjectMirrors.ForcePushMirrorUpdate(42, 101487, nil)

	// THEN the response should be successful with Forbidden status
	require.Error(t, err)
	require.NotNil(t, resp)
	require.Equal(t, http.StatusForbidden, resp.StatusCode)
}
+44 −0
Original line number Diff line number Diff line
@@ -173,6 +173,50 @@ func (c *MockProjectMirrorServiceInterfaceEditProjectMirrorCall) DoAndReturn(f f
	return c
}

// ForcePushMirrorUpdate mocks base method.
func (m *MockProjectMirrorServiceInterface) ForcePushMirrorUpdate(pid any, mirror int64, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
	m.ctrl.T.Helper()
	varargs := []any{pid, mirror}
	for _, a := range options {
		varargs = append(varargs, a)
	}
	ret := m.ctrl.Call(m, "ForcePushMirrorUpdate", varargs...)
	ret0, _ := ret[0].(*gitlab.Response)
	ret1, _ := ret[1].(error)
	return ret0, ret1
}

// ForcePushMirrorUpdate indicates an expected call of ForcePushMirrorUpdate.
func (mr *MockProjectMirrorServiceInterfaceMockRecorder) ForcePushMirrorUpdate(pid, mirror any, options ...any) *MockProjectMirrorServiceInterfaceForcePushMirrorUpdateCall {
	mr.mock.ctrl.T.Helper()
	varargs := append([]any{pid, mirror}, options...)
	call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ForcePushMirrorUpdate", reflect.TypeOf((*MockProjectMirrorServiceInterface)(nil).ForcePushMirrorUpdate), varargs...)
	return &MockProjectMirrorServiceInterfaceForcePushMirrorUpdateCall{Call: call}
}

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

// Return rewrite *gomock.Call.Return
func (c *MockProjectMirrorServiceInterfaceForcePushMirrorUpdateCall) Return(arg0 *gitlab.Response, arg1 error) *MockProjectMirrorServiceInterfaceForcePushMirrorUpdateCall {
	c.Call = c.Call.Return(arg0, arg1)
	return c
}

// Do rewrite *gomock.Call.Do
func (c *MockProjectMirrorServiceInterfaceForcePushMirrorUpdateCall) Do(f func(any, int64, ...gitlab.RequestOptionFunc) (*gitlab.Response, error)) *MockProjectMirrorServiceInterfaceForcePushMirrorUpdateCall {
	c.Call = c.Call.Do(f)
	return c
}

// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockProjectMirrorServiceInterfaceForcePushMirrorUpdateCall) DoAndReturn(f func(any, int64, ...gitlab.RequestOptionFunc) (*gitlab.Response, error)) *MockProjectMirrorServiceInterfaceForcePushMirrorUpdateCall {
	c.Call = c.Call.DoAndReturn(f)
	return c
}

// GetProjectMirror mocks base method.
func (m *MockProjectMirrorServiceInterface) GetProjectMirror(pid any, mirror int64, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectMirror, *gitlab.Response, error) {
	m.ctrl.T.Helper()