BigW Consortium Gitlab

pull_request_formatter.rb 2.37 KB
Newer Older
1 2
module Gitlab
  module GithubImport
3
    class PullRequestFormatter < IssuableFormatter
4 5
      delegate :user, :project, :ref, :repo, :sha, to: :source_branch, prefix: true
      delegate :user, :exists?, :project, :ref, :repo, :sha, :short_sha, to: :target_branch, prefix: true
6

7 8
      def attributes
        {
9
          iid: number,
10 11
          title: raw_data.title,
          description: description,
12 13
          source_project: source_branch_project,
          source_branch: source_branch_name,
14
          source_branch_sha: source_branch_sha,
15 16
          target_project: target_branch_project,
          target_branch: target_branch_name,
17
          target_branch_sha: target_branch_sha,
18
          state: state,
19
          milestone: milestone,
20 21 22
          author_id: author_id,
          assignee_id: assignee_id,
          created_at: raw_data.created_at,
23
          updated_at: raw_data.updated_at,
24
          imported: true
25 26 27
        }
      end

28 29 30 31
      def project_association
        :merge_requests
      end

32
      def valid?
33
        source_branch.valid? && target_branch.valid?
34 35 36
      end

      def source_branch
37
        @source_branch ||= BranchFormatter.new(project, raw_data.head)
38 39
      end

40
      def source_branch_name
Gabriel Mazetto committed
41 42
        @source_branch_name ||=
          if cross_project? || !source_branch_exists?
43
            source_branch_name_prefixed
44
          else
Gabriel Mazetto committed
45
            source_branch_ref
46
          end
47 48
      end

49
      def source_branch_name_prefixed
50
        "gh-#{target_branch_short_sha}/#{number}/#{source_branch_user}/#{source_branch_ref}"
51 52
      end

53
      def source_branch_exists?
Gabriel Mazetto committed
54
        !cross_project? && source_branch.exists?
55 56
      end

57
      def target_branch
58
        @target_branch ||= BranchFormatter.new(project, raw_data.base)
59 60
      end

61
      def target_branch_name
Gabriel Mazetto committed
62
        @target_branch_name ||= target_branch_exists? ? target_branch_ref : target_branch_name_prefixed
63 64
      end

65
      def target_branch_name_prefixed
66
        "gl-#{target_branch_short_sha}/#{number}/#{target_branch_user}/#{target_branch_ref}"
67 68
      end

Gabriel Mazetto committed
69
      def cross_project?
70 71 72
        return true if source_branch_repo.nil?

        source_branch_repo.id != target_branch_repo.id
Gabriel Mazetto committed
73 74
      end

75 76 77 78
      def opened?
        state == 'opened'
      end

79 80
      private

81 82 83
      def state
        if raw_data.state == 'closed' && raw_data.merged_at.present?
          'merged'
84
        else
85
          super
86
        end
87 88 89 90
      end
    end
  end
end