BigW Consortium Gitlab

pull_request_formatter.rb 1.77 KB
Newer Older
1 2
module Gitlab
  module GithubImport
3
    class PullRequestFormatter < IssuableFormatter
4 5
      delegate :exists?, :project, :ref, :repo, :sha, to: :source_branch, prefix: true
      delegate :exists?, :project, :ref, :repo, :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 25 26
        }
      end

27 28 29 30
      def project_association
        :merge_requests
      end

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

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

39 40 41 42 43 44
      def source_branch_name
        @source_branch_name ||= begin
          source_branch_exists? ? source_branch_ref : "pull/#{number}/#{source_branch_ref}"
        end
      end

45
      def target_branch
46
        @target_branch ||= BranchFormatter.new(project, raw_data.base)
47 48
      end

49 50 51 52 53 54
      def target_branch_name
        @target_branch_name ||= begin
          target_branch_exists? ? target_branch_ref : "pull/#{number}/#{target_branch_ref}"
        end
      end

55 56
      private

57 58 59
      def state
        if raw_data.state == 'closed' && raw_data.merged_at.present?
          'merged'
60
        else
61
          super
62
        end
63 64 65 66
      end
    end
  end
end