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

/
  # fenced code block
  (?<=\n|^)(?<fenceopen>(?>`{3,}|~{3,})).*?(?>\k<fenceopen>)|
  # code span
  (?<codespanopen>`+)(?![^`]*?\n{2,})(?:.|\n)*?(?>\k<codespanopen>)
/xm.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.



39
40
41
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 39

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.



37
38
39
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 37

def github_redirection_service
  @github_redirection_service
end

Instance Method Details



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 43

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 "~~~"
  sanitized_text = []
  scan = StringScanner.new(text)
  until scan.eos?
    block = scan.scan_until(CODEBLOCK_REGEX) ||
            scan.scan_until(EOS_REGEX)
    sanitized_text << sanitize_links_and_mentions_in_block(block)
  end
  sanitized_text.join
end