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
GITLAB_USERNAME =
/[a-z0-9](?:[a-z0-9_.-]*[a-z0-9_-])?/i
GITHUB_HOST =
"github.com"
GITLAB_HOST =
"gitlab.com"
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+)}
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:, metadata_source_url: nil) ⇒ LinkAndMentionSanitizer

Returns a new instance of LinkAndMentionSanitizer.



62
63
64
65
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 62

def initialize(github_redirection_service:, metadata_source_url: nil)
  @github_redirection_service = github_redirection_service
  @metadata_source_url = 
end

Instance Attribute Details

#github_redirection_serviceObject (readonly)

Returns the value of attribute github_redirection_service.



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

def github_redirection_service
  @github_redirection_service
end

#metadata_source_urlObject (readonly)

Returns the value of attribute metadata_source_url.



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

def 
  @metadata_source_url
end

Instance Method Details



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb', line 68

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