diff --git a/lib/api/entities/remote_mirror.rb b/lib/api/entities/remote_mirror.rb index dde3e9dea9929c5d84890c762433054e574a7841..18d51726baba8c61647280f6734b4f63777bd875 100644 --- a/lib/api/entities/remote_mirror.rb +++ b/lib/api/entities/remote_mirror.rb @@ -12,6 +12,9 @@ class RemoteMirror < Grape::Entity expose :last_successful_update_at expose :last_error expose :only_protected_branches + expose :keep_divergent_refs, if: -> (mirror, _options) do + ::Feature.enabled?(:keep_divergent_refs, mirror.project) + end end end end diff --git a/lib/api/remote_mirrors.rb b/lib/api/remote_mirrors.rb index bdaec67108d0bec1830b6d8210679d9d24e0042f..cb8383ff08a05a4d0c17ed64f5c1700cbdb3d543 100644 --- a/lib/api/remote_mirrors.rb +++ b/lib/api/remote_mirrors.rb @@ -33,9 +33,11 @@ class RemoteMirrors < Grape::API requires :url, type: String, desc: 'The URL for a remote mirror' optional :enabled, type: Boolean, desc: 'Determines if the mirror is enabled' optional :only_protected_branches, type: Boolean, desc: 'Determines if only protected branches are mirrored' + optional :keep_divergent_refs, type: Boolean, desc: 'Determines if divergent refs are kept on the target' end post ':id/remote_mirrors' do create_params = declared_params(include_missing: false) + create_params.delete(:keep_divergent_refs) unless ::Feature.enabled?(:keep_divergent_refs, user_project) new_mirror = user_project.remote_mirrors.create(create_params) @@ -53,12 +55,15 @@ class RemoteMirrors < Grape::API requires :mirror_id, type: String, desc: 'The ID of a remote mirror' optional :enabled, type: Boolean, desc: 'Determines if the mirror is enabled' optional :only_protected_branches, type: Boolean, desc: 'Determines if only protected branches are mirrored' + optional :keep_divergent_refs, type: Boolean, desc: 'Determines if divergent refs are kept on the target' end put ':id/remote_mirrors/:mirror_id' do mirror = user_project.remote_mirrors.find(params[:mirror_id]) mirror_params = declared_params(include_missing: false) mirror_params[:id] = mirror_params.delete(:mirror_id) + mirror_params.delete(:keep_divergent_refs) unless ::Feature.enabled?(:keep_divergent_refs, user_project) + update_params = { remote_mirrors_attributes: mirror_params } result = ::Projects::UpdateService diff --git a/spec/fixtures/api/schemas/remote_mirror.json b/spec/fixtures/api/schemas/remote_mirror.json index 416b0f080d9124622c2b15ae44953c62c70de2f2..87bde189db52f59846f665a97a308eecc1fbc15c 100644 --- a/spec/fixtures/api/schemas/remote_mirror.json +++ b/spec/fixtures/api/schemas/remote_mirror.json @@ -10,7 +10,7 @@ "last_successful_update_at", "last_error", "only_protected_branches" - ], + ], "properties": { "id": { "type": "integer" }, "enabled": { "type": "boolean" }, @@ -20,7 +20,8 @@ "last_update_started_at": { "type": ["string", "null"] }, "last_successful_update_at": { "type": ["string", "null"] }, "last_error": { "type": ["string", "null"] }, - "only_protected_branches": { "type": "boolean" } + "only_protected_branches": { "type": "boolean" }, + "keep_divergent_refs": { "type": ["boolean", "null"] } }, "additionalProperties": false } diff --git a/spec/requests/api/remote_mirrors_spec.rb b/spec/requests/api/remote_mirrors_spec.rb index 2186fe375ac94c8490b821214d03824a496f9a87..5b5188e024cd75e9a1a74f70b0cc0ba0a1e7c2d3 100644 --- a/spec/requests/api/remote_mirrors_spec.rb +++ b/spec/requests/api/remote_mirrors_spec.rb @@ -91,6 +91,10 @@ let(:route) { ->(id) { "/projects/#{project.id}/remote_mirrors/#{id}" } } let(:mirror) { project.remote_mirrors.first } + before do + stub_feature_flags(keep_divergent_refs: false) + end + it 'requires `admin_remote_mirror` permission' do put api(route[mirror.id], developer) @@ -102,12 +106,31 @@ put api(route[mirror.id], user), params: { enabled: '0', - only_protected_branches: 'true' + only_protected_branches: 'true', + keep_divergent_refs: 'true' } expect(response).to have_gitlab_http_status(:success) expect(json_response['enabled']).to eq(false) expect(json_response['only_protected_branches']).to eq(true) + + # Deleted due to lack of feature availability + expect(json_response['keep_divergent_refs']).to be_nil + end + + context 'with the `keep_divergent_refs` feature enabled' do + before do + stub_feature_flags(keep_divergent_refs: { enabled: true, project: project }) + end + + it 'updates the `keep_divergent_refs` attribute' do + project.add_maintainer(user) + + put api(route[mirror.id], user), params: { keep_divergent_refs: 'true' } + + expect(response).to have_gitlab_http_status(:success) + expect(json_response['keep_divergent_refs']).to eq(true) + end end # TODO: Remove flag: https://gitlab.com/gitlab-org/gitlab/issues/38121