Class: Dradis::Plugins::CSVExport::Exporter

Inherits:
Export::Base
  • Object
show all
Defined in:
lib/dradis/plugins/csv_export/exporter.rb

Instance Method Summary collapse

Instance Method Details

#export(args = {}) ⇒ Object



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
# File 'lib/dradis/plugins/csv_export/exporter.rb', line 3

def export(args={})
  issues = content_service.all_issues

  if issues.empty?
    return "The project didn't contain any issues"
  else
    # All fields from all Issues in the project
    issue_keys    = issues.map(&:fields).map(&:keys).flatten.uniq

    # All fields from all Evidence in the project
    evidence_keys = issues.map do |issue|
      if issue.evidence.any?
        issue.evidence.map(&:fields).map(&:keys).flatten.uniq
      else
        []
      end
    end.flatten.uniq

    keys = issue_keys + evidence_keys

    issue_row    = []
    evidence_row = []

    # Create the CSV data
    csv_string = ::CSV.generate do |csv|

      # Add the header line with all the field names. Remember note.field returns
      # a hash: {'Title' => 'Foo', 'Description' => 'Bar'}
      csv << keys

      # For each of the notes in our category, we dump the field values.
      # This assumes all notes have the same fields, in the same order
      issues.each do |issue|
        issue_row = issue_keys.map do |key|
          issue.fields.fetch(key, 'n/a')
        end

        # If we've got multiple Evidence, we add one row per Evidence
        if issue.evidence.any?
          issue.evidence.each do |evidence|
            evidence_row = evidence_keys.map do |key|
              evidence.fields.fetch(key, 'n/a')
            end

            csv << issue_row + evidence_row
          end
        else
          csv << issue_row
        end
      end
    end

    csv_string
  end

end