module RiskSummary::RiskParser
copied from github.com/zendesk/samson/blob/master/app/models/changeset/pull_request.rb modified to use unsafe regex stripping
Public Class Methods
parse(text)
click to toggle source
# File lib/risk_summary.rb, line 12 def parse(text) return :missing unless section = risk_section(text) return :missing if section.strip.empty? return :none if section.match?(/\A\s*-?\s*None\Z/i) section end
Private Class Methods
risk_section(body)
click to toggle source
# File lib/risk_summary.rb, line 21 def risk_section(body) body_stripped = body.gsub(%r{</?[^>]+?>}, "") # not safe, but much simpler than pulling in nokogiri return nil unless section = section_content('Risks', body_stripped) section.rstrip.sub(/\A\s*\n/, "") end
section_content(section_title, text)
click to toggle source
# File lib/risk_summary.rb, line 27 def section_content(section_title, text) # ### Risks or Risks followed by === / --- desired_header_regexp = "^(?:\\s*#+\\s*#{section_title}.*|\\s*#{section_title}.*\\n\\s*(?:-{2,}|={2,}))\\n" content_regexp = '([\W\w]*?)' # capture all section content, including new lines, but not next header next_header_regexp = '(?=^(?:\s*#+|.*\n\s*(?:-{2,}|={2,}\s*\n))|\z)' text[/#{desired_header_regexp}#{content_regexp}#{next_header_regexp}/i, 1] end