Skip to content

Commit a0a2daa

Browse files
v-HaripriyaCv-HaripriyaC
andauthored
Fix issue #13972: Ensure 'Sourced from' links format correctly for scoped packages (#14833)
When scoped package names (@scope/pkg) appear in markdown links, they were being processed incorrectly: 1. Added guard to skip team-mention-shaped text already inside links 2. For team mentions inside links: insert zero-width space (prevents notifications) without replacing the text node (preserves link formatting) 3. For team mentions outside links: use normal code-wrapped node replacement This fixes both the formatting issue (#13972) and prevents unwanted team notifications for scoped package names in 'Sourced from' release notes links. Co-authored-by: v-HaripriyaC <v-haripriyac@microsoft.com>
1 parent 5ae4593 commit a0a2daa

2 files changed

Lines changed: 42 additions & 10 deletions

File tree

common/lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,25 @@ def sanitize_mentions(doc)
106106
end
107107
end
108108

109-
# When we come across something that looks like a team mention (e.g. @dependabot/reviewers),
110-
# we replace it with a text node.
111-
# This is because there are ecosystems that have packages that follow the same pattern
112-
# (e.g. @angular/angular-cli), and we don't want to create an invalid link, since
113-
# team mentions link to `https://github.com/org/:organization_name/teams/:team_name`.
109+
# Sanitize team mentions (e.g. @org/team) to prevent notifications; must run before sanitize_mentions.
114110
sig { params(doc: Commonmarker::Node).void }
115111
def sanitize_team_mentions(doc)
116112
doc.walk do |node|
117113
if node.type == :text &&
118114
node.string_content.match?(TEAM_MENTION_REGEX)
115+
if parent_node_link?(node)
116+
# Preserve text node formatting while preventing notifications with zero-width space
117+
node.string_content = node.string_content.gsub(TEAM_MENTION_REGEX) do |match|
118+
insert_zero_width_space_in_mention(match)
119+
end
120+
else
121+
nodes = build_team_mention_nodes(node.string_content)
119122

120-
nodes = build_team_mention_nodes(node.string_content)
121-
122-
nodes.each do |n|
123-
node.insert_before(n)
123+
nodes.each do |n|
124+
node.insert_before(n)
125+
end
126+
node.delete
124127
end
125-
node.delete
126128
end
127129
end
128130
end

common/spec/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,36 @@
233233
)
234234
end
235235
end
236+
237+
context "when a scoped package name is inside a link" do
238+
let(:text) { "Sourced from [@adamlui/minify.js](https://npmjs.com/package/@adamlui/minify.js)" }
239+
240+
it "preserves the link without partial code formatting" do
241+
expect(sanitize_links_and_mentions).to eq(
242+
"<p>Sourced from <a href=\"https://npmjs.com/package/@adamlui/minify.js\">" \
243+
"@\u200Badamlui/minify.js</a></p>\n"
244+
)
245+
end
246+
end
247+
248+
context "when a scoped package name without extension is inside a link" do
249+
let(:text) { "Sourced from [@angular/cli](https://npmjs.com/package/@angular/cli)" }
250+
251+
it "preserves the link without code formatting" do
252+
expect(sanitize_links_and_mentions).to eq(
253+
"<p>Sourced from <a href=\"https://npmjs.com/package/@angular/cli\">" \
254+
"@\u200Bangular/cli</a></p>\n"
255+
)
256+
end
257+
end
258+
259+
context "when a team mention is inside a link" do
260+
let(:text) { "Reviewed by [@dependabot/reviewers](https://github.com/orgs/dependabot/teams/reviewers)" }
261+
262+
it "inserts zero-width space to prevent notifications" do
263+
expect(sanitize_links_and_mentions).to include("@\u200Bdependabot/reviewers")
264+
end
265+
end
236266
end
237267

238268
context "with empty text" do

0 commit comments

Comments
 (0)