# frozen_string_literal: true

# Class that represents a package issue.
class PackageIssue
  attr_reader :cve_number

  def initialize(severity, cve_number, package)
    @severity   = severity
    @cve_number = cve_number
    @package    = package
  end

  def description
    return "<#{@related_url}|#{@cve_number}>" unless @related_url.nil?

    @cve_number
  end

  # Only set the related URL if it contains the same CVE number as present in the package description.
  def related_url=(url)
    @related_url = url if url.include?(@cve_number)
  end

  def to_slack_attachment
    color = @severity == 'CRITICAL' ? 'danger' : 'warning'
    text  = "Package `#{@package}`\n#{description}"
    {
      'color': color,
      'fallback': text,
      'title': @severity.capitalize,
      'text': text
    }
  end
end
