summaryrefslogtreecommitdiff
path: root/lib/gitlab/phabricator_import/representation/task.rb
blob: 6aedc71b62618802de37bf1702164eb410788e12 (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
# frozen_string_literal: true
module Gitlab
  module PhabricatorImport
    module Representation
      class Task
        def initialize(json)
          @json = json
        end

        def phabricator_id
          json['phid']
        end

        def issue_attributes
          @issue_attributes ||= {
            title: issue_title,
            description: issue_description,
            state: issue_state,
            created_at: issue_created_at,
            closed_at: issue_closed_at
          }
        end

        private

        attr_reader :json

        def issue_title
          # The 255 limit is the validation we impose on the Issue title in
          # Issuable
          @issue_title ||= json['fields']['name'].truncate(255)
        end

        def issue_description
          json['fields']['description']['raw']
        end

        def issue_state
          issue_closed_at.present? ? :closed : :opened
        end

        def issue_created_at
          return unless json['fields']['dateCreated']

          @issue_created_at ||= cast_datetime(json['fields']['dateCreated'])
        end

        def issue_closed_at
          return unless json['fields']['dateClosed']

          @issue_closed_at ||= cast_datetime(json['fields']['dateClosed'])
        end

        def cast_datetime(value)
          Time.at(value.to_i)
        end
      end
    end
  end
end