summaryrefslogtreecommitdiff
path: root/app/services/issues/export_csv_service.rb
blob: dd43c77adfa9a56102702f2cd9c7b4bed90f6535 (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
61
# frozen_string_literal: true

module Issues
  class ExportCsvService < Issuable::ExportCsv::BaseService
    include Gitlab::Routing.url_helpers
    include GitlabRoutingHelper

    def initialize(issuables_relation, project)
      super

      @labels = @issuables.labels_hash.transform_values { |labels| labels.sort.join(',').presence }
    end

    def email(user)
      Notify.issues_csv_email(user, project, csv_data, csv_builder.status).deliver_now
    end

    private

    def associations_to_preload
      %i(author assignees timelogs milestone project)
    end

    def header_to_value_hash
      {
       'Issue ID' => 'iid',
       'URL' => -> (issue) { issue_url(issue) },
       'Title' => 'title',
       'State' => -> (issue) { issue.closed? ? 'Closed' : 'Open' },
       'Description' => 'description',
       'Author' => 'author_name',
       'Author Username' => -> (issue) { issue.author&.username },
       'Assignee' => -> (issue) { issue.assignees.map(&:name).join(', ') },
       'Assignee Username' => -> (issue) { issue.assignees.map(&:username).join(', ') },
       'Confidential' => -> (issue) { issue.confidential? ? 'Yes' : 'No' },
       'Locked' => -> (issue) { issue.discussion_locked? ? 'Yes' : 'No' },
       'Due Date' => -> (issue) { issue.due_date&.to_s(:csv) },
       'Created At (UTC)' => -> (issue) { issue.created_at&.to_s(:csv) },
       'Updated At (UTC)' => -> (issue) { issue.updated_at&.to_s(:csv) },
       'Closed At (UTC)' => -> (issue) { issue.closed_at&.to_s(:csv) },
       'Milestone' => -> (issue) { issue.milestone&.title },
       'Weight' => -> (issue) { issue.weight },
       'Labels' => -> (issue) { issue_labels(issue) },
       'Time Estimate' => ->(issue) { issue.time_estimate.to_s(:csv) },
       'Time Spent' => -> (issue) { issue_time_spent(issue) }
      }
    end

    def issue_labels(issue)
      @labels[issue.id]
    end

    # rubocop: disable CodeReuse/ActiveRecord
    def issue_time_spent(issue)
      issue.timelogs.map(&:time_spent).sum
    end
    # rubocop: enable CodeReuse/ActiveRecord
  end
end

Issues::ExportCsvService.prepend_if_ee('EE::Issues::ExportCsvService')