Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pull_request_importer.rb « importers « bitbucket_server_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8fd602c851c8c55c3427a9f8855f496a8701b94d (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module Gitlab
  module BitbucketServerImport
    module Importers
      class PullRequestImporter
        include Loggable

        def initialize(project, hash)
          @project = project
          @formatter = Gitlab::ImportFormatter.new
          @user_finder = UserFinder.new(project)
          @mentions_converter = Gitlab::Import::MentionsConverter.new('bitbucket_server', project.id)

          # 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
        end

        def execute
          log_info(import_stage: 'import_pull_request', message: 'starting', iid: object[:iid])

          attributes = {
            iid: object[:iid],
            title: object[:title],
            description: description,
            reviewer_ids: reviewers,
            source_project_id: project.id,
            source_branch: Gitlab::Git.ref_name(object[:source_branch_name]),
            source_branch_sha: source_branch_sha,
            target_project_id: project.id,
            target_branch: Gitlab::Git.ref_name(object[:target_branch_name]),
            target_branch_sha: object[:target_branch_sha],
            state_id: MergeRequest.available_states[object[:state]],
            author_id: user_finder.author_id(object),
            created_at: object[:created_at],
            updated_at: object[:updated_at]
          }

          creator = Gitlab::Import::MergeRequestCreator.new(project)

          creator.execute(attributes)

          log_info(import_stage: 'import_pull_request', message: 'finished', iid: object[:iid])
        end

        private

        attr_reader :object, :project, :formatter, :user_finder, :mentions_converter

        def description
          description = ''
          description += author_line
          description += object[:description] if object[:description]

          if Feature.enabled?(:bitbucket_server_convert_mentions_to_users, project.creator)
            description = mentions_converter.convert(description)
          end

          description
        end

        def author_line
          return '' if user_finder.uid(object)

          formatter.author_line(object[:author])
        end

        def reviewers
          return [] unless object[:reviewers].present?

          object[:reviewers].filter_map do |reviewer|
            if Feature.enabled?(:bitbucket_server_user_mapping_by_username, type: :ops)
              user_finder.find_user_id(by: :username, value: reviewer.dig('user', 'slug'))
            else
              user_finder.find_user_id(by: :email, value: reviewer.dig('user', 'emailAddress'))
            end
          end
        end

        def source_branch_sha
          source_branch_sha = project.repository.commit(object[:source_branch_sha])&.sha

          return source_branch_sha if source_branch_sha

          project.repository.find_commits_by_message(object[:source_branch_sha])&.first&.sha
        end
      end
    end
  end
end