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
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/
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.



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

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.



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

def github_redirection_service
  @github_redirection_service
end

Instance Method Details



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

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)
  sanitize_nwo_text(doc)

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