Class: Dependabot::PullRequestCreator::MessageBuilder::LinkAndMentionSanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb

Constant Summary collapse

GITHUB_USERNAME =
/[a-z0-9]+(-[a-z0-9]+)*/i.freeze
GITHUB_REF_REGEX =
%r{
  (?:https?://)?
  github\.com/(?<repo>#{GITHUB_USERNAME}/[^/\s]+)/
  (?:issue|pull)s?/(?<number>\d+)
}x.freeze
CODEBLOCK_REGEX =

rubocop:disable Metrics/LineLength Context:

  • github.github.com/gfm/#fenced-code-block (“‘ or ~~~) (?<=n|^) Positive look-behind to ensure we start at a line start (?>`3,|~3,) Atomic group marking the beginning of the block (3 or more chars) (?>k<fenceopen>) Atomic group marking the end of the code block (same length as opening)

  • github.github.com/gfm/#code-span (?<codespanopen>‘+) Capturing group marking the beginning of the span (1 or more chars) (?![^`]*?n2,) Negative look-ahead to avoid empty lines inside code span (?:.|n)*? Non-capturing group to consume code span content (non-eager) (?>k<codespanopen>) Atomic group marking the end of the code span (same length as opening)

rubocop:enable Metrics/LineLength

/```|~~~/.freeze
EOS_REGEX =

End of string

/\z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(github_redirection_service:) ⇒ LinkAndMentionSanitizer

Returns a new instance of LinkAndMentionSanitizer.



34
35
36
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 34

def initialize(github_redirection_service:)
  @github_redirection_service = github_redirection_service
end

Instance Attribute Details

#github_redirection_serviceObject (readonly)

Returns the value of attribute github_redirection_service.



32
33
34
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 32

def github_redirection_service
  @github_redirection_service
end

Instance Method Details



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 38

def sanitize_links_and_mentions(text:)
  # We don't want to sanitize any links or mentions that are contained
  # within code blocks, so we split the text on "```" or "~~~"
  lines = []
  scan = StringScanner.new(text)
  until scan.eos?
    line = scan.scan_until(CODEBLOCK_REGEX) ||
           scan.scan_until(EOS_REGEX)
    delimiter = line.match(CODEBLOCK_REGEX)&.to_s
    unless delimiter && lines.count { |l| l.include?(delimiter) }.odd?
      line = sanitize_mentions(line)
      line = sanitize_links(line)
    end
    lines << line
  end
  lines.join
end