Skip to content

Commit

Permalink
Merge pull request #5646 from dependabot/pavera/add-spaces-to-nwo-refs
Browse files Browse the repository at this point in the history
Adding code tags around any nwo#number text string
  • Loading branch information
pavera authored Sep 7, 2022
2 parents 5a3f022 + 640186e commit 6a10daa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"name": "Debug Tests",
"type": "Ruby",
"request": "launch",
"program": "${workspaceRoot}/${input:ecosystem}/.bundle/bin/rspec",
"program": "${workspaceRoot}/omnibus/.bundle/bin/rspec",
"cwd": "${workspaceRoot}/${input:ecosystem}",
"useBundler": true,
"args": ["${input:test_path}"],
Expand Down Expand Up @@ -106,6 +106,7 @@
"options": [
"bundler",
"cargo",
"common",
"composer",
"docker",
"elm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class LinkAndMentionSanitizer
github\.com/(?<repo>#{GITHUB_USERNAME}/[^/\s]+)/
(?:issue|pull)s?/(?<number>\d+)
}x.freeze
# [^/\s#]+ means one or more characters not matching (^) the class /, whitespace (\s), or #
GITHUB_NWO_REGEX = %r{(?<repo>#{GITHUB_USERNAME}/[^/\s#]+)#(?<number>\d+)}.freeze
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
Expand All @@ -40,6 +42,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)
Expand Down Expand Up @@ -109,6 +112,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"
Expand Down Expand Up @@ -171,6 +193,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
end
end

context "with a GitHub link in rdoc" do
let(:text) do
"{Issue 111}[https://github.com/dependabot/dependabot-core/issues/111]"
Expand Down

0 comments on commit 6a10daa

Please sign in to comment.