vulnerability_presenter.rb 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
# frozen_string_literal: true

class VulnerabilityPresenter < Gitlab::View::Presenter::Delegated
  presents :vulnerability

  def links
    vulnerability.links.map(&:with_indifferent_access)
  end

10 11 12 13
  def remediations
    vulnerability.remediations.to_a.compact.map(&:with_indifferent_access)
  end

14 15 16 17 18 19
  def location_text
    return file unless line

    "#{file}:#{line}"
  end

20 21 22
  def location_link_with_raw_path
    location_link_for(raw_path)
  end
23

24 25
  def location_link
    location_link_for(blob_path)
26 27
  end

28
  def raw_path
29 30
    return unless file

31 32
    path_with_line_number(project_raw_path(vulnerability.project, File.join(pipeline_branch, file)))
  end
33

34 35
  def blob_path
    return unless file
36

37
    path_with_line_number(project_blob_path(vulnerability.project, File.join(pipeline_branch, file)))
38 39
  end

40 41 42 43 44 45 46 47
  def scanner
    finding.scanner || {}
  end

  def scan
    finding.scan || {}
  end

48 49 50 51 52 53 54
  def jira_issue_description
    ApplicationController.render(
      template: 'vulnerabilities/jira_issue_description.md.erb',
      locals: { vulnerability: self }
    )
  end

55 56 57 58
  def description
    vulnerability.description || finding.description
  end

59 60
  private

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  def location_link_for(path)
    return location_text unless path

    "#{root_url}#{path}"
  end

  def pipeline_branch
    finding.pipelines&.last&.sha || project.default_branch
  end

  def path_with_line_number(path)
    return unless path

    path = path.gsub(/^\//, '')

    add_line_numbers(path, finding.location['start_line'], finding.location['end_line'])
  end

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  def root_url
    Gitlab::Routing.url_helpers.root_url
  end

  def line
    finding.location.dig("start_line")
  end

  def file
    finding.location.dig("file")
  end

  def add_line_numbers(path, start_line, end_line)
    return path unless start_line

    path.tap do |complete_path|
      complete_path << "#L#{start_line}"
      complete_path << "-#{end_line}" if end_line != start_line
    end
  end
end