diff options
Diffstat (limited to 'lib/gitlab/github_import/importer')
6 files changed, 227 insertions, 6 deletions
diff --git a/lib/gitlab/github_import/importer/lfs_objects_importer.rb b/lib/gitlab/github_import/importer/lfs_objects_importer.rb index 5980b3c2179..c74a7706117 100644 --- a/lib/gitlab/github_import/importer/lfs_objects_importer.rb +++ b/lib/gitlab/github_import/importer/lfs_objects_importer.rb @@ -23,16 +23,13 @@ module Gitlab end def each_object_to_import - lfs_objects = Projects::LfsPointers::LfsImportService.new(project).execute + lfs_objects = Projects::LfsPointers::LfsObjectDownloadListService.new(project).execute lfs_objects.each do |object| yield object end rescue StandardError => e - Gitlab::Import::Logger.error( - message: 'The Lfs import process failed', - error: e.message - ) + error(project.id, e) end end end diff --git a/lib/gitlab/github_import/importer/pull_request_merged_by_importer.rb b/lib/gitlab/github_import/importer/pull_request_merged_by_importer.rb new file mode 100644 index 00000000000..11181edf0e9 --- /dev/null +++ b/lib/gitlab/github_import/importer/pull_request_merged_by_importer.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +module Gitlab + module GithubImport + module Importer + class PullRequestMergedByImporter + def initialize(pull_request, project, client) + @project = project + @pull_request = pull_request + @client = client + end + + def execute + merge_request = project.merge_requests.find_by_iid(pull_request.iid) + user_finder = GithubImport::UserFinder.new(project, client) + gitlab_user_id = user_finder.user_id_for(pull_request.merged_by) + + if gitlab_user_id + timestamp = Time.new.utc + MergeRequest::Metrics.upsert({ + target_project_id: project.id, + merge_request_id: merge_request.id, + merged_by_id: gitlab_user_id, + created_at: timestamp, + updated_at: timestamp + }, unique_by: :merge_request_id) + else + merge_request.notes.create!( + importing: true, + note: "*Merged by: #{pull_request.merged_by.login}*", + author_id: project.creator_id, + project: project, + created_at: pull_request.created_at + ) + end + end + + private + + attr_reader :project, :pull_request, :client + end + end + end +end diff --git a/lib/gitlab/github_import/importer/pull_request_review_importer.rb b/lib/gitlab/github_import/importer/pull_request_review_importer.rb new file mode 100644 index 00000000000..14ee69ba089 --- /dev/null +++ b/lib/gitlab/github_import/importer/pull_request_review_importer.rb @@ -0,0 +1,101 @@ +# frozen_string_literal: true + +module Gitlab + module GithubImport + module Importer + class PullRequestReviewImporter + def initialize(review, project, client) + @review = review + @project = project + @client = client + @merge_request = project.merge_requests.find_by_id(review.merge_request_id) + end + + def execute + user_finder = GithubImport::UserFinder.new(project, client) + gitlab_user_id = user_finder.user_id_for(review.author) + + if gitlab_user_id + add_review_note!(gitlab_user_id) + add_approval!(gitlab_user_id) + else + add_complementary_review_note!(project.creator_id) + end + end + + private + + attr_reader :review, :merge_request, :project, :client + + def add_review_note!(author_id) + return if review.note.empty? + + add_note!(author_id, review_note_content) + end + + def add_complementary_review_note!(author_id) + return if review.note.empty? && !review.approval? + + note = "*Created by %{login}*\n\n%{note}" % { + note: review_note_content, + login: review.author.login + } + + add_note!(author_id, note) + end + + def review_note_content + header = "**Review:** #{review.review_type.humanize}" + + if review.note.present? + "#{header}\n\n#{review.note}" + else + header + end + end + + def add_note!(author_id, note) + note = Note.new(note_attributes(author_id, note)) + + note.save! + end + + def note_attributes(author_id, note, extra = {}) + { + importing: true, + noteable_id: merge_request.id, + noteable_type: 'MergeRequest', + project_id: project.id, + author_id: author_id, + note: note, + system: false, + created_at: review.submitted_at, + updated_at: review.submitted_at + }.merge(extra) + end + + def add_approval!(user_id) + return unless review.review_type == 'APPROVED' + + add_approval_system_note!(user_id) + + merge_request.approvals.create!( + user_id: user_id, + created_at: review.submitted_at + ) + end + + def add_approval_system_note!(user_id) + attributes = note_attributes( + user_id, + 'approved this merge request', + system: true, + system_note_metadata: SystemNoteMetadata.new(action: 'approved') + ) + + Note.create!(attributes) + end + end + end + end +end diff --git a/lib/gitlab/github_import/importer/pull_requests_importer.rb b/lib/gitlab/github_import/importer/pull_requests_importer.rb index dcae8ca01fa..7f1569f592f 100644 --- a/lib/gitlab/github_import/importer/pull_requests_importer.rb +++ b/lib/gitlab/github_import/importer/pull_requests_importer.rb @@ -11,7 +11,7 @@ module Gitlab end def representation_class - Representation::PullRequest + Gitlab::GithubImport::Representation::PullRequest end def sidekiq_worker_class diff --git a/lib/gitlab/github_import/importer/pull_requests_merged_by_importer.rb b/lib/gitlab/github_import/importer/pull_requests_merged_by_importer.rb new file mode 100644 index 00000000000..466288fde4c --- /dev/null +++ b/lib/gitlab/github_import/importer/pull_requests_merged_by_importer.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Gitlab + module GithubImport + module Importer + class PullRequestsMergedByImporter + include ParallelScheduling + + def importer_class + PullRequestMergedByImporter + end + + def representation_class + Gitlab::GithubImport::Representation::PullRequest + end + + def sidekiq_worker_class + ImportPullRequestMergedByWorker + end + + def collection_method + :pull_requests_merged_by + end + + def id_for_already_imported_cache(pr) + pr.number + end + + def each_object_to_import + project.merge_requests.with_state(:merged).find_each do |merge_request| + pull_request = client.pull_request(project.import_source, merge_request.iid) + yield(pull_request) + end + end + end + end + end +end diff --git a/lib/gitlab/github_import/importer/pull_requests_reviews_importer.rb b/lib/gitlab/github_import/importer/pull_requests_reviews_importer.rb new file mode 100644 index 00000000000..6d1b588f0e0 --- /dev/null +++ b/lib/gitlab/github_import/importer/pull_requests_reviews_importer.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +module Gitlab + module GithubImport + module Importer + class PullRequestsReviewsImporter + include ParallelScheduling + + def importer_class + PullRequestReviewImporter + end + + def representation_class + Gitlab::GithubImport::Representation::PullRequestReview + end + + def sidekiq_worker_class + ImportPullRequestReviewWorker + end + + def collection_method + :pull_request_reviews + end + + def id_for_already_imported_cache(review) + review.github_id + end + + def each_object_to_import + project.merge_requests.find_each do |merge_request| + reviews = client.pull_request_reviews(project.import_source, merge_request.iid) + reviews.each do |review| + review.merge_request_id = merge_request.id + yield(review) + end + end + end + end + end + end +end |