BigW Consortium Gitlab

merge_request_parser.rb 1.18 KB
Newer Older
1 2 3
module Gitlab
  module ImportExport
    class MergeRequestParser
4
      FORKED_PROJECT_ID = nil
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

      def initialize(project, diff_head_sha, merge_request, relation_hash)
        @project = project
        @diff_head_sha = diff_head_sha
        @merge_request = merge_request
        @relation_hash = relation_hash
      end

      def parse!
        if fork_merge_request? && @diff_head_sha
          @merge_request.source_project_id = @relation_hash['project_id']

          fetch_ref unless branch_exists?(@merge_request.source_branch)
          create_target_branch unless branch_exists?(@merge_request.target_branch)
        end

        @merge_request
      end

      def create_target_branch
        @project.repository.create_branch(@merge_request.target_branch, @merge_request.target_branch_sha)
      end

      def fetch_ref
James Lopez committed
29
        @project.repository.fetch_ref(@project.repository, source_ref: @diff_head_sha, target_ref: @merge_request.source_branch)
30 31 32
      end

      def branch_exists?(branch_name)
33
        @project.repository.raw.branch_exists?(branch_name)
34 35 36 37 38 39 40 41
      end

      def fork_merge_request?
        @relation_hash['source_project_id'] == FORKED_PROJECT_ID
      end
    end
  end
end