-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding code tags around any nwo#number text string #5646
Changes from 6 commits
3605fbf
b07430e
0f6b6c7
7c99a8a
3f8fe0c
730c4c1
6fa21ee
640186e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ class LinkAndMentionSanitizer | |
github\.com/(?<repo>#{GITHUB_USERNAME}/[^/\s]+)/ | ||
(?:issue|pull)s?/(?<number>\d+) | ||
}x.freeze | ||
GITHUB_NWO_REGEX = %r{(?<repo>#{GITHUB_USERNAME}/[^/\s#]+)#(?<number>\d+)}.freeze | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain this regex? Specifically There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cribbed it together from the above regex that grabs the repo and number from a full github pr or issue link. Given a string like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pavera : could you put the regex explanation in the comment as well. It will be easier for future debugging. |
||
MENTION_REGEX = %r{(?<![A-Za-z0-9`~])@#{GITHUB_USERNAME}/?}.freeze | ||
# regex to match a team mention on github | ||
TEAM_MENTION_REGEX = %r{(?<![A-Za-z0-9`~])@(?<org>#{GITHUB_USERNAME})/(?<team>#{GITHUB_USERNAME})/?}.freeze | ||
|
@@ -40,6 +41,7 @@ def sanitize_links_and_mentions(text:, unsafe: false) | |
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) | ||
|
@@ -109,6 +111,25 @@ def sanitize_links(doc) | |
end | ||
end | ||
|
||
def sanitize_nwo_text(doc) | ||
doc.walk do |node| | ||
if node.type == :text && | ||
node.string_content.match?(GITHUB_NWO_REGEX) && | ||
!parent_node_link?(node) | ||
replace_nwo_node(node) | ||
end | ||
end | ||
end | ||
|
||
def replace_nwo_node(node) | ||
match = node.string_content.match(GITHUB_NWO_REGEX) | ||
repo = match.named_captures.fetch("repo") | ||
number = match.named_captures.fetch("number") | ||
new_node = build_nwo_text_node("#{repo}##{number}") | ||
node.insert_before(new_node) | ||
node.delete | ||
end | ||
|
||
def replace_github_host(text) | ||
text.gsub( | ||
/(www\.)?github.com/, github_redirection_service || "github.com" | ||
|
@@ -171,6 +192,12 @@ def build_mention_link_text_nodes(text) | |
[code_node] | ||
end | ||
|
||
def build_nwo_text_node(text) | ||
code_node = CommonMarker::Node.new(:code) | ||
code_node.string_content = text | ||
code_node | ||
end | ||
|
||
def create_link_node(url, text) | ||
link_node = CommonMarker::Node.new(:link) | ||
code_node = CommonMarker::Node.new(:code) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -281,6 +281,17 @@ | |
end | ||
end | ||
|
||
context "with a GitHub NWO and PR number" do | ||
let(:text) do | ||
"dsp-testing/dependabot-ts-definitely-typed#25" | ||
end | ||
it do | ||
is_expected.to eq( | ||
"<p><code>dsp-testing/dependabot-ts-definitely-typed#25</code></p>\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to include the newline at the end of the line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the call here renders the doc and adds the newline, so the test check has to expect it as far as I know. |
||
) | ||
end | ||
end | ||
|
||
context "with a GitHub link in rdoc" do | ||
let(:text) do | ||
"{Issue 111}[https://github.com/dependabot/dependabot-core/issues/111]" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the rationale for this? I don't see it mentioned in the commit messages / PR description, so was surprised to see it... did I just miss it somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was necessitated by #5529