Skip to content

Commit cbd0875

Browse files
authored
Merge pull request #5863 from dependabot/jurre/yarn-berry-registry-finder-support
Yarn Berry: Ensure registry config is respected
2 parents 4b27eaa + 88f5ddf commit cbd0875

File tree

10 files changed

+207
-10
lines changed

10 files changed

+207
-10
lines changed

npm_and_yarn/lib/dependabot/npm_and_yarn/file_updater/npm_lockfile_updater.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,9 @@ def handle_missing_package(package_name, error_message)
429429
reg = NpmAndYarn::UpdateChecker::RegistryFinder.new(
430430
dependency: missing_dep,
431431
credentials: credentials,
432-
npmrc_file: dependency_files.
433-
find { |f| f.name.end_with?(".npmrc") },
434-
yarnrc_file: dependency_files.
435-
find { |f| f.name.end_with?(".yarnrc") }
432+
npmrc_file: dependency_files. find { |f| f.name.end_with?(".npmrc") },
433+
yarnrc_file: dependency_files. find { |f| f.name.end_with?(".yarnrc") },
434+
yarnrc_yml_file: dependency_files.find { |f| f.name.end_with?(".yarnrc.yml") }
436435
).registry
437436

438437
return if UpdateChecker::RegistryFinder.central_registry?(reg) && !package_name.start_with?("@")

npm_and_yarn/lib/dependabot/npm_and_yarn/file_updater/yarn_lockfile_updater.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ def handle_missing_package(package_name, error_message, yarn_lock)
474474
dependency: missing_dep,
475475
credentials: credentials,
476476
npmrc_file: npmrc_file,
477-
yarnrc_file: yarnrc_file
477+
yarnrc_file: yarnrc_file,
478+
yarnrc_yml_file: yarnrc_yml_file
478479
).registry
479480

480481
return if UpdateChecker::RegistryFinder.central_registry?(reg) && !package_name.start_with?("@")
@@ -578,6 +579,10 @@ def yarnrc_file
578579
def npmrc_file
579580
dependency_files.find { |f| f.name == ".npmrc" }
580581
end
582+
583+
def yarnrc_yml_file
584+
dependency_files.find { |f| f.name.end_with?(".yarnrc.yml") }
585+
end
581586
end
582587
end
583588
end

npm_and_yarn/lib/dependabot/npm_and_yarn/update_checker/latest_version_finder.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ def registry_finder
371371
dependency: dependency,
372372
credentials: credentials,
373373
npmrc_file: npmrc_file,
374-
yarnrc_file: yarnrc_file
374+
yarnrc_file: yarnrc_file,
375+
yarnrc_yml_file: yarnrc_yml_file
375376
)
376377
end
377378

@@ -395,6 +396,10 @@ def yarnrc_file
395396
dependency_files.find { |f| f.name.end_with?(".yarnrc") }
396397
end
397398

399+
def yarnrc_yml_file
400+
dependency_files.find { |f| f.name.end_with?(".yarnrc.yml") }
401+
end
402+
398403
# TODO: Remove need for me
399404
def git_dependency?
400405
# ignored_version/raise_on_ignored are irrelevant.

npm_and_yarn/lib/dependabot/npm_and_yarn/update_checker/library_detector.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def registry
6565
dependency: nil,
6666
credentials: credentials,
6767
npmrc_file: dependency_files.find { |f| f.name.end_with?(".npmrc") },
68-
yarnrc_file: dependency_files.find { |f| f.name.end_with?(".yarnrc") }
68+
yarnrc_file: dependency_files.find { |f| f.name.end_with?(".yarnrc") },
69+
yarnrc_yml_file: dependency_files.find { |f| f.name.end_with?(".yarnrc.yml") }
6970
).registry_from_rc(project_name)
7071
end
7172
end

npm_and_yarn/lib/dependabot/npm_and_yarn/update_checker/registry_finder.rb

+17-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ class RegistryFinder
2525
/['"](?<scope>@[^:]+):registry['"]\s((['"](?<registry>.*)['"])|(?<registry>.*))/.freeze
2626

2727
def initialize(dependency:, credentials:, npmrc_file: nil,
28-
yarnrc_file: nil)
28+
yarnrc_file: nil, yarnrc_yml_file: nil)
2929
@dependency = dependency
3030
@credentials = credentials
3131
@npmrc_file = npmrc_file
3232
@yarnrc_file = yarnrc_file
33+
@yarnrc_yml_file = yarnrc_yml_file
3334
end
3435

3536
def registry
@@ -59,7 +60,7 @@ def registry_from_rc(dependency_name)
5960

6061
private
6162

62-
attr_reader :dependency, :credentials, :npmrc_file, :yarnrc_file
63+
attr_reader :dependency, :credentials, :npmrc_file, :yarnrc_file, :yarnrc_yml_file
6364

6465
def first_registry_with_dependency_details
6566
@first_registry_with_dependency_details ||=
@@ -214,6 +215,8 @@ def global_registry
214215
return Regexp.last_match[:registry].strip
215216
end
216217

218+
return parsed_yarnrc_yml["npmRegistryServer"] if parsed_yarnrc_yml&.key?("npmRegistryServer")
219+
217220
"https://registry.npmjs.org"
218221
end
219222

@@ -230,6 +233,11 @@ def scoped_registry(scope)
230233
return Regexp.last_match[:registry].strip
231234
end
232235

236+
if parsed_yarnrc_yml
237+
yarn_berry_registry = parsed_yarnrc_yml.dig("npmScopes", scope.delete_prefix("@"), "npmRegistryServer")
238+
return yarn_berry_registry if yarn_berry_registry
239+
end
240+
233241
global_registry
234242
end
235243

@@ -245,6 +253,13 @@ def registry_source_url
245253

246254
sources.find { |s| s[:type] == "registry" }&.fetch(:url)
247255
end
256+
257+
def parsed_yarnrc_yml
258+
return unless yarnrc_yml_file
259+
return @parsed_yarnrc_yml if defined? @parsed_yarnrc_yml
260+
261+
@parsed_yarnrc_yml = YAML.safe_load(yarnrc_yml_file.content)
262+
end
248263
end
249264
end
250265
end

npm_and_yarn/spec/dependabot/npm_and_yarn/update_checker/registry_finder_spec.rb

+53-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
dependency: dependency,
1010
credentials: credentials,
1111
npmrc_file: npmrc_file,
12-
yarnrc_file: yarnrc_file
12+
yarnrc_file: yarnrc_file,
13+
yarnrc_yml_file: yarnrc_yml_file
1314
)
1415
end
1516
let(:npmrc_file) { nil }
1617
let(:yarnrc_file) { nil }
18+
let(:yarnrc_yml_file) { nil }
1719
let(:credentials) do
1820
[{
1921
"type" => "git_source",
@@ -62,6 +64,14 @@
6264
it { is_expected.to eq("http://example.com") }
6365
end
6466

67+
context "with a global yarn berry registry" do
68+
let(:yarnrc_yml_file) do
69+
Dependabot::DependencyFile.new(name: ".yarnrc.yml", content: 'npmRegistryServer: "https://example.com"')
70+
end
71+
72+
it { is_expected.to eq("https://example.com") }
73+
end
74+
6575
context "with a scoped npm registry" do
6676
let(:dependency_name) { "@dependabot/some_dep" }
6777
let(:npmrc_file) { Dependabot::DependencyFile.new(name: ".npmrc", content: "@dependabot:registry=http://example.com") }
@@ -82,6 +92,20 @@
8292

8393
it { is_expected.to eq("http://example.com") }
8494
end
95+
96+
context "with a scoped yarn berry registry" do
97+
let(:dependency_name) { "@dependabot/some_dep" }
98+
let(:yarnrc_yml_content) do
99+
<<~YARNRC
100+
npmScopes:
101+
dependabot:
102+
npmRegistryServer: "https://example.com"
103+
YARNRC
104+
end
105+
let(:yarnrc_yml_file) { Dependabot::DependencyFile.new(name: ".yarnrc", content: yarnrc_yml_content) }
106+
107+
it { is_expected.to eq("https://example.com") }
108+
end
85109
end
86110

87111
describe "registry" do
@@ -221,6 +245,34 @@
221245
end
222246
end
223247

248+
context "with a .yarnrc.yml file" do
249+
let(:yarnrc_yml_file) do
250+
project_dependency_files(project_name).find { |f| f.name == ".yarnrc.yml" }
251+
end
252+
let(:project_name) { "yarn_berry/yarnrc_global_registry" }
253+
254+
before do
255+
url = "https://npm-proxy.fury.io/password/dependabot/etag"
256+
body = fixture("gemfury_responses", "gemfury_response_etag.json")
257+
258+
stub_request(:get, url).to_return(status: 200, body: body)
259+
end
260+
261+
it { is_expected.to eq("npm-proxy.fury.io/password/dependabot") }
262+
263+
context "that can't be reached" do
264+
before do
265+
url = "https://npm-proxy.fury.io/password/dependabot/etag"
266+
stub_request(:get, url).to_return(status: 401, body: "")
267+
end
268+
269+
# Since this registry is declared at the global registry, in the absence
270+
# of other information we should still us it (and *not* flaa back to
271+
# registry.npmjs.org)
272+
it { is_expected.to eq("npm-proxy.fury.io/password/dependabot") }
273+
end
274+
end
275+
224276
context "with a private registry source" do
225277
let(:source) do
226278
{ type: "registry", url: "https://npm.fury.io/dependabot" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npmRegistryServer: "https://npm-proxy.fury.io/password/dependabot/"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "{{ name }}",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/waltfy/PROTO_TEST.git"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"bugs": {
13+
"url": "https://github.com/waltfy/PROTO_TEST/issues"
14+
},
15+
"homepage": "https://github.com/waltfy/PROTO_TEST#readme",
16+
"dependencies": {
17+
"fetch-factory": "^0.0.1"
18+
},
19+
"devDependencies": {
20+
"etag": "^1.0.0"
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# This file is generated by running "yarn install" inside your project.
2+
# Manual changes might be lost - proceed with caution!
3+
4+
__metadata:
5+
version: 6
6+
cacheKey: 8
7+
8+
"encoding@npm:^0.1.11":
9+
version: 0.1.12
10+
resolution: "encoding@npm:0.1.12"
11+
dependencies:
12+
iconv-lite: ~0.4.13
13+
checksum: 96df688a93821e866bea19dd689863b1f9e07ef1c15321dde1fbcb8008ed7c785c48b248c4def01367780d2637c459b8ffa988de9647afe4200b003b1ac369ef
14+
languageName: node
15+
linkType: hard
16+
17+
"es6-promise@npm:^3.0.2":
18+
version: 3.3.1
19+
resolution: "es6-promise@npm:3.3.1"
20+
checksum: ce4044009c2b78db18b15212338eb711cd8a4d485961bc9ec18bb24e8c1e91c96d3295b0fcf63066fc0fa1b0ade36da05e6657827d4336dece382be2429b8398
21+
languageName: node
22+
linkType: hard
23+
24+
"etag@npm:^1.0.0":
25+
version: 1.7.0
26+
resolution: "etag@npm:1.7.0"
27+
checksum: a76e03c51881902070fa3ecd4bd5c5b9286657ea467ada24bf866c1bdd545d08b65191d085b70fdc859caea2d68ff99c4f6936d2fa2026fd2fcc796d013e1978
28+
languageName: node
29+
linkType: hard
30+
31+
"fetch-factory@npm:^0.0.1":
32+
version: 0.0.1
33+
resolution: "fetch-factory@npm:0.0.1"
34+
dependencies:
35+
es6-promise: ^3.0.2
36+
isomorphic-fetch: ^2.1.1
37+
lodash: ^3.10.1
38+
checksum: ff7fe6fdb8dd22080ff2d10495d0701068aac2d4d2c7c00baa675d9efa0d9b472deee7de0a60a2094446ec907833fdf0322ddaa814e1c594de5796b1e08157d9
39+
languageName: node
40+
linkType: hard
41+
42+
"iconv-lite@npm:~0.4.13":
43+
version: 0.4.15
44+
resolution: "iconv-lite@npm:0.4.15"
45+
checksum: 858ed660b795386d1ab85c43962d34519d46511d61432f6a74c1488dce2b6023f7eaec82f35f1e94eb20f2cfb36c6ad07e3814f9551a4b7c6058a162bbab382e
46+
languageName: node
47+
linkType: hard
48+
49+
"is-stream@npm:^1.0.1":
50+
version: 1.1.0
51+
resolution: "is-stream@npm:1.1.0"
52+
checksum: 063c6bec9d5647aa6d42108d4c59723d2bd4ae42135a2d4db6eadbd49b7ea05b750fd69d279e5c7c45cf9da753ad2c00d8978be354d65aa9f6bb434969c6a2ae
53+
languageName: node
54+
linkType: hard
55+
56+
"isomorphic-fetch@npm:^2.1.1":
57+
version: 2.2.1
58+
resolution: "isomorphic-fetch@npm:2.2.1"
59+
dependencies:
60+
node-fetch: ^1.0.1
61+
whatwg-fetch: ">=0.10.0"
62+
checksum: bb5daa7c3785d6742f4379a81e55b549a469503f7c9bf9411b48592e86632cf5e8fe8ea878dba185c0f33eb7c510c23abdeb55aebfdf5d3c70f031ced68c5424
63+
languageName: node
64+
linkType: hard
65+
66+
"lodash@npm:^3.10.1":
67+
version: 3.10.1
68+
resolution: "lodash@npm:3.10.1"
69+
checksum: 53065d3712a2fd90b55690c5af19f9625a5bbb2b7876ff76d782ee1dc22618fd4dff191d44a8e165a17b5b81a851c3e884d3b5b25e314422fbe24bb299542685
70+
languageName: node
71+
linkType: hard
72+
73+
"node-fetch@npm:^1.0.1":
74+
version: 1.6.3
75+
resolution: "node-fetch@npm:1.6.3"
76+
dependencies:
77+
encoding: ^0.1.11
78+
is-stream: ^1.0.1
79+
checksum: cd8e3990065538141796c4f4d67dc9a9969880d9c4b9c6ad61cd5d140d9839bb04c099d53c16910dbef77ae73f390554370e2208862754ca71e1f6e445cb52f0
80+
languageName: node
81+
linkType: hard
82+
83+
"whatwg-fetch@npm:>=0.10.0":
84+
version: 2.0.2
85+
resolution: "whatwg-fetch@npm:2.0.2"
86+
checksum: 686d167f497d894d9dddb7f91d042ab927775a9c877015945261406ad64248e464c7fcfddc5a41e400102e3f68934da4762b7b8fadced8fc126b0a88f5e3bd0f
87+
languageName: node
88+
linkType: hard
89+
90+
"{{ name }}@workspace:.":
91+
version: 0.0.0-use.local
92+
resolution: "{{ name }}@workspace:."
93+
dependencies:
94+
etag: ^1.0.0
95+
fetch-factory: ^0.0.1
96+
languageName: unknown
97+
linkType: soft

0 commit comments

Comments
 (0)