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_REF_REGEX =
%r{
  (?:https?://)?
  github\.com/[^/\s]+/[^/\s]+/
  (?:issue|pull)s?/(?<number>\d+)
}x.freeze
CODEBLOCK_REGEX =

Note that we’re being deliberately careful about not matching different length strings of what look like code block quotes. By doing so we err on the side of sanitizing, which is much better than accidentally not sanitizing.

rubocop:disable Style/RegexpLiteral

%r{
  (?=[\s]`{3}[^`])|(?=[\s]`{3}\Z)|(?=\A`{3}[^`])|
  (?=[\s]~{3}[^~])|(?=[\s]~{3}\Z)|(?=\A~{3}[^~])
}x.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(github_redirection_service:) ⇒ LinkAndMentionSanitizer

Returns a new instance of LinkAndMentionSanitizer.



29
30
31
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 29

def initialize(github_redirection_service:)
  @github_redirection_service = github_redirection_service
end

Instance Attribute Details

#github_redirection_serviceObject (readonly)

rubocop:enable Style/RegexpLiteral



27
28
29
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 27

def github_redirection_service
  @github_redirection_service
end

Instance Method Details



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 33

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 "```"
  snippets = text.split(CODEBLOCK_REGEX)
  if snippets.first&.start_with?(CODEBLOCK_REGEX)
    snippets = ["", *snippets]
  end

  snippets.map.with_index do |snippet, index|
    next snippet if index.odd?

    snippet = sanitize_mentions(snippet)
    sanitize_links(snippet)
  end.join
end