From f98abb317b64abc2903016214b14cbc6e1f313fa Mon Sep 17 00:00:00 2001 From: George Koltsov Date: Mon, 8 Nov 2021 17:19:18 +0000 Subject: [PATCH] Add protected branches to Project Migration - Add protected branches to Projet Migration when using Group Migration tool in order to be able to migrate projects' protected branches configurations --- .../protected_branches_pipeline_spec.rb | 37 +++++++++++ .../ee/bulk_imports/projects/stage_spec.rb | 1 + .../pipelines/protected_branches_pipeline.rb | 15 +++++ lib/bulk_imports/projects/stage.rb | 4 ++ .../protected_branches_pipeline_spec.rb | 61 +++++++++++++++++++ spec/lib/bulk_imports/projects/stage_spec.rb | 3 +- 6 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 ee/spec/lib/bulk_imports/projects/pipelines/protected_branches_pipeline_spec.rb create mode 100644 lib/bulk_imports/projects/pipelines/protected_branches_pipeline.rb create mode 100644 spec/lib/bulk_imports/projects/pipelines/protected_branches_pipeline_spec.rb diff --git a/ee/spec/lib/bulk_imports/projects/pipelines/protected_branches_pipeline_spec.rb b/ee/spec/lib/bulk_imports/projects/pipelines/protected_branches_pipeline_spec.rb new file mode 100644 index 000000000000..7016e6abd404 --- /dev/null +++ b/ee/spec/lib/bulk_imports/projects/pipelines/protected_branches_pipeline_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BulkImports::Projects::Pipelines::ProtectedBranchesPipeline do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:bulk_import) { create(:bulk_import, user: user) } + let_it_be(:entity) { create(:bulk_import_entity, :project_entity, project: project, bulk_import: bulk_import) } + let_it_be(:tracker) { create(:bulk_import_tracker, entity: entity) } + let_it_be(:context) { BulkImports::Pipeline::Context.new(tracker) } + let_it_be(:protected_branch) do + { + 'name' => 'main', + 'created_at' => '2016-06-14T15:02:47.967Z', + 'updated_at' => '2016-06-14T15:02:47.967Z', + 'unprotect_access_levels' => [{ 'access_level' => 0 }] + } + end + + subject(:pipeline) { described_class.new(context) } + + describe '#run' do + it 'imports protected branch information with unprotect access levels' do + allow_next_instance_of(BulkImports::Common::Extractors::NdjsonExtractor) do |extractor| + allow(extractor).to receive(:extract).and_return(BulkImports::Pipeline::ExtractedData.new(data: [protected_branch, 0])) + end + + pipeline.run + + imported_protected_branch = project.protected_branches.last + unprotect_access_level = imported_protected_branch.unprotect_access_levels.first + + expect(unprotect_access_level.access_level).to eq(protected_branch['unprotect_access_levels'].first['access_level']) + end + end +end diff --git a/ee/spec/lib/ee/bulk_imports/projects/stage_spec.rb b/ee/spec/lib/ee/bulk_imports/projects/stage_spec.rb index 4a76d1c8e275..4ac4c82bf378 100644 --- a/ee/spec/lib/ee/bulk_imports/projects/stage_spec.rb +++ b/ee/spec/lib/ee/bulk_imports/projects/stage_spec.rb @@ -14,6 +14,7 @@ [4, BulkImports::Projects::Pipelines::MergeRequestsPipeline], [4, BulkImports::Projects::Pipelines::ExternalPullRequestsPipeline], [4, BulkImports::Projects::Pipelines::PushRulePipeline], + [4, BulkImports::Projects::Pipelines::ProtectedBranchesPipeline], [5, BulkImports::Common::Pipelines::WikiPipeline], [5, BulkImports::Common::Pipelines::UploadsPipeline], [6, BulkImports::Common::Pipelines::EntityFinisher] diff --git a/lib/bulk_imports/projects/pipelines/protected_branches_pipeline.rb b/lib/bulk_imports/projects/pipelines/protected_branches_pipeline.rb new file mode 100644 index 000000000000..a570143227d6 --- /dev/null +++ b/lib/bulk_imports/projects/pipelines/protected_branches_pipeline.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module BulkImports + module Projects + module Pipelines + class ProtectedBranchesPipeline + include NdjsonPipeline + + relation_name 'protected_branches' + + extractor ::BulkImports::Common::Extractors::NdjsonExtractor, relation: relation + end + end + end +end diff --git a/lib/bulk_imports/projects/stage.rb b/lib/bulk_imports/projects/stage.rb index d41beceffd7f..9ccc9efff1d9 100644 --- a/lib/bulk_imports/projects/stage.rb +++ b/lib/bulk_imports/projects/stage.rb @@ -39,6 +39,10 @@ def config pipeline: BulkImports::Projects::Pipelines::ExternalPullRequestsPipeline, stage: 4 }, + protected_branches: { + pipeline: BulkImports::Projects::Pipelines::ProtectedBranchesPipeline, + stage: 4 + }, wiki: { pipeline: BulkImports::Common::Pipelines::WikiPipeline, stage: 5 diff --git a/spec/lib/bulk_imports/projects/pipelines/protected_branches_pipeline_spec.rb b/spec/lib/bulk_imports/projects/pipelines/protected_branches_pipeline_spec.rb new file mode 100644 index 000000000000..7de2e2661926 --- /dev/null +++ b/spec/lib/bulk_imports/projects/pipelines/protected_branches_pipeline_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BulkImports::Projects::Pipelines::ProtectedBranchesPipeline do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:bulk_import) { create(:bulk_import, user: user) } + let_it_be(:entity) { create(:bulk_import_entity, :project_entity, project: project, bulk_import: bulk_import) } + let_it_be(:tracker) { create(:bulk_import_tracker, entity: entity) } + let_it_be(:context) { BulkImports::Pipeline::Context.new(tracker) } + let_it_be(:protected_branch) do + { + 'name' => 'main', + 'created_at' => '2016-06-14T15:02:47.967Z', + 'updated_at' => '2016-06-14T15:02:47.967Z', + 'merge_access_levels' => [ + { + 'access_level' => 40, + 'created_at' => '2016-06-15T15:02:47.967Z', + 'updated_at' => '2016-06-15T15:02:47.967Z' + } + ], + 'push_access_levels' => [ + { + 'access_level' => 30, + 'created_at' => '2016-06-16T15:02:47.967Z', + 'updated_at' => '2016-06-16T15:02:47.967Z' + } + ] + } + end + + subject(:pipeline) { described_class.new(context) } + + describe '#run' do + it 'imports protected branch information' do + allow_next_instance_of(BulkImports::Common::Extractors::NdjsonExtractor) do |extractor| + allow(extractor).to receive(:extract).and_return(BulkImports::Pipeline::ExtractedData.new(data: [protected_branch, 0])) + end + + pipeline.run + + imported_protected_branch = project.protected_branches.last + merge_access_level = imported_protected_branch.merge_access_levels.first + push_access_level = imported_protected_branch.push_access_levels.first + + aggregate_failures do + expect(imported_protected_branch.name).to eq(protected_branch['name']) + expect(imported_protected_branch.updated_at).to eq(protected_branch['updated_at']) + expect(imported_protected_branch.created_at).to eq(protected_branch['created_at']) + expect(merge_access_level.access_level).to eq(protected_branch['merge_access_levels'].first['access_level']) + expect(merge_access_level.created_at).to eq(protected_branch['merge_access_levels'].first['created_at']) + expect(merge_access_level.updated_at).to eq(protected_branch['merge_access_levels'].first['updated_at']) + expect(push_access_level.access_level).to eq(protected_branch['push_access_levels'].first['access_level']) + expect(push_access_level.created_at).to eq(protected_branch['push_access_levels'].first['created_at']) + expect(push_access_level.updated_at).to eq(protected_branch['push_access_levels'].first['updated_at']) + end + end + end +end diff --git a/spec/lib/bulk_imports/projects/stage_spec.rb b/spec/lib/bulk_imports/projects/stage_spec.rb index 7c07c4f7b3d3..e7670085f603 100644 --- a/spec/lib/bulk_imports/projects/stage_spec.rb +++ b/spec/lib/bulk_imports/projects/stage_spec.rb @@ -13,6 +13,7 @@ [4, BulkImports::Common::Pipelines::BoardsPipeline], [4, BulkImports::Projects::Pipelines::MergeRequestsPipeline], [4, BulkImports::Projects::Pipelines::ExternalPullRequestsPipeline], + [4, BulkImports::Projects::Pipelines::ProtectedBranchesPipeline], [5, BulkImports::Common::Pipelines::WikiPipeline], [5, BulkImports::Common::Pipelines::UploadsPipeline], [6, BulkImports::Common::Pipelines::EntityFinisher] @@ -27,7 +28,7 @@ describe '#pipelines' do it 'list all the pipelines with their stage number, ordered by stage' do - expect(subject.pipelines & pipelines).to eq(pipelines) + expect(subject.pipelines & pipelines).to contain_exactly(*pipelines) expect(subject.pipelines.last.last).to eq(BulkImports::Common::Pipelines::EntityFinisher) end end