From 70d2555229fe97d14c880bad1ccd3ecedfe737c7 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 25 Feb 2020 15:49:50 -0600 Subject: [PATCH] Modify keep_divergent_refs via RemoteMirror API When the feature is enabled, this attribute can now be modified via the API. --- lib/api/entities/remote_mirror.rb | 3 +++ lib/api/remote_mirrors.rb | 5 ++++ spec/fixtures/api/schemas/remote_mirror.json | 5 ++-- spec/requests/api/remote_mirrors_spec.rb | 25 +++++++++++++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/api/entities/remote_mirror.rb b/lib/api/entities/remote_mirror.rb index dde3e9dea9929c..18d51726baba8c 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 bdaec67108d0be..cb8383ff08a05a 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 416b0f080d9124..87bde189db52f5 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 2186fe375ac94c..5b5188e024cd75 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 -- GitLab