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(
  { escaped_char_spans: false, github_pre_lang: true, full_info_string: true, width: 120 }.freeze,
  T::Hash[Symbol, T.any(T::Boolean, Integer)]
)
COMMONMARKER_EXTENSIONS =
T.let(
  {
    autolink: true,
    header_ids: nil,
    shortcodes: false,
    strikethrough: true,
    table: true,
    tagfilter: true,
    tasklist: true
  }.freeze,
  T::Hash[Symbol, T.nilable(T::Boolean)]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(github_redirection_service:) ⇒ LinkAndMentionSanitizer

Returns a new instance of LinkAndMentionSanitizer.



53
54
55
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 53

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.



50
51
52
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 50

def github_redirection_service
  @github_redirection_service
end

Instance Method Details



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 58

def sanitize_links_and_mentions(text:, unsafe: false, format_html: true)
  doc = Commonmarker.parse(text, options: { extension: COMMONMARKER_EXTENSIONS, render: COMMONMARKER_OPTIONS })

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

  render_options = COMMONMARKER_OPTIONS.dup
  render_options[:hardbreaks] = false if text.match?(MARKDOWN_REGEX)
  render_options[:unsafe] = true if unsafe
  unless format_html
    return doc.to_commonmark(
      options: {
        extension: COMMONMARKER_EXTENSIONS,
        render: render_options
      }
    )
  end

  doc.to_html(
    options: {
      extension: COMMONMARKER_EXTENSIONS,
      render: render_options
    },
    plugins: { syntax_highlighter: nil }
  )
end