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
MENTION_REGEX =
%r{(?<![A-Za-z0-9`~])@#{GITHUB_USERNAME}/?}.freeze
TEAM_MENTION_REGEX =

regex to match a team mention on github

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

End of string

/\z/.freeze
COMMONMARKER_OPTIONS =
%i(
  GITHUB_PRE_LANG FULL_INFO_STRING
).freeze
COMMONMARKER_EXTENSIONS =
%i(
  table tasklist strikethrough autolink tagfilter
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(github_redirection_service:) ⇒ LinkAndMentionSanitizer

Returns a new instance of LinkAndMentionSanitizer.



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

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.



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

def github_redirection_service
  @github_redirection_service
end

Instance Method Details



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

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

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

  mode = unsafe ? :UNSAFE : :DEFAULT
  doc.to_html(([mode] + COMMONMARKER_OPTIONS), COMMONMARKER_EXTENSIONS)
end