summaryrefslogtreecommitdiff
path: root/lib/gitlab/import/merge_request_creator.rb
blob: 8291372bba94430407dfb99da1ac008f4d5b6ea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

# This module is designed for importers that need to create many merge
# requests quickly.  When creating merge requests there are a lot of hooks
# that may run, for many different reasons. Many of these hooks (e.g. the ones
# used for rendering Markdown) are completely unnecessary and may even lead to
# transaction timeouts.
#
# To ensure importing merge requests has a minimal impact and can complete in
# a reasonable time we bypass all the hooks by inserting the row and then
# retrieving it. We then only perform the additional work that is strictly
# necessary.
module Gitlab
  module Import
    class MergeRequestCreator
      include ::Gitlab::Import::DatabaseHelpers
      include ::Gitlab::Import::MergeRequestHelpers

      attr_accessor :project

      def initialize(project)
        @project = project
      end

      def execute(attributes)
        source_branch_sha = attributes.delete(:source_branch_sha)
        target_branch_sha = attributes.delete(:target_branch_sha)
        iid = attributes[:iid]

        merge_request, already_exists = create_merge_request_without_hooks(project, attributes, iid)

        if merge_request
          insert_or_replace_git_data(merge_request, source_branch_sha, target_branch_sha, already_exists)
        end

        merge_request
      end
    end
  end
end