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

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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
GITHUB_REF_REGEX =
%r{
  (?:https?://)?
  github\.com/(?<repo>#{GITHUB_USERNAME}/[^/\s]+)/
  (?:issue|pull)s?/(?<number>\d+)
}x
GITHUB_NWO_REGEX =

[^/s#]+ means one or more characters not matching (^) the class /, whitespace (s), or #

%r{(?<repo>#{GITHUB_USERNAME}/[^/\s#]+)#(?<number>\d+)}
MENTION_REGEX =
%r{(?<![A-Za-z0-9`~])@#{GITHUB_USERNAME}/?}
TEAM_MENTION_REGEX =

regex to match a team mention on github

%r{(?<![A-Za-z0-9`~])@(?<org>#{GITHUB_USERNAME})/(?<team>#{GITHUB_USERNAME})/?}
EOS_REGEX =

End of string

/\z/
MARKDOWN_REGEX =

regex to match markdown headers or links

/\[(.+?)\]\(([^)]+)\)|\[(.+?)\]|\A#+\s+([^\s].*)/
COMMONMARKER_OPTIONS =
T.let(
  %i(GITHUB_PRE_LANG FULL_INFO_STRING).freeze,
  T::Array[Symbol]
)
COMMONMARKER_EXTENSIONS =
T.let(
  %i(table tasklist strikethrough autolink tagfilter).freeze,
  T::Array[Symbol]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(github_redirection_service:) ⇒ LinkAndMentionSanitizer

Returns a new instance of LinkAndMentionSanitizer.



45
46
47
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 45

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.



42
43
44
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 42

def github_redirection_service
  @github_redirection_service
end

Instance Method Details



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 50

def sanitize_links_and_mentions(text:, unsafe: false, format_html: true)
  doc = CommonMarker.render_doc(
    text, :LIBERAL_HTML_TAG, COMMONMARKER_EXTENSIONS
  )

  sanitize_team_mentions(doc)
  sanitize_mentions(doc)
  sanitize_links(doc)
  sanitize_nwo_text(doc)

  render_options = if text.match?(MARKDOWN_REGEX)
                     COMMONMARKER_OPTIONS
                   else
                     COMMONMARKER_OPTIONS + [:HARDBREAKS]
                   end

  mode = unsafe ? :UNSAFE : :DEFAULT
  return doc.to_commonmark([mode] + render_options) unless format_html

  doc.to_html(([mode] + render_options), COMMONMARKER_EXTENSIONS)
end