1# frozen_string_literal: true
2
3module Atlassian
4  module JiraConnect
5    module Serializers
6      class PullRequestEntity < BaseEntity
7        STATUS_MAPPING = {
8          'opened' => 'OPEN',
9          'locked' => 'OPEN',
10          'merged' => 'MERGED',
11          'closed' => 'DECLINED'
12        }.freeze
13
14        expose :id, format_with: :string
15        expose :issueKeys do |mr|
16          JiraIssueKeyExtractor.new(mr.title, mr.description).issue_keys
17        end
18        expose :displayId do |mr|
19          mr.to_reference(full: true)
20        end
21        expose :title
22        expose :author, using: JiraConnect::Serializers::AuthorEntity
23        expose :commentCount do |mr|
24          if options[:user_notes_count]
25            options[:user_notes_count].fetch(mr.id, 0)
26          else
27            mr.user_notes_count
28          end
29        end
30        expose :source_branch, as: :sourceBranch
31        expose :target_branch, as: :destinationBranch
32        expose :lastUpdate do |mr|
33          mr.last_edited_at || mr.created_at
34        end
35        expose :status do |mr|
36          STATUS_MAPPING[mr.state] || 'UNKNOWN'
37        end
38
39        expose :sourceBranchUrl do |mr|
40          project_commits_url(mr.project, mr.source_branch)
41        end
42        expose :url do |mr|
43          merge_request_url(mr)
44        end
45      end
46    end
47  end
48end
49