From 5ea225601fd9f6b311c78c169c227b2af23b30b1 Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Wed, 4 Dec 2024 11:44:23 +0100 Subject: [PATCH 01/13] Add backticks and remove FF This adds backticks around @ username mentions and removes the bitbucket_server_convert_mentions_to_users feature flag and related ImportUsersWorker and Stage classes which use the feature. Changelog: added --- .rubocop_todo/rspec/change_by_zero.yml | 1 - app/workers/all_queues.yml | 9 -- .../stage/import_repository_worker.rb | 6 +- .../stage/import_users_worker.rb | 27 ------ ...ucket_server_convert_mentions_to_users.yml | 8 -- config/sidekiq_queues.yml | 2 - .../importers/pull_request_importer.rb | 7 -- .../importers/pull_request_notes_importer.rb | 11 +-- .../importers/pull_request_importer_spec.rb | 14 +-- .../pull_request_notes_importer_spec.rb | 29 ------ spec/workers/every_sidekiq_worker_spec.rb | 1 - .../stage/import_users_worker_spec.rb | 42 --------- .../stage/import_repository_worker_spec.rb | 15 +--- .../stage/import_users_worker_spec.rb | 90 ------------------- 14 files changed, 4 insertions(+), 258 deletions(-) delete mode 100644 app/workers/gitlab/bitbucket_server_import/stage/import_users_worker.rb delete mode 100644 config/feature_flags/development/bitbucket_server_convert_mentions_to_users.yml delete mode 100644 spec/workers/gitlab/bitbucket_import/stage/import_users_worker_spec.rb delete mode 100644 spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb diff --git a/.rubocop_todo/rspec/change_by_zero.yml b/.rubocop_todo/rspec/change_by_zero.yml index 74acf4d504e9f0..c88bc67c044902 100644 --- a/.rubocop_todo/rspec/change_by_zero.yml +++ b/.rubocop_todo/rspec/change_by_zero.yml @@ -117,7 +117,6 @@ RSpec/ChangeByZero: - 'spec/workers/gitlab/bitbucket_import/stage/import_pull_requests_worker_spec.rb' - 'spec/workers/gitlab/bitbucket_server_import/stage/import_pull_requests_worker_spec.rb' - 'spec/workers/gitlab/bitbucket_server_import/stage/import_repository_worker_spec.rb' - - 'spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb' - 'spec/workers/packages/cleanup_package_file_worker_spec.rb' - 'spec/workers/packages/maven/metadata/sync_worker_spec.rb' - 'spec/workers/packages/rubygems/extraction_worker_spec.rb' diff --git a/app/workers/all_queues.yml b/app/workers/all_queues.yml index 9aa1618f3b2a76..09ccf34d685c23 100644 --- a/app/workers/all_queues.yml +++ b/app/workers/all_queues.yml @@ -2748,15 +2748,6 @@ :weight: 1 :idempotent: false :tags: [] -- :name: bitbucket_server_import_stage_import_users - :worker_name: Gitlab::BitbucketServerImport::Stage::ImportUsersWorker - :feature_category: :importers - :has_external_dependencies: true - :urgency: :low - :resource_boundary: :unknown - :weight: 1 - :idempotent: true - :tags: [] - :name: bulk_import :worker_name: BulkImportWorker :feature_category: :importers diff --git a/app/workers/gitlab/bitbucket_server_import/stage/import_repository_worker.rb b/app/workers/gitlab/bitbucket_server_import/stage/import_repository_worker.rb index 573c73cd7dfe66..b378d07d59cd60 100644 --- a/app/workers/gitlab/bitbucket_server_import/stage/import_repository_worker.rb +++ b/app/workers/gitlab/bitbucket_server_import/stage/import_repository_worker.rb @@ -14,11 +14,7 @@ def import(project) importer.execute - if Feature.enabled?(:bitbucket_server_convert_mentions_to_users, project.creator) - ImportUsersWorker.perform_async(project.id) - else - ImportPullRequestsWorker.perform_async(project.id) - end + ImportPullRequestsWorker.perform_async(project.id) end def importer_class diff --git a/app/workers/gitlab/bitbucket_server_import/stage/import_users_worker.rb b/app/workers/gitlab/bitbucket_server_import/stage/import_users_worker.rb deleted file mode 100644 index 4c323a11755687..00000000000000 --- a/app/workers/gitlab/bitbucket_server_import/stage/import_users_worker.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module BitbucketServerImport - module Stage - class ImportUsersWorker - include StageMethods - - idempotent! - - private - - def import(project) - importer = importer_class.new(project) - - importer.execute - - ImportPullRequestsWorker.perform_async(project.id) - end - - def importer_class - Importers::UsersImporter - end - end - end - end -end diff --git a/config/feature_flags/development/bitbucket_server_convert_mentions_to_users.yml b/config/feature_flags/development/bitbucket_server_convert_mentions_to_users.yml deleted file mode 100644 index 90ea8d56c0acca..00000000000000 --- a/config/feature_flags/development/bitbucket_server_convert_mentions_to_users.yml +++ /dev/null @@ -1,8 +0,0 @@ ---- -name: bitbucket_server_convert_mentions_to_users -introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/139097 -rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/434453 -milestone: '16.7' -type: development -group: group::import and integrate -default_enabled: false diff --git a/config/sidekiq_queues.yml b/config/sidekiq_queues.yml index 10e759923ccf14..fd58a83b8ceaa5 100644 --- a/config/sidekiq_queues.yml +++ b/config/sidekiq_queues.yml @@ -141,8 +141,6 @@ - 1 - - bitbucket_server_import_stage_import_repository - 1 -- - bitbucket_server_import_stage_import_users - - 1 - - bulk_import - 1 - - bulk_imports_entity diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb index 9712a8d2551462..e12fe7d842e1af 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb @@ -62,16 +62,9 @@ def description description += author_line description += object[:description] if object[:description] - description = mentions_converter.convert(description) if convert_mentions? - description end - def convert_mentions? - Feature.enabled?(:bitbucket_server_convert_mentions_to_users, project.creator) && - !user_mapping_enabled?(project) - end - def author_line return '' if user_mapping_enabled?(project) || user_finder.uid(object) diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb index 89f46c98fd139d..c7dfb62d2eb791 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb @@ -221,11 +221,7 @@ def pull_request_comment_attributes(comment) note = "*By #{comment.author_username} (#{comment.author_email})*\n\n" end - comment_note = if convert_mentions? - mentions_converter.convert(comment.note) - else - comment.note - end + comment_note = comment.note note += # Provide some context for replying @@ -245,11 +241,6 @@ def pull_request_comment_attributes(comment) } end - def convert_mentions? - Feature.enabled?(:bitbucket_server_convert_mentions_to_users, project.creator) && - !user_mapping_enabled?(project) - end - def author(comment) if user_mapping_enabled?(project) user_finder.uid( diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb index 67f2925cde3330..12ebe342311cdf 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb @@ -164,22 +164,10 @@ ) end - context 'when the `bitbucket_server_convert_mentions_to_users` flag is disabled' do - before do - stub_feature_flags(bitbucket_server_convert_mentions_to_users: false) - end - - it 'does not convert mentions' do - expect_next(Gitlab::Import::MentionsConverter, 'bitbucket_server', project).not_to receive(:convert) - - importer.execute - end - end - context 'when alternate UCM flags are disabled' do before do stub_feature_flags( - bitbucket_server_convert_mentions_to_users: false, + bitbucket_server_user_mapping_by_username: false, bitbucket_server_user_mapping: false ) end diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb index f403d534c7750d..bfc990c79f0136 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb @@ -13,7 +13,6 @@ let_it_be(:pull_request_data) { Gitlab::Json.parse(fixture_file('importers/bitbucket_server/pull_request.json')) } let_it_be(:pull_request) { BitbucketServer::Representation::PullRequest.new(pull_request_data) } let_it_be_with_reload(:merge_request) { create(:merge_request, iid: pull_request.iid, source_project: project) } - let(:mentions_converter) { Gitlab::Import::MentionsConverter.new('bitbucket_server', project) } let(:merge_event) do instance_double( @@ -97,10 +96,6 @@ def expect_log(stage:, message:) .to receive(:info).with(include(import_stage: stage, message: message)) end - before do - allow(Gitlab::Import::MentionsConverter).to receive(:new).and_return(mentions_converter) - end - subject(:importer) { described_class.new(project.reload, pull_request.to_hash) } describe '#execute', :clean_gitlab_redis_shared_state do @@ -223,18 +218,6 @@ def expect_log(stage:, message:) end end - context 'when the `bitbucket_server_convert_mentions_to_users` flag is disabled' do - before do - stub_feature_flags(bitbucket_server_convert_mentions_to_users: false) - end - - it 'does not convert mentions' do - expect(mentions_converter).not_to receive(:convert) - - importer.execute - end - end - it 'logs its progress' do expect_log(stage: 'import_standalone_pr_comments', message: 'starting') expect_log(stage: 'import_standalone_pr_comments', message: 'finished') @@ -376,18 +359,6 @@ def expect_log(stage:, message:) end end - context 'when the `bitbucket_server_convert_mentions_to_users` flag is disabled' do - before do - stub_feature_flags(bitbucket_server_convert_mentions_to_users: false) - end - - it 'does not convert mentions' do - expect(mentions_converter).not_to receive(:convert) - - importer.execute - end - end - it 'logs its progress' do expect_log(stage: 'import_inline_comments', message: 'starting') expect_log(stage: 'import_inline_comments', message: 'finished') diff --git a/spec/workers/every_sidekiq_worker_spec.rb b/spec/workers/every_sidekiq_worker_spec.rb index 192e51e8a47df6..62efc4cc9daa1d 100644 --- a/spec/workers/every_sidekiq_worker_spec.rb +++ b/spec/workers/every_sidekiq_worker_spec.rb @@ -262,7 +262,6 @@ 'Gitlab::BitbucketServerImport::Stage::ImportNotesWorker' => 6, 'Gitlab::BitbucketServerImport::Stage::ImportPullRequestsWorker' => 6, 'Gitlab::BitbucketServerImport::Stage::ImportRepositoryWorker' => 6, - 'Gitlab::BitbucketServerImport::Stage::ImportUsersWorker' => 6, 'Gitlab::GithubImport::AdvanceStageWorker' => 6, 'Gitlab::GithubImport::Attachments::ImportReleaseWorker' => 5, 'Gitlab::GithubImport::Attachments::ImportNoteWorker' => 5, diff --git a/spec/workers/gitlab/bitbucket_import/stage/import_users_worker_spec.rb b/spec/workers/gitlab/bitbucket_import/stage/import_users_worker_spec.rb deleted file mode 100644 index 588fcdc76683f5..00000000000000 --- a/spec/workers/gitlab/bitbucket_import/stage/import_users_worker_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::BitbucketImport::Stage::ImportUsersWorker, feature_category: :importers do - let_it_be(:project) do - create(:project, :import_started, import_url: 'https://bitbucket.org') - end - - subject(:worker) { described_class.new } - - before do - allow_next_instance_of(Gitlab::BitbucketImport::Importers::UsersImporter) do |users_importer| - allow(users_importer).to receive(:execute).and_return(nil) - end - - allow(Gitlab::BitbucketImport::Stage::ImportPullRequestsWorker).to receive(:perform_async).and_return(nil) - end - - it_behaves_like Gitlab::BitbucketImport::StageMethods - - describe '#perform' do - let(:job_args) { [project.id] } - - it_behaves_like 'an idempotent worker' - - it 'executes the UsersImporter' do - expect_next_instance_of(Gitlab::BitbucketImport::Importers::UsersImporter) do |users_importer| - expect(users_importer).to receive(:execute) - end - - worker.perform(job_args) - end - - it 'schedules the next stage' do - expect(Gitlab::BitbucketImport::Stage::ImportPullRequestsWorker).to receive(:perform_async) - .with(project.id) - - worker.perform(job_args) - end - end -end diff --git a/spec/workers/gitlab/bitbucket_server_import/stage/import_repository_worker_spec.rb b/spec/workers/gitlab/bitbucket_server_import/stage/import_repository_worker_spec.rb index 1531b30089c9c2..7ea23041e791b3 100644 --- a/spec/workers/gitlab/bitbucket_server_import/stage/import_repository_worker_spec.rb +++ b/spec/workers/gitlab/bitbucket_server_import/stage/import_repository_worker_spec.rb @@ -18,25 +18,12 @@ end it 'schedules the next stage' do - expect(Gitlab::BitbucketServerImport::Stage::ImportUsersWorker).to receive(:perform_async) + expect(Gitlab::BitbucketServerImport::Stage::ImportPullRequestsWorker).to receive(:perform_async) .with(project.id) worker.perform(project.id) end - context 'when the bitbucket_server_convert_mentions_to_users flag is disabled' do - before do - stub_feature_flags(bitbucket_server_convert_mentions_to_users: false) - end - - it 'skips the user import and schedules the next stage' do - expect(Gitlab::BitbucketServerImport::Stage::ImportPullRequestsWorker).to receive(:perform_async) - .with(project.id) - - worker.perform(project.id) - end - end - it 'logs stage start and finish' do expect(Gitlab::BitbucketServerImport::Logger) .to receive(:info).with(hash_including(message: 'starting stage', project_id: project.id)) diff --git a/spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb b/spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb deleted file mode 100644 index 1141d08729d356..00000000000000 --- a/spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb +++ /dev/null @@ -1,90 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::BitbucketServerImport::Stage::ImportUsersWorker, feature_category: :importers do - let_it_be(:project) do - create(:project, :import_started, - import_data_attributes: { - data: { 'project_key' => 'key', 'repo_slug' => 'slug' }, - credentials: { 'base_uri' => 'http://bitbucket.org/', 'user' => 'bitbucket', 'password' => 'password' } - } - ) - end - - let(:worker) { described_class.new } - - it_behaves_like Gitlab::BitbucketServerImport::StageMethods - - describe '#perform' do - context 'when the import succeeds' do - before do - allow_next_instance_of(Gitlab::BitbucketServerImport::Importers::UsersImporter) do |importer| - allow(importer).to receive(:execute) - end - - allow(Gitlab::BitbucketServerImport::Stage::ImportPullRequestsWorker).to receive(:perform_async).and_return(nil) - end - - it_behaves_like 'an idempotent worker' do - let(:job_args) { [project.id] } - end - - it 'schedules the next stage' do - expect(Gitlab::BitbucketServerImport::Stage::ImportPullRequestsWorker).to receive(:perform_async) - .with(project.id) - - worker.perform(project.id) - end - - it 'logs stage start and finish' do - expect(Gitlab::BitbucketServerImport::Logger) - .to receive(:info).with(hash_including(message: 'starting stage', project_id: project.id)) - expect(Gitlab::BitbucketServerImport::Logger) - .to receive(:info).with(hash_including(message: 'stage finished', project_id: project.id)) - - worker.perform(project.id) - end - end - - context 'when project does not exists' do - it 'does not call importer' do - expect(Gitlab::BitbucketServerImport::Importers::UsersImporter).not_to receive(:new) - - worker.perform(-1) - end - end - - context 'when project import state is not `started`' do - it 'does not call importer' do - project = create(:project, :import_canceled) - - expect(Gitlab::BitbucketServerImport::Importers::UsersImporter).not_to receive(:new) - - worker.perform(project.id) - end - end - - context 'when the importer fails' do - it 'does not schedule the next stage and raises error' do - exception = StandardError.new('Error') - - allow_next_instance_of(Gitlab::BitbucketServerImport::Importers::UsersImporter) do |importer| - allow(importer).to receive(:execute).and_raise(exception) - end - - expect(Gitlab::Import::ImportFailureService) - .to receive(:track).with( - project_id: project.id, - exception: exception, - error_source: described_class.name, - fail_import: false - ).and_call_original - - expect { worker.perform(project.id) } - .to change { Gitlab::BitbucketServerImport::Stage::ImportUsersWorker.jobs.size }.by(0) - .and raise_error(exception) - end - end - end -end -- GitLab From 6bb57df4476077a6ed38db28938b394b488945f3 Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Wed, 4 Dec 2024 19:11:48 +0100 Subject: [PATCH 02/13] Remove use of mentions converter --- .../importers/pull_request_importer.rb | 6 ++--- .../pull_request_notes/base_importer.rb | 4 +-- .../base_note_diff_importer.rb | 3 ++- .../importers/pull_request_notes_importer.rb | 6 +++-- .../pull_request_notes/inline_spec.rb | 27 ++++++------------- .../standalone_notes_spec.rb | 14 +++++----- 6 files changed, 25 insertions(+), 35 deletions(-) diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb index e12fe7d842e1af..5b6f7114ea7b7e 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb @@ -11,8 +11,6 @@ def initialize(project, hash) @project = project @formatter = Gitlab::ImportFormatter.new @user_finder = UserFinder.new(project) - @mentions_converter = Gitlab::Import::MentionsConverter.new('bitbucket_server', project) - # Object should behave as a object so we can remove object.is_a?(Hash) check # This will be fixed in https://gitlab.com/gitlab-org/gitlab/-/issues/412328 @object = hash.with_indifferent_access @@ -55,13 +53,15 @@ def execute private - attr_reader :object, :project, :formatter, :user_finder, :mentions_converter + attr_reader :object, :project, :formatter, :user_finder def description description = '' description += author_line description += object[:description] if object[:description] + # TODO: add backticks to description + description end diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_importer.rb index fc9f1d81b9bf3f..dbb0192a3f8e9a 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_importer.rb @@ -8,6 +8,7 @@ module PullRequestNotes class BaseImporter include Loggable include ::Import::PlaceholderReferences::Pusher + include ::Import::UsernameMentionRewriter # @param project [Project] # @param merge_request [MergeRequest] @@ -15,7 +16,6 @@ def initialize(project, merge_request) @project = project @user_finder = UserFinder.new(project) @formatter = Gitlab::ImportFormatter.new - @mentions_converter = Gitlab::Import::MentionsConverter.new('bitbucket_server', project) @merge_request = merge_request end @@ -25,7 +25,7 @@ def execute(_args) private - attr_reader :project, :user_finder, :merge_request, :mentions_converter + attr_reader :project, :user_finder, :merge_request end end end diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb index 0e73959882f224..c187b04aa204ed 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb @@ -62,7 +62,8 @@ def pull_request_comment_attributes(comment) note = "*By #{comment[:author_username]} (#{comment[:author_email]})*\n\n" end - comment_note = mentions_converter.convert(comment[:note]) + # TODO: add backticks around comment_note + # comment_note = mentions_converter.convert(comment[:note]) note += # Provide some context for replying diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb index c7dfb62d2eb791..bf9d5e012c3b49 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb @@ -7,12 +7,12 @@ class PullRequestNotesImporter include ::Gitlab::Import::MergeRequestHelpers include Loggable include ::Import::PlaceholderReferences::Pusher + include ::Import::UsernameMentionRewriter def initialize(project, hash) @project = project @user_finder = UserFinder.new(project) @formatter = Gitlab::ImportFormatter.new - @mentions_converter = Gitlab::Import::MentionsConverter.new('bitbucket_server', project) @object = hash.with_indifferent_access end @@ -30,7 +30,7 @@ def execute private - attr_reader :object, :project, :formatter, :user_finder, :mentions_converter + attr_reader :object, :project, :formatter, :user_finder def import_notes_in_batch(merge_request) activities = client.activities(project_key, repository_slug, merge_request.iid) @@ -221,6 +221,8 @@ def pull_request_comment_attributes(comment) note = "*By #{comment.author_username} (#{comment.author_email})*\n\n" end + # TODO: add backticks to comment + comment_note = comment.note note += diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb index 467dcc52f86bad..0acf15007ef230 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb @@ -11,7 +11,6 @@ let_it_be(:merge_request) { create(:merge_request, source_project: project) } let_it_be(:now) { Time.now.utc.change(usec: 0) } - let_it_be(:mentions_converter) { Gitlab::Import::MentionsConverter.new('bitbucket_server', project) } let_it_be(:reply) do { author_email: 'reply_author@example.org', @@ -45,10 +44,6 @@ let_it_be(:reply_source_user) { generate_source_user(project, reply[:author_username]) } let_it_be(:note_source_user) { generate_source_user(project, pr_inline_comment[:author_username]) } - before do - allow(Gitlab::Import::MentionsConverter).to receive(:new).and_return(mentions_converter) - end - def expect_log(stage:, message:, iid:, comment_id:) allow(Gitlab::BitbucketServerImport::Logger).to receive(:info).and_call_original expect(Gitlab::BitbucketServerImport::Logger) @@ -69,7 +64,8 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'imports the threaded discussion' do - expect(mentions_converter).to receive(:convert).and_call_original.twice + # TODO test backticks + # expect(mentions_converter).to receive(:convert).and_call_original.twice expect { importer.execute(pr_inline_comment) }.to change { Note.count }.by(2) @@ -111,7 +107,8 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'fallback to basic note' do - expect(mentions_converter).to receive(:convert).and_call_original.twice + # TODO test backticks + # expect(mentions_converter).to receive(:convert).and_call_original.twice expect { importer.execute(invalid_comment) }.to change { Note.count }.by(1) @@ -125,7 +122,8 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'logs its fallback' do - expect(mentions_converter).to receive(:convert).and_call_original.twice + # TODO test backticks + # expect(mentions_converter).to receive(:convert).and_call_original.twice expect_log( stage: 'create_diff_note', message: 'creating standalone fallback for DiffNote', @@ -148,7 +146,8 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'imports the threaded discussion' do - expect(mentions_converter).to receive(:convert).and_call_original.twice + # TODO test backticks + # expect(mentions_converter).to receive(:convert).and_call_original.twice expect { importer.execute(pr_inline_comment) }.to change { Note.count }.by(2) @@ -162,16 +161,6 @@ def expect_log(stage:, message:, iid:, comment_id:) expect(reply_note.author_id).to eq(reply_author.id) end - context 'when converting mention is failed' do - it 'logs its exception' do - expect(mentions_converter).to receive(:convert).and_raise(StandardError) - expect(Gitlab::ErrorTracking).to receive(:log_exception) - .with(StandardError, include(import_stage: 'create_diff_note')) - - importer.execute(pr_inline_comment) - end - end - it 'does not push placeholder references' do importer.execute(pr_inline_comment) diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb index 01fd4843f03886..75db10a0e8f73b 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb @@ -8,7 +8,6 @@ let_it_be(:project) { create(:project, :repository, :bitbucket_server_import, :import_user_mapping_enabled) } let_it_be(:merge_request) { create(:merge_request, source_project: project) } let_it_be(:now) { Time.now.utc.change(usec: 0) } - let_it_be(:mentions_converter) { Gitlab::Import::MentionsConverter.new('bitbucket_server', project) } let_it_be(:author_details) do { author_name: 'John Notes', @@ -30,10 +29,6 @@ let_it_be(:source_user) { generate_source_user(project, pr_comment[:author_username]) } - before do - allow(Gitlab::Import::MentionsConverter).to receive(:new).and_return(mentions_converter) - end - def expect_log(stage:, message:, iid:, comment_id:) allow(Gitlab::BitbucketServerImport::Logger).to receive(:info).and_call_original expect(Gitlab::BitbucketServerImport::Logger) @@ -53,7 +48,8 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'imports the stand alone comments' do - expect(mentions_converter).to receive(:convert).and_call_original + # TODO test backticks + # expect(mentions_converter).to receive(:convert).and_call_original expect { importer.execute(pr_comment) }.to change { Note.count }.by(1) @@ -93,7 +89,8 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'imports multiple comments' do - expect(mentions_converter).to receive(:convert).and_call_original.twice + # TODO test backticks + # expect(mentions_converter).to receive(:convert).and_call_original.twice expect { importer.execute(pr_comment) }.to change { Note.count }.by(2) @@ -158,7 +155,8 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'logs its exception' do - expect(mentions_converter).to receive(:convert).and_call_original + # TODO test backticks + # expect(mentions_converter).to receive(:convert).and_call_original expect(Gitlab::ErrorTracking).to receive(:log_exception) .with(StandardError, include(import_stage: 'import_standalone_notes_comments')) -- GitLab From caac822f2250d1f82b99d4eea695d5dd337b5c21 Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Thu, 5 Dec 2024 15:14:43 +0100 Subject: [PATCH 03/13] Update PR importer and spec --- .../importers/pull_request_importer.rb | 5 ++--- .../importers/pull_request_importer_spec.rb | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb index 5b6f7114ea7b7e..7e2aba7b61645e 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_importer.rb @@ -5,6 +5,7 @@ module BitbucketServerImport module Importers class PullRequestImporter include Loggable + include Gitlab::Import::UsernameMentionRewriter include ::Import::PlaceholderReferences::Pusher def initialize(project, hash) @@ -60,9 +61,7 @@ def description description += author_line description += object[:description] if object[:description] - # TODO: add backticks to description - - description + wrap_mentions_in_backticks(description) end def author_line diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb index 12ebe342311cdf..11c8be27999014 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb @@ -53,6 +53,23 @@ ) end + describe 'when handling @ username mentions' do + let(:original_body) { "I said to @sam_allen.greg the code should follow @bob's advice. @.ali-ce/group#9?" } + let(:expected_body) { "I said to `@sam_allen.greg` the code should follow `@bob`'s advice. `@.ali-ce/group#9`?" } + let(:pull_request_data) do + Gitlab::Json.parse(fixture_file('importers/bitbucket_server/pull_request.json')) + .merge({ "description" => original_body }) + end + + it 'inserts backticks around mentions' do + importer.execute + + merge_request = project.merge_requests.find_by_iid(pull_request.iid) + + expect(merge_request.description).to eq(expected_body) + end + end + describe 'refs/merge-requests/:iid/head creation' do before do project.repository.create_branch(pull_request.source_branch_name, 'master') -- GitLab From d67f440d7a2e8a3d7715cdd0ba66153101f65dac Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Thu, 5 Dec 2024 15:55:33 +0100 Subject: [PATCH 04/13] Update pr notes importer and spec --- .../importers/pull_request_notes_importer.rb | 9 ++-- .../pull_request_notes_importer_spec.rb | 52 +++++++++++++++++-- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb index bf9d5e012c3b49..3570630050aade 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb @@ -5,9 +5,9 @@ module BitbucketServerImport module Importers class PullRequestNotesImporter include ::Gitlab::Import::MergeRequestHelpers + include ::Gitlab::Import::UsernameMentionRewriter include Loggable include ::Import::PlaceholderReferences::Pusher - include ::Import::UsernameMentionRewriter def initialize(project, hash) @project = project @@ -221,14 +221,13 @@ def pull_request_comment_attributes(comment) note = "*By #{comment.author_username} (#{comment.author_email})*\n\n" end - # TODO: add backticks to comment - - comment_note = comment.note + comment_note = wrap_mentions_in_backticks(comment.note) note += # Provide some context for replying if comment.parent_comment - "> #{comment.parent_comment.note.truncate(80)}\n\n#{comment_note}" + parent_comment_note = wrap_mentions_in_backticks(comment.parent_comment.note) + "> #{parent_comment_note.truncate(80)}\n\n#{comment_note}" else comment_note end diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb index bfc990c79f0136..046bf7e2f88900 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb @@ -162,7 +162,51 @@ def expect_log(stage:, message:) ) end - context 'when the note has a parent note' do + context 'when the note has @ mentions' do + let(:original_note_text) { "I said to @sam_allen.greg follow @bob's advice. @.ali-ce/group#9?" } + let(:expected_note_text) { "I said to `@sam_allen.greg` follow `@bob`'s advice. `@.ali-ce/group#9`?" } + let(:original_reply_text) { "@bachhus I don't agree. See @ali's evidence cc @.ali-ce/group#9?" } + let(:expected_reply_text) { "`@bachhus` I don't agree. See `@ali`'s evidence cc `@.ali-ce/group#9`?" } + + let(:pr_note) do + instance_double( + BitbucketServer::Representation::Comment, + id: 456, + note: original_note_text, + author_name: 'Note Author', + author_email: 'note_author@example.org', + author_username: 'note_author', + comments: [pr_note_reply], + created_at: now, + updated_at: now, + parent_comment: pr_parent_note) + end + + let(:pr_note_reply) do + instance_double( + BitbucketServer::Representation::Comment, + note: original_reply_text, + author_name: 'Note Author', + author_email: 'note_author@example.org', + author_username: 'note_author', + comments: [], + created_at: now, + updated_at: now, + parent_comment: nil) + end + + it 'inserts backticks around the mentions' do + importer.execute + + expect(Note.first.note).to eq(expected_note_text) + expect(Note.last.note).to eq(expected_reply_text) + end + end + + context 'when the note has a parent note with @ mentions' do + let(:original_parent_text) { "Attention: @ali has worked on this. @fred's work from @.ali-ce/group#9?" } + let(:expected_parent_text) { "Attention: `@ali` has worked on this. `@fred`'s work from `@.ali-ce/group#9`?" } + let(:pr_note) do instance_double( BitbucketServer::Representation::Comment, @@ -180,7 +224,7 @@ def expect_log(stage:, message:) let(:pr_parent_note) do instance_double( BitbucketServer::Representation::Comment, - note: 'Parent note', + note: original_parent_text, author_name: 'Note Author', author_email: 'note_author@example.org', author_username: 'note_author', @@ -191,10 +235,10 @@ def expect_log(stage:, message:) ) end - it 'adds the parent note before the actual note' do + it 'adds the parent note before the actual note with backticks inserted' do importer.execute - expect(Note.first.note).to include("> #{pr_parent_note.note}\n\n") + expect(Note.first.note).to include("> #{expected_parent_text}\n\n") end end -- GitLab From 90cac5c956ef8544b18da6eac28c53396b9a915a Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Thu, 5 Dec 2024 16:58:35 +0100 Subject: [PATCH 05/13] Update base diff note importer & spec --- .../importers/pull_request_notes/base_importer.rb | 2 +- .../pull_request_notes/base_note_diff_importer.rb | 8 ++++---- .../importers/pull_request_notes_importer_spec.rb | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_importer.rb index dbb0192a3f8e9a..0d7c00569656b4 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_importer.rb @@ -7,8 +7,8 @@ module PullRequestNotes # Base class for importing pull request notes during project import from Bitbucket Server class BaseImporter include Loggable + include ::Gitlab::Import::UsernameMentionRewriter include ::Import::PlaceholderReferences::Pusher - include ::Import::UsernameMentionRewriter # @param project [Project] # @param merge_request [MergeRequest] diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb index c187b04aa204ed..e4d4c6ef6210e7 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb @@ -62,13 +62,13 @@ def pull_request_comment_attributes(comment) note = "*By #{comment[:author_username]} (#{comment[:author_email]})*\n\n" end - # TODO: add backticks around comment_note - # comment_note = mentions_converter.convert(comment[:note]) + comment_note = wrap_mentions_in_backticks(comment[:note]) note += # Provide some context for replying if comment[:parent_comment_note] - "> #{comment[:parent_comment_note].truncate(PARENT_COMMENT_CONTEXT_LENGTH)}\n\n#{comment_note}" + parent_comment_note = wrap_mentions_in_backticks(comment[:parent_comment_note]) + "> #{parent_comment_note.truncate(PARENT_COMMENT_CONTEXT_LENGTH)}\n\n#{comment_note}" else comment_note end @@ -99,7 +99,7 @@ def create_basic_fallback_note(merge_request, comment, position) note_text += " #{position.old_path}:#{position.old_line} -->" if position.old_line note_text += " #{position.new_path}:#{position.new_line}" if position.new_line - note_text += "*\n\n#{comment[:note]}" + note_text += "*\n\n#{wrap_mentions_in_backticks(comment[:note])}" attributes[:note] = note_text diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb index 046bf7e2f88900..8341f5f3a11712 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb @@ -179,7 +179,7 @@ def expect_log(stage:, message:) comments: [pr_note_reply], created_at: now, updated_at: now, - parent_comment: pr_parent_note) + parent_comment: nil) end let(:pr_note_reply) do -- GitLab From e15c6fc0cf67b58666747525e3f3f5419ca3c7c0 Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Tue, 10 Dec 2024 16:42:31 +0100 Subject: [PATCH 06/13] Apply MR suggestions --- .../importers/users_importer.rb | 70 ----------------- .../importers/pull_request_importer_spec.rb | 1 - .../pull_request_notes/inline_spec.rb | 48 +++++++++++- .../importers/users_importer_spec.rb | 78 ------------------- 4 files changed, 45 insertions(+), 152 deletions(-) delete mode 100644 lib/gitlab/bitbucket_server_import/importers/users_importer.rb delete mode 100644 spec/lib/gitlab/bitbucket_server_import/importers/users_importer_spec.rb diff --git a/lib/gitlab/bitbucket_server_import/importers/users_importer.rb b/lib/gitlab/bitbucket_server_import/importers/users_importer.rb deleted file mode 100644 index 588c7be5dd4bd6..00000000000000 --- a/lib/gitlab/bitbucket_server_import/importers/users_importer.rb +++ /dev/null @@ -1,70 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module BitbucketServerImport - module Importers - class UsersImporter - include Loggable - include Gitlab::Import::UserFromMention - - BATCH_SIZE = 100 - - def initialize(project) - @project = project - @project_id = project.id - end - - attr_reader :project, :project_id - - def execute - log_info(import_stage: 'import_users', message: 'starting') - - current = page_counter.current - - loop do - log_info( - import_stage: 'import_users', - message: "importing page #{current} using batch size #{BATCH_SIZE}" - ) - - users = client.users(project_key, page_offset: current, limit: BATCH_SIZE).to_a - - break if users.empty? - - cache_users(users) - - current += 1 - page_counter.set(current) - end - - page_counter.expire! - - log_info(import_stage: 'import_users', message: 'finished') - end - - private - - def cache_users(users) - users_hash = users.each_with_object({}) do |user, hash| - cache_key = source_user_cache_key('bitbucket_server', project_id, user.username) - hash[cache_key] = source_user_cache_value(user.email, type: :email) - end - - cache_multiple(users_hash) - end - - def client - @client ||= BitbucketServer::Client.new(project.import_data.credentials) - end - - def project_key - project.import_data.data['project_key'] - end - - def page_counter - @page_counter ||= Gitlab::Import::PageCounter.new(project, :users, 'bitbucket-server-importer') - end - end - end - end -end diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb index 11c8be27999014..01ac292c7501af 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb @@ -184,7 +184,6 @@ context 'when alternate UCM flags are disabled' do before do stub_feature_flags( - bitbucket_server_user_mapping_by_username: false, bitbucket_server_user_mapping: false ) end diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb index 0acf15007ef230..3f612dac69f79a 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb @@ -64,9 +64,6 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'imports the threaded discussion' do - # TODO test backticks - # expect(mentions_converter).to receive(:convert).and_call_original.twice - expect { importer.execute(pr_inline_comment) }.to change { Note.count }.by(2) expect(merge_request.discussions.count).to eq(1) @@ -97,6 +94,51 @@ def expect_log(stage:, message:, iid:, comment_id:) importer.execute(pr_inline_comment) end + context 'when note has @ username mentions' do + let(:original_text) { "Attention: @ali has worked on this. @fred's work from @.ali-ce/group#9?" } + let(:expected_text) { "Attention: `@ali` has worked on this. `@fred`'s work from `@.ali-ce/group#9`?" } + let(:mention_reply) do + { + author_email: 'reply_author@example.org', + author_username: 'reply_author', + note: original_text, + created_at: now, + updated_at: now, + parent_comment_note: nil + } + end + + let(:mention_comment) do + { + id: 7, + file_type: 'ADDED', + from_sha: 'c5f4288162e2e6218180779c7f6ac1735bb56eab', + to_sha: 'a4c2164330f2549f67c13f36a93884cf66e976be', + file_path: '.gitmodules', + old_pos: nil, + new_pos: 4, + note: original_text, + author_email: 'inline_note_author@example.org', + author_username: 'inline_note_author', + comments: [mention_reply], + created_at: now, + updated_at: now, + parent_comment_note: nil + } + end + + it 'inserts backticks around the mentions' do + importer.execute(mention_comment) + + notes = merge_request.notes.order(:id).to_a + start_note = notes.first + expect(start_note.note).to end_with(expected_text) + + reply_note = notes.last + expect(reply_note.note).to eq(expected_text) + end + end + context 'when note is invalid' do let(:invalid_comment) do pr_inline_comment.merge( diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/users_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/users_importer_spec.rb deleted file mode 100644 index 5519a829daab18..00000000000000 --- a/spec/lib/gitlab/bitbucket_server_import/importers/users_importer_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::BitbucketServerImport::Importers::UsersImporter, feature_category: :importers do - let(:logger) { Gitlab::BitbucketServerImport::Logger } - - let_it_be(:project) do - create(:project, :with_import_url, :import_started, :empty_repo, - import_data_attributes: { - data: { 'project_key' => 'key', 'repo_slug' => 'slug' }, - credentials: { 'base_uri' => 'http://bitbucket.org/', 'user' => 'bitbucket', 'password' => 'password' } - } - ) - end - - let(:user_1) do - BitbucketServer::Representation::User.new( - { 'user' => { 'emailAddress' => 'email1', 'slug' => 'username1' } } - ) - end - - let(:user_2) do - BitbucketServer::Representation::User.new( - { 'user' => { 'emailAddress' => 'email2', 'slug' => 'username2' } } - ) - end - - let(:user_3) do - BitbucketServer::Representation::User.new( - { 'user' => { 'emailAddress' => 'email3', 'slug' => 'username3' } } - ) - end - - before do - stub_const("#{described_class}::BATCH_SIZE", 2) - - allow_next_instance_of(BitbucketServer::Client) do |client| - allow(client).to receive(:users).with('key', limit: 2, page_offset: 1).and_return([user_1, user_2]) - allow(client).to receive(:users).with('key', limit: 2, page_offset: 2).and_return([user_3]) - allow(client).to receive(:users).with('key', limit: 2, page_offset: 3).and_return([]) - end - end - - subject(:importer) { described_class.new(project) } - - describe '#execute' do - it 'writes the username and email to cache for every user in batches' do - expect(logger).to receive(:info).with(hash_including(message: 'starting')) - expect(logger).to receive(:info).with(hash_including(message: 'importing page 1 using batch size 2')) - expect(logger).to receive(:info).with(hash_including(message: 'importing page 2 using batch size 2')) - expect(logger).to receive(:info).with(hash_including(message: 'importing page 3 using batch size 2')) - expect(logger).to receive(:info).with(hash_including(message: 'finished')) - - expect_next_instance_of(Gitlab::Import::PageCounter) do |page_counter| - expect(page_counter).to receive(:current).and_call_original.once - expect(page_counter).to receive(:set).with(2).and_call_original.once - expect(page_counter).to receive(:set).with(3).and_call_original.once - expect(page_counter).to receive(:expire!).and_call_original.once - end - - expect(Gitlab::Cache::Import::Caching).to receive(:write_multiple).and_call_original.twice - - importer.execute - - cache_key_prefix = "bitbucket_server/project/#{project.id}/source" - expect(Gitlab::Cache::Import::Caching.read("#{cache_key_prefix}/username1")).to eq( - { value: 'email1', type: :email }.to_json - ) - expect(Gitlab::Cache::Import::Caching.read("#{cache_key_prefix}/username2")).to eq( - { value: 'email2', type: :email }.to_json - ) - expect(Gitlab::Cache::Import::Caching.read("#{cache_key_prefix}/username3")).to eq( - { value: 'email3', type: :email }.to_json - ) - end - end -end -- GitLab From 7ebf41bce7a5dbdce6be6d06c8b5a6556f94b0d9 Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Wed, 11 Dec 2024 15:45:38 +0100 Subject: [PATCH 07/13] Make worker no-op, update specs --- app/workers/all_queues.yml | 9 ++++ .../stage/import_users_worker.rb | 21 +++++++++ config/sidekiq_queues.yml | 2 + .../pull_request_notes/inline_spec.rb | 8 ---- .../standalone_notes_spec.rb | 43 +++++++++++++++---- spec/workers/every_sidekiq_worker_spec.rb | 1 + .../stage/import_users_worker_spec.rb | 42 ++++++++++++++++++ .../stage/import_users_worker_spec.rb | 24 +++++++++++ 8 files changed, 134 insertions(+), 16 deletions(-) create mode 100644 app/workers/gitlab/bitbucket_server_import/stage/import_users_worker.rb create mode 100644 spec/workers/gitlab/bitbucket_import/stage/import_users_worker_spec.rb create mode 100644 spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb diff --git a/app/workers/all_queues.yml b/app/workers/all_queues.yml index 09ccf34d685c23..9aa1618f3b2a76 100644 --- a/app/workers/all_queues.yml +++ b/app/workers/all_queues.yml @@ -2748,6 +2748,15 @@ :weight: 1 :idempotent: false :tags: [] +- :name: bitbucket_server_import_stage_import_users + :worker_name: Gitlab::BitbucketServerImport::Stage::ImportUsersWorker + :feature_category: :importers + :has_external_dependencies: true + :urgency: :low + :resource_boundary: :unknown + :weight: 1 + :idempotent: true + :tags: [] - :name: bulk_import :worker_name: BulkImportWorker :feature_category: :importers diff --git a/app/workers/gitlab/bitbucket_server_import/stage/import_users_worker.rb b/app/workers/gitlab/bitbucket_server_import/stage/import_users_worker.rb new file mode 100644 index 00000000000000..7d2602283e3c9d --- /dev/null +++ b/app/workers/gitlab/bitbucket_server_import/stage/import_users_worker.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Gitlab + module BitbucketServerImport + module Stage + class ImportUsersWorker + include StageMethods + + idempotent! + + private + + def import(project); end + + def importer_class + Importers::UsersImporter + end + end + end + end +end diff --git a/config/sidekiq_queues.yml b/config/sidekiq_queues.yml index fd58a83b8ceaa5..10e759923ccf14 100644 --- a/config/sidekiq_queues.yml +++ b/config/sidekiq_queues.yml @@ -141,6 +141,8 @@ - 1 - - bitbucket_server_import_stage_import_repository - 1 +- - bitbucket_server_import_stage_import_users + - 1 - - bulk_import - 1 - - bulk_imports_entity diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb index 3f612dac69f79a..89a2b045682c27 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/inline_spec.rb @@ -149,9 +149,6 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'fallback to basic note' do - # TODO test backticks - # expect(mentions_converter).to receive(:convert).and_call_original.twice - expect { importer.execute(invalid_comment) }.to change { Note.count }.by(1) expect(merge_request.discussions.count).to eq(1) @@ -164,8 +161,6 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'logs its fallback' do - # TODO test backticks - # expect(mentions_converter).to receive(:convert).and_call_original.twice expect_log( stage: 'create_diff_note', message: 'creating standalone fallback for DiffNote', @@ -188,9 +183,6 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'imports the threaded discussion' do - # TODO test backticks - # expect(mentions_converter).to receive(:convert).and_call_original.twice - expect { importer.execute(pr_inline_comment) }.to change { Note.count }.by(2) expect(merge_request.discussions.count).to eq(1) diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb index 75db10a0e8f73b..57421fab9abcd1 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb @@ -48,9 +48,6 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'imports the stand alone comments' do - # TODO test backticks - # expect(mentions_converter).to receive(:convert).and_call_original - expect { importer.execute(pr_comment) }.to change { Note.count }.by(1) expect(merge_request.notes.count).to eq(1) @@ -89,9 +86,6 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'imports multiple comments' do - # TODO test backticks - # expect(mentions_converter).to receive(:convert).and_call_original.twice - expect { importer.execute(pr_comment) }.to change { Note.count }.by(2) expect(merge_request.notes.count).to eq(2) @@ -112,6 +106,41 @@ def expect_log(stage:, message:, iid:, comment_id:) end end + context 'when note has @ username mentions' do + let(:original_text) { "Attention: @ali has worked on this. @fred's work from @.ali-ce/group#9?" } + let(:expected_text) { "Attention: `@ali` has worked on this. `@fred`'s work from `@.ali-ce/group#9`?" } + let(:mention_extra_comment) do + { + id: 6, + note: original_text, + comments: [], + created_at: now, + updated_at: now, + parent_comment_note: nil, + imported_from: 'bitbucket_server' + }.merge(author_details) + end + + let(:mention_comment) do + { + id: 5, + note: original_text, + comments: [mention_extra_comment], + created_at: now, + updated_at: now, + parent_comment_note: nil, + imported_from: 'bitbucket_server' + }.merge(author_details) + end + + it 'inserts backticks around the mentions' do + importer.execute(mention_comment) + + expect(merge_request.notes.first.note).to eq(expected_text) + expect(merge_request.notes.last.note).to eq(expected_text) + end + end + context 'when the note has a parent note' do let(:pr_comment) do { @@ -155,8 +184,6 @@ def expect_log(stage:, message:, iid:, comment_id:) end it 'logs its exception' do - # TODO test backticks - # expect(mentions_converter).to receive(:convert).and_call_original expect(Gitlab::ErrorTracking).to receive(:log_exception) .with(StandardError, include(import_stage: 'import_standalone_notes_comments')) diff --git a/spec/workers/every_sidekiq_worker_spec.rb b/spec/workers/every_sidekiq_worker_spec.rb index 62efc4cc9daa1d..192e51e8a47df6 100644 --- a/spec/workers/every_sidekiq_worker_spec.rb +++ b/spec/workers/every_sidekiq_worker_spec.rb @@ -262,6 +262,7 @@ 'Gitlab::BitbucketServerImport::Stage::ImportNotesWorker' => 6, 'Gitlab::BitbucketServerImport::Stage::ImportPullRequestsWorker' => 6, 'Gitlab::BitbucketServerImport::Stage::ImportRepositoryWorker' => 6, + 'Gitlab::BitbucketServerImport::Stage::ImportUsersWorker' => 6, 'Gitlab::GithubImport::AdvanceStageWorker' => 6, 'Gitlab::GithubImport::Attachments::ImportReleaseWorker' => 5, 'Gitlab::GithubImport::Attachments::ImportNoteWorker' => 5, diff --git a/spec/workers/gitlab/bitbucket_import/stage/import_users_worker_spec.rb b/spec/workers/gitlab/bitbucket_import/stage/import_users_worker_spec.rb new file mode 100644 index 00000000000000..588fcdc76683f5 --- /dev/null +++ b/spec/workers/gitlab/bitbucket_import/stage/import_users_worker_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BitbucketImport::Stage::ImportUsersWorker, feature_category: :importers do + let_it_be(:project) do + create(:project, :import_started, import_url: 'https://bitbucket.org') + end + + subject(:worker) { described_class.new } + + before do + allow_next_instance_of(Gitlab::BitbucketImport::Importers::UsersImporter) do |users_importer| + allow(users_importer).to receive(:execute).and_return(nil) + end + + allow(Gitlab::BitbucketImport::Stage::ImportPullRequestsWorker).to receive(:perform_async).and_return(nil) + end + + it_behaves_like Gitlab::BitbucketImport::StageMethods + + describe '#perform' do + let(:job_args) { [project.id] } + + it_behaves_like 'an idempotent worker' + + it 'executes the UsersImporter' do + expect_next_instance_of(Gitlab::BitbucketImport::Importers::UsersImporter) do |users_importer| + expect(users_importer).to receive(:execute) + end + + worker.perform(job_args) + end + + it 'schedules the next stage' do + expect(Gitlab::BitbucketImport::Stage::ImportPullRequestsWorker).to receive(:perform_async) + .with(project.id) + + worker.perform(job_args) + end + end +end diff --git a/spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb b/spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb new file mode 100644 index 00000000000000..16cbaa95a592ee --- /dev/null +++ b/spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BitbucketServerImport::Stage::ImportUsersWorker, feature_category: :importers do + let_it_be(:project) do + create(:project, :import_started, + import_data_attributes: { + data: { 'project_key' => 'key', 'repo_slug' => 'slug' }, + credentials: { 'base_uri' => 'http://bitbucket.org/', 'user' => 'bitbucket', 'password' => 'password' } + } + ) + end + + let(:worker) { described_class.new } + + describe '#perform' do + it 'returns without calling the next import stage' do # no-oped. Will be removed in follow up https://gitlab.com/gitlab-org/gitlab/-/issues/508916 + result = worker.perform(project.id) + expect(::Gitlab::BitbucketServerImport::Stage::ImportUsersWorker).not_to receive(:perform_async) + expect(result).to be(true) + end + end +end -- GitLab From 6df8d5efe19d7cc16e58bc87a8ddc9c61626490a Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Wed, 11 Dec 2024 19:04:56 +0100 Subject: [PATCH 08/13] Update truncation for backticks --- .../pull_request_notes/base_note_diff_importer.rb | 7 ++++--- .../importers/pull_request_notes_importer.rb | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb index e4d4c6ef6210e7..39955e7bf939ec 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb @@ -5,7 +5,7 @@ module BitbucketServerImport module Importers module PullRequestNotes class BaseNoteDiffImporter < BaseImporter - PARENT_COMMENT_CONTEXT_LENGTH = 80 + PARENT_COMMENT_CONTEXT_LENGTH = 78 def build_position(merge_request, pr_comment) params = { @@ -67,8 +67,9 @@ def pull_request_comment_attributes(comment) note += # Provide some context for replying if comment[:parent_comment_note] - parent_comment_note = wrap_mentions_in_backticks(comment[:parent_comment_note]) - "> #{parent_comment_note.truncate(PARENT_COMMENT_CONTEXT_LENGTH)}\n\n#{comment_note}" + parent_comment_note = comment[:parent_comment_note] + truncated_note = "#{parent_comment_note.truncate(PARENT_COMMENT_CONTEXT_LENGTH, omission: ' ')}... " + "> #{wrap_mentions_in_backticks(truncated_note)}\n\n#{comment_note}" else comment_note end diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb index 3570630050aade..c705611cc5c201 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb @@ -226,8 +226,9 @@ def pull_request_comment_attributes(comment) note += # Provide some context for replying if comment.parent_comment - parent_comment_note = wrap_mentions_in_backticks(comment.parent_comment.note) - "> #{parent_comment_note.truncate(80)}\n\n#{comment_note}" + parent_comment_note = comment.parent_comment.note + truncated_note = "#{parent_comment_note.truncate(78, omission: ' ')}..." + "> #{wrap_mentions_in_backticks(truncated_note)}\n\n#{comment_note}" else comment_note end -- GitLab From ac348a84e5c600d3b7c960eafbf24233ed8dea3e Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Thu, 12 Dec 2024 11:58:00 +0100 Subject: [PATCH 09/13] Fix spec failures --- .../importers/pull_request_importer_spec.rb | 5 ++++- .../importers/pull_request_notes/standalone_notes_spec.rb | 2 +- .../importers/pull_request_notes_importer_spec.rb | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb index 01ac292c7501af..5fbd72c0ce6439 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb @@ -55,7 +55,10 @@ describe 'when handling @ username mentions' do let(:original_body) { "I said to @sam_allen.greg the code should follow @bob's advice. @.ali-ce/group#9?" } - let(:expected_body) { "I said to `@sam_allen.greg` the code should follow `@bob`'s advice. `@.ali-ce/group#9`?" } + let(:expected_body) do + "I said to `@sam_allen.greg` the code should follow `@bob`'s advice. `@.ali-ce/group#9`?..." + end + let(:pull_request_data) do Gitlab::Json.parse(fixture_file('importers/bitbucket_server/pull_request.json')) .merge({ "description" => original_body }) diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb index 57421fab9abcd1..0a923acb4b56ec 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb @@ -157,7 +157,7 @@ def expect_log(stage:, message:, iid:, comment_id:) it 'adds the parent note before the actual note' do importer.execute(pr_comment) - expect(Note.first.note).to include("> #{pr_comment[:parent_comment_note]}\n\n") + expect(Note.first.note).to include("> #{pr_comment[:parent_comment_note]}... \n\n") end it 'logs its progress' do diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb index 8341f5f3a11712..c6b85f0d42aa30 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb @@ -205,7 +205,9 @@ def expect_log(stage:, message:) context 'when the note has a parent note with @ mentions' do let(:original_parent_text) { "Attention: @ali has worked on this. @fred's work from @.ali-ce/group#9?" } - let(:expected_parent_text) { "Attention: `@ali` has worked on this. `@fred`'s work from `@.ali-ce/group#9`?" } + let(:expected_parent_text) do + "Attention: `@ali` has worked on this. `@fred`'s work from `@.ali-ce/group#9`?..." + end let(:pr_note) do instance_double( -- GitLab From 246006688799e80b8dea75fa727b3112f462ce4f Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Thu, 12 Dec 2024 12:08:25 +0100 Subject: [PATCH 10/13] Apply MR comment --- .../importers/pull_request_notes/base_note_diff_importer.rb | 2 +- .../importers/pull_request_notes_importer.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb index 39955e7bf939ec..9f0db2796bde1b 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb @@ -5,7 +5,7 @@ module BitbucketServerImport module Importers module PullRequestNotes class BaseNoteDiffImporter < BaseImporter - PARENT_COMMENT_CONTEXT_LENGTH = 78 + PARENT_COMMENT_CONTEXT_LENGTH = 80 def build_position(merge_request, pr_comment) params = { diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb index c705611cc5c201..846f77ec188aa6 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb @@ -227,7 +227,7 @@ def pull_request_comment_attributes(comment) # Provide some context for replying if comment.parent_comment parent_comment_note = comment.parent_comment.note - truncated_note = "#{parent_comment_note.truncate(78, omission: ' ')}..." + truncated_note = "#{parent_comment_note.truncate(80, omission: ' ')}..." "> #{wrap_mentions_in_backticks(truncated_note)}\n\n#{comment_note}" else comment_note -- GitLab From af0dd67f7374ae7e2f1ad93ce163f2d17e0bfef8 Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Thu, 12 Dec 2024 12:28:50 +0100 Subject: [PATCH 11/13] Only truncate when truncation needed --- .../pull_request_notes/base_note_diff_importer.rb | 7 ++++++- .../importers/pull_request_notes_importer.rb | 7 ++++++- .../importers/pull_request_importer_spec.rb | 2 +- .../importers/pull_request_notes/standalone_notes_spec.rb | 2 +- .../importers/pull_request_notes_importer_spec.rb | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb index 9f0db2796bde1b..6570eac131d499 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb @@ -68,7 +68,12 @@ def pull_request_comment_attributes(comment) # Provide some context for replying if comment[:parent_comment_note] parent_comment_note = comment[:parent_comment_note] - truncated_note = "#{parent_comment_note.truncate(PARENT_COMMENT_CONTEXT_LENGTH, omission: ' ')}... " + truncated_note = if parent_comment_note.length > 80 + "#{parent_comment_note.truncate(80, omission: ' ')}..." + else + parent_comment_note + end + "> #{wrap_mentions_in_backticks(truncated_note)}\n\n#{comment_note}" else comment_note diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb index 846f77ec188aa6..fb9194f84ec34f 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb @@ -227,7 +227,12 @@ def pull_request_comment_attributes(comment) # Provide some context for replying if comment.parent_comment parent_comment_note = comment.parent_comment.note - truncated_note = "#{parent_comment_note.truncate(80, omission: ' ')}..." + truncated_note = if parent_comment_note.length > 80 + "#{parent_comment_note.truncate(80, omission: ' ')}..." + else + parent_comment_note + end + "> #{wrap_mentions_in_backticks(truncated_note)}\n\n#{comment_note}" else comment_note diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb index 5fbd72c0ce6439..e4ad27086df21a 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_importer_spec.rb @@ -56,7 +56,7 @@ describe 'when handling @ username mentions' do let(:original_body) { "I said to @sam_allen.greg the code should follow @bob's advice. @.ali-ce/group#9?" } let(:expected_body) do - "I said to `@sam_allen.greg` the code should follow `@bob`'s advice. `@.ali-ce/group#9`?..." + "I said to `@sam_allen.greg` the code should follow `@bob`'s advice. `@.ali-ce/group#9`?" end let(:pull_request_data) do diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb index 0a923acb4b56ec..57421fab9abcd1 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/standalone_notes_spec.rb @@ -157,7 +157,7 @@ def expect_log(stage:, message:, iid:, comment_id:) it 'adds the parent note before the actual note' do importer.execute(pr_comment) - expect(Note.first.note).to include("> #{pr_comment[:parent_comment_note]}... \n\n") + expect(Note.first.note).to include("> #{pr_comment[:parent_comment_note]}\n\n") end it 'logs its progress' do diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb index c6b85f0d42aa30..9fdd44a32005b0 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb @@ -206,7 +206,7 @@ def expect_log(stage:, message:) context 'when the note has a parent note with @ mentions' do let(:original_parent_text) { "Attention: @ali has worked on this. @fred's work from @.ali-ce/group#9?" } let(:expected_parent_text) do - "Attention: `@ali` has worked on this. `@fred`'s work from `@.ali-ce/group#9`?..." + "Attention: `@ali` has worked on this. `@fred`'s work from `@.ali-ce/group#9`?" end let(:pr_note) do -- GitLab From aebe6d46b9c650fa863f0ac3f7fd414f62984e38 Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Thu, 12 Dec 2024 15:56:35 +0100 Subject: [PATCH 12/13] Improve truncation blocks --- .../base_note_diff_importer.rb | 17 +++++------------ .../importers/pull_request_notes_importer.rb | 15 +++++---------- .../pull_request_notes_importer_spec.rb | 9 ++++++--- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb index 6570eac131d499..1cffe9f8998dc1 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb @@ -62,26 +62,19 @@ def pull_request_comment_attributes(comment) note = "*By #{comment[:author_username]} (#{comment[:author_email]})*\n\n" end - comment_note = wrap_mentions_in_backticks(comment[:note]) - note += # Provide some context for replying if comment[:parent_comment_note] - parent_comment_note = comment[:parent_comment_note] - truncated_note = if parent_comment_note.length > 80 - "#{parent_comment_note.truncate(80, omission: ' ')}..." - else - parent_comment_note - end - - "> #{wrap_mentions_in_backticks(truncated_note)}\n\n#{comment_note}" + parent_comment_note = comment[:parent_comment_note].truncate(80, omission: ' ...') + + "> #{parent_comment_note}\n\n#{comment[:note]}" else - comment_note + comment[:note] end { project: project, - note: note, + note: wrap_mentions_in_backticks(note), author_id: author, created_at: comment[:created_at], updated_at: comment[:updated_at] diff --git a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb index fb9194f84ec34f..7f470e17f8d883 100644 --- a/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb +++ b/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer.rb @@ -221,26 +221,21 @@ def pull_request_comment_attributes(comment) note = "*By #{comment.author_username} (#{comment.author_email})*\n\n" end - comment_note = wrap_mentions_in_backticks(comment.note) + comment_note = comment.note note += # Provide some context for replying if comment.parent_comment - parent_comment_note = comment.parent_comment.note - truncated_note = if parent_comment_note.length > 80 - "#{parent_comment_note.truncate(80, omission: ' ')}..." - else - parent_comment_note - end - - "> #{wrap_mentions_in_backticks(truncated_note)}\n\n#{comment_note}" + parent_comment_note = comment.parent_comment.note.truncate(80, omission: ' ...') + + "> #{parent_comment_note}\n\n#{comment_note}" else comment_note end { project: project, - note: note, + note: wrap_mentions_in_backticks(note), author_id: author, created_at: comment.created_at, updated_at: comment.updated_at, diff --git a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb index 9fdd44a32005b0..c89fc2ad1a260c 100644 --- a/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb +++ b/spec/lib/gitlab/bitbucket_server_import/importers/pull_request_notes_importer_spec.rb @@ -204,9 +204,12 @@ def expect_log(stage:, message:) end context 'when the note has a parent note with @ mentions' do - let(:original_parent_text) { "Attention: @ali has worked on this. @fred's work from @.ali-ce/group#9?" } + let(:original_parent_text) do + "Attention: To inform @ali has worked on this. @fred's work from @.ali-ce/group#9?" + end + let(:expected_parent_text) do - "Attention: `@ali` has worked on this. `@fred`'s work from `@.ali-ce/group#9`?" + "Attention: To inform `@ali` has worked on this. `@fred`'s work from `@.ali-ce/gro` ..." end let(:pr_note) do @@ -240,7 +243,7 @@ def expect_log(stage:, message:) it 'adds the parent note before the actual note with backticks inserted' do importer.execute - expect(Note.first.note).to include("> #{expected_parent_text}\n\n") + expect(Note.first.note).to include("> #{expected_parent_text}") end end -- GitLab From 7684b2e4ad2dda7e2b4612cdc281c4d6017d3e28 Mon Sep 17 00:00:00 2001 From: carlad-gl Date: Thu, 12 Dec 2024 16:06:10 +0100 Subject: [PATCH 13/13] Apply MR spec suggestion --- .../bitbucket_server_import/stage/import_users_worker_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb b/spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb index 16cbaa95a592ee..dcc72c187f7c60 100644 --- a/spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb +++ b/spec/workers/gitlab/bitbucket_server_import/stage/import_users_worker_spec.rb @@ -16,9 +16,7 @@ describe '#perform' do it 'returns without calling the next import stage' do # no-oped. Will be removed in follow up https://gitlab.com/gitlab-org/gitlab/-/issues/508916 - result = worker.perform(project.id) - expect(::Gitlab::BitbucketServerImport::Stage::ImportUsersWorker).not_to receive(:perform_async) - expect(result).to be(true) + expect { worker.perform(project.id) }.not_to raise_error end end end -- GitLab