Skip to content

Commit e019be9

Browse files
committed
Extended Gradle TOML support
1 parent 6e8efc9 commit e019be9

9 files changed

Lines changed: 266 additions & 11 deletions

gradle/lib/dependabot/gradle/file_fetcher.rb

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ def fetch_files
8181

8282
sig { params(root_dir: String).returns(T::Array[DependencyFile]) }
8383
def all_buildfiles_in_build(root_dir)
84-
files = [buildfile(root_dir), settings_file(root_dir), version_catalog_file(root_dir), lockfile(root_dir)]
84+
files = [buildfile(root_dir), settings_file(root_dir), lockfile(root_dir)]
8585
.compact
8686
files += wrapper_files(root_dir)
87+
files += catalog_files(root_dir)
88+
files += subproject_catalog_files(root_dir)
8789
files += subproject_buildfiles(root_dir)
8890
files += subproject_lockfiles(root_dir)
8991
files += dependency_script_plugins(root_dir)
@@ -199,13 +201,6 @@ def wrapper_files(dir)
199201
end
200202
end
201203

202-
sig { params(root_dir: String).returns(T.nilable(DependencyFile)) }
203-
def version_catalog_file(root_dir)
204-
return nil unless root_dir == "."
205-
206-
gradle_toml_file(root_dir)
207-
end
208-
209204
# rubocop:disable Metrics/PerceivedComplexity
210205
sig { params(root_dir: String).returns(T::Array[DependencyFile]) }
211206
def dependency_script_plugins(root_dir)
@@ -258,9 +253,29 @@ def buildfile(dir)
258253
file
259254
end
260255

261-
sig { params(dir: String).returns(T.nilable(DependencyFile)) }
262-
def gradle_toml_file(dir)
263-
find_first(dir, SUPPORTED_VERSION_CATALOG_FILE_PATH)
256+
sig { params(dir: String, look_at_gradle_dir: T::Boolean).returns(T::Array[DependencyFile]) }
257+
def catalog_files(dir, look_at_gradle_dir: true)
258+
files = repo_contents(dir: dir, raise_errors: false).flat_map do |f|
259+
next [f] if f.type == "file"
260+
next [] unless look_at_gradle_dir && f.type == "dir" && f.name == "gradle"
261+
262+
catalog_files(File.join(dir, f.name), look_at_gradle_dir: false)
263+
end
264+
265+
files.select { |f| f.type == "file" && f.name.end_with?(".versions.toml") }
266+
.map { |f| fetch_file_from_host(File.join(dir, f.name)) }
267+
end
268+
269+
sig { params(root_dir: String).returns(T::Array[DependencyFile]) }
270+
def subproject_catalog_files(root_dir)
271+
return [] unless settings_file(root_dir)
272+
273+
subproject_paths =
274+
SettingsFileParser
275+
.new(settings_file: T.must(settings_file(root_dir)))
276+
.subproject_paths
277+
278+
subproject_paths.flat_map { |path| catalog_files(File.join(root_dir, path), look_at_gradle_dir: false) }
264279
end
265280

266281
sig { params(dir: String).returns(T.nilable(DependencyFile)) }

gradle/spec/dependabot/gradle/file_fetcher_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def stub_no_content_request(path)
5454
stub_content_request("?ref=sha", "contents_java.json")
5555
stub_content_request("build.gradle?ref=sha", "contents_java_basic_buildfile.json")
5656
stub_no_content_request("gradle.lockfile?ref=sha")
57+
stub_no_content_request("app?ref=sha")
58+
stub_no_content_request("libs.versions.toml?ref=sha")
5759
end
5860

5961
it "fetches the buildfile" do
@@ -143,13 +145,39 @@ def stub_no_content_request(path)
143145
expect(file_fetcher_instance.files.map(&:name))
144146
.to match_array(%w(build.gradle settings.gradle app/build.gradle gradle/libs.versions.toml))
145147
end
148+
149+
context "with many catalogs" do
150+
before do
151+
stub_content_request("?ref=sha", "contents_with_tomls.json")
152+
stub_content_request("settings.gradle?ref=sha", "contents_java_simple_settings.json")
153+
stub_content_request("gradle?ref=sha", "content_gradle_toml_many.json")
154+
stub_content_request("gradle/buildSrc.versions.toml?ref=sha", "libs_versions_toml.json")
155+
stub_content_request("app?ref=sha", "content_app_toml.json")
156+
stub_content_request("app/libs.versions.toml?ref=sha", "libs_versions_toml.json")
157+
end
158+
159+
it "fetches the toml file" do
160+
expect(file_fetcher_instance.files.map(&:name))
161+
.to match_array(
162+
%w(
163+
gradle/libs.versions.toml
164+
gradle/buildSrc.versions.toml
165+
build.gradle
166+
settings.gradle
167+
app/build.gradle
168+
app/libs.versions.toml
169+
)
170+
)
171+
end
172+
end
146173
end
147174
end
148175

149176
context "with included builds" do
150177
context "when has buildSrc" do
151178
before do
152179
stub_content_request("buildSrc?ref=sha", "contents_java.json")
180+
stub_no_content_request("buildSrc/gradle?ref=sha")
153181
stub_content_request("buildSrc/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
154182
stub_no_content_request("buildSrc/gradle.lockfile?ref=sha")
155183
end
@@ -182,6 +210,7 @@ def stub_no_content_request(path)
182210
stub_content_request("?ref=sha", "contents_java_with_buildsrc_and_settings.json")
183211
stub_content_request("settings.gradle?ref=sha", "contents_java_settings_explicit_buildsrc.json")
184212
stub_content_request("included?ref=sha", "contents_java.json")
213+
stub_no_content_request("included/gradle?ref=sha")
185214
stub_content_request("included/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
186215
stub_no_content_request("included/gradle.lockfile?ref=sha")
187216
end
@@ -208,9 +237,11 @@ def stub_no_content_request(path)
208237
stub_content_request("app/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
209238
stub_no_content_request("app/gradle.lockfile?ref=sha")
210239
stub_content_request("included?ref=sha", "contents_java_with_settings.json")
240+
stub_no_content_request("included/gradle?ref=sha")
211241
stub_content_request("included/settings.gradle?ref=sha", "contents_java_simple_settings.json")
212242
stub_content_request("included/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
213243
stub_no_content_request("included/gradle.lockfile?ref=sha")
244+
stub_no_content_request("included/app?ref=sha")
214245
stub_content_request("included/app/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
215246
stub_no_content_request("included/app/gradle.lockfile?ref=sha")
216247
end
@@ -237,14 +268,18 @@ def stub_no_content_request(path)
237268
stub_content_request("app/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
238269
stub_no_content_request("app/gradle.lockfile?ref=sha")
239270
stub_content_request("included?ref=sha", "contents_java_with_settings.json")
271+
stub_no_content_request("included/gradle?ref=sha")
240272
stub_content_request("included/settings.gradle?ref=sha", "contents_java_simple_settings.json")
241273
stub_content_request("included/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
242274
stub_no_content_request("included/gradle.lockfile?ref=sha")
275+
stub_no_content_request("included/app?ref=sha")
243276
stub_content_request("included/app/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
244277
stub_no_content_request("included/app/gradle.lockfile?ref=sha")
245278
stub_content_request("included2?ref=sha", "contents_java_with_settings.json")
279+
stub_no_content_request("included2/gradle?ref=sha")
246280
stub_content_request("included2/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
247281
stub_no_content_request("included2/gradle.lockfile?ref=sha")
282+
stub_no_content_request("included2/app?ref=sha")
248283
stub_content_request("included2/settings.gradle?ref=sha", "contents_java_simple_settings.json")
249284
stub_content_request("included2/app/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
250285
stub_no_content_request("included2/app/gradle.lockfile?ref=sha")
@@ -274,24 +309,30 @@ def stub_no_content_request(path)
274309
stub_content_request("app/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
275310
stub_no_content_request("app/gradle.lockfile?ref=sha")
276311
stub_content_request("included?ref=sha", "contents_java_with_settings.json")
312+
stub_no_content_request("included/gradle?ref=sha")
277313
stub_content_request("included/settings.gradle?ref=sha", "contents_java_settings_1_included_build.json")
278314
stub_content_request("included/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
279315
stub_no_content_request("included/gradle.lockfile?ref=sha")
280316
stub_content_request("included/app/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
281317
stub_no_content_request("included/app/gradle.lockfile?ref=sha")
282318
stub_content_request("included/included?ref=sha", "contents_java_with_settings.json")
319+
stub_no_content_request("included/included/gradle?ref=sha")
283320
stub_content_request("included/included/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
284321
stub_no_content_request("included/included/gradle.lockfile?ref=sha")
285322
stub_content_request(
286323
"included/included/settings.gradle?ref=sha",
287324
"contents_java_settings_1_included_build.json"
288325
)
326+
stub_no_content_request("included/app?ref=sha")
289327
stub_content_request("included/included/app/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
290328
stub_no_content_request("included/included/app/gradle.lockfile?ref=sha")
291329
stub_content_request("included/included/included?ref=sha", "contents_java_with_buildsrc.json")
330+
stub_no_content_request("included/included/included/gradle?ref=sha")
292331
stub_content_request("included/included/included/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
293332
stub_no_content_request("included/included/included/gradle.lockfile?ref=sha")
333+
stub_no_content_request("included/included/app?ref=sha")
294334
stub_content_request("included/included/included/buildSrc?ref=sha", "contents_java.json")
335+
stub_no_content_request("included/included/included/buildSrc/gradle?ref=sha")
295336
stub_content_request(
296337
"included/included/included/buildSrc/build.gradle?ref=sha",
297338
"contents_java_basic_buildfile.json"
@@ -325,6 +366,7 @@ def stub_no_content_request(path)
325366
stub_content_request("app/build.gradle?ref=sha", "contents_java_basic_buildfile.json")
326367
stub_no_content_request("app/gradle.lockfile?ref=sha")
327368
stub_content_request("included?ref=sha", "contents_java.json")
369+
stub_no_content_request("included/gradle?ref=sha")
328370
stub_content_request("included/build.gradle?ref=sha", "contents_java_buildfile_with_script_plugins.json")
329371
stub_no_content_request("included/gradle.lockfile?ref=sha")
330372
stub_content_request("included/gradle/dependencies.gradle?ref=sha", "contents_java_simple_settings.json")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[
2+
{
3+
"name": "build.gradle",
4+
"path": "app/build.gradle",
5+
"sha": "f036f9f0357a50b02a422923f0bb0c3719eaf8ae",
6+
"size": 2355,
7+
"url": "https://api.github.com/repos/dependabot-fixtures/gradle/contents/build.gradle?ref=main",
8+
"html_url": "https://github.com/dependabot-fixtures/gradle/blob/main/build.gradle",
9+
"git_url": "https://api.github.com/repos/dependabot-fixtures/gradle/git/blobs/f036f9f0357a50b02a422923f0bb0c3719eaf8ae",
10+
"download_url": "https://raw.githubusercontent.com/dependabot-fixtures/gradle/main/build.gradle",
11+
"type": "file",
12+
"_links": {
13+
"self": "https://api.github.com/repos/dependabot-fixtures/gradle/contents/build.gradle?ref=main",
14+
"git": "https://api.github.com/repos/dependabot-fixtures/gradle/git/blobs/f036f9f0357a50b02a422923f0bb0c3719eaf8ae",
15+
"html": "https://github.com/dependabot-fixtures/gradle/blob/main/build.gradle"
16+
}
17+
},
18+
{
19+
"name": "libs.versions.toml",
20+
"path": "app/libs.versions.toml",
21+
"sha": "61e2017c7cfb111d2c1ada6dc9508711ae02b5c1",
22+
"size": 1470,
23+
"url": "https://api.github.com/repos/bigandroidenergies/version_catalog/contents/app/libs.versions.toml?ref=main",
24+
"html_url": "https://github.com/bigandroidenergies/version_catalog/blob/main/app/libs.versions.toml",
25+
"git_url": "https://api.github.com/repos/bigandroidenergies/version_catalog/git/blobs/61e2017c7cfb111d2c1ada6dc9508711ae02b5c1",
26+
"download_url": "https://raw.githubusercontent.com/bigandroidenergies/version_catalog/main/app/libs.versions.toml",
27+
"type": "file",
28+
"_links": {
29+
"self": "https://api.github.com/repos/bigandroidenergies/version_catalog/contents/app/libs.versions.toml?ref=main",
30+
"git": "https://api.github.com/repos/bigandroidenergies/version_catalog/git/blobs/61e2017c7cfb111d2c1ada6dc9508711ae02b5c1",
31+
"html": "https://github.com/bigandroidenergies/version_catalog/blob/main/app/libs.versions.toml"
32+
}
33+
}
34+
]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[
2+
{
3+
"name": "libs.versions.toml",
4+
"path": "gradle/libs.versions.toml",
5+
"sha": "61e2017c7cfb111d2c1ada6dc9508711ae02b5c1",
6+
"size": 1470,
7+
"url": "https://api.github.com/repos/bigandroidenergies/version_catalog/contents/gradle/libs.versions.toml?ref=main",
8+
"html_url": "https://github.com/bigandroidenergies/version_catalog/blob/main/gradle/libs.versions.toml",
9+
"git_url": "https://api.github.com/repos/bigandroidenergies/version_catalog/git/blobs/61e2017c7cfb111d2c1ada6dc9508711ae02b5c1",
10+
"download_url": "https://raw.githubusercontent.com/bigandroidenergies/version_catalog/main/gradle/libs.versions.toml",
11+
"type": "file",
12+
"_links": {
13+
"self": "https://api.github.com/repos/bigandroidenergies/version_catalog/contents/gradle/libs.versions.toml?ref=main",
14+
"git": "https://api.github.com/repos/bigandroidenergies/version_catalog/git/blobs/61e2017c7cfb111d2c1ada6dc9508711ae02b5c1",
15+
"html": "https://github.com/bigandroidenergies/version_catalog/blob/main/gradle/libs.versions.toml"
16+
}
17+
},
18+
{
19+
"name": "buildSrc.versions.toml",
20+
"path": "gradle/buildSrc.versions.toml",
21+
"sha": "61e2017c7cfb111d2c1ada6dc9508711ae02b5c1",
22+
"size": 1470,
23+
"url": "https://api.github.com/repos/bigandroidenergies/version_catalog/contents/gradle/buildSrc.versions.toml?ref=main",
24+
"html_url": "https://github.com/bigandroidenergies/version_catalog/blob/main/gradle/buildSrc.versions.toml",
25+
"git_url": "https://api.github.com/repos/bigandroidenergies/version_catalog/git/blobs/61e2017c7cfb111d2c1ada6dc9508711ae02b5c1",
26+
"download_url": "https://raw.githubusercontent.com/bigandroidenergies/version_catalog/main/gradle/buildSrc.versions.toml",
27+
"type": "file",
28+
"_links": {
29+
"self": "https://api.github.com/repos/bigandroidenergies/version_catalog/contents/gradle/buildSrc.versions.toml?ref=main",
30+
"git": "https://api.github.com/repos/bigandroidenergies/version_catalog/git/blobs/61e2017c7cfb111d2c1ada6dc9508711ae02b5c1",
31+
"html": "https://github.com/bigandroidenergies/version_catalog/blob/main/gradle/buildSrc.versions.toml"
32+
}
33+
}
34+
]

gradle/spec/fixtures/github/contents_java.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515
"html": "https://github.com/dependabot-fixtures/gradle/blob/main/.gitignore"
1616
}
1717
},
18+
{
19+
"name": "gradle",
20+
"path": "gradle",
21+
"sha": "193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
22+
"size": 0,
23+
"url": "https://api.github.com/repos/dependabot-fixtures/gradle/contents/gradle?ref=main",
24+
"html_url": "https://github.com/dependabot-fixtures/gradle/tree/main/gradle",
25+
"git_url": "https://api.github.com/repos/dependabot-fixtures/gradle/git/trees/193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
26+
"download_url": null,
27+
"type": "dir",
28+
"_links": {
29+
"self": "https://api.github.com/repos/dependabot-fixtures/gradle/contents/gradle?ref=main",
30+
"git": "https://api.github.com/repos/dependabot-fixtures/gradle/git/trees/193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
31+
"html": "https://github.com/dependabot-fixtures/gradle/tree/main/gradle"
32+
}
33+
},
1834
{
1935
"name": "build.gradle",
2036
"path": "build.gradle",

gradle/spec/fixtures/github/contents_java_only_settings.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515
"html": "https://github.com/dependabot-fixtures/gradle-with-settings/blob/main/.gitignore"
1616
}
1717
},
18+
{
19+
"name": "gradle",
20+
"path": "gradle",
21+
"sha": "193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
22+
"size": 0,
23+
"url": "https://api.github.com/repos/dependabot-fixtures/gradle/contents/gradle?ref=main",
24+
"html_url": "https://github.com/dependabot-fixtures/gradle/tree/main/gradle",
25+
"git_url": "https://api.github.com/repos/dependabot-fixtures/gradle/git/trees/193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
26+
"download_url": null,
27+
"type": "dir",
28+
"_links": {
29+
"self": "https://api.github.com/repos/dependabot-fixtures/gradle/contents/gradle?ref=main",
30+
"git": "https://api.github.com/repos/dependabot-fixtures/gradle/git/trees/193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
31+
"html": "https://github.com/dependabot-fixtures/gradle/tree/main/gradle"
32+
}
33+
},
1834
{
1935
"name": "dependencies",
2036
"path": "dependencies",

gradle/spec/fixtures/github/contents_java_with_buildsrc.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515
"html": "https://github.com/git/git/blob/main/build.gradle"
1616
}
1717
},
18+
{
19+
"name": "gradle",
20+
"path": "gradle",
21+
"sha": "193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
22+
"size": 0,
23+
"url": "https://api.github.com/repos/dependabot-fixtures/gradle/contents/gradle?ref=main",
24+
"html_url": "https://github.com/dependabot-fixtures/gradle/tree/main/gradle",
25+
"git_url": "https://api.github.com/repos/dependabot-fixtures/gradle/git/trees/193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
26+
"download_url": null,
27+
"type": "dir",
28+
"_links": {
29+
"self": "https://api.github.com/repos/dependabot-fixtures/gradle/contents/gradle?ref=main",
30+
"git": "https://api.github.com/repos/dependabot-fixtures/gradle/git/trees/193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
31+
"html": "https://github.com/dependabot-fixtures/gradle/tree/main/gradle"
32+
}
33+
},
1834
{
1935
"name": "buildSrc",
2036
"path": "buildSrc",

gradle/spec/fixtures/github/contents_java_with_settings.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515
"html": "https://github.com/dependabot-fixtures/gradle-with-settings/blob/main/.gitignore"
1616
}
1717
},
18+
{
19+
"name": "gradle",
20+
"path": "gradle",
21+
"sha": "193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
22+
"size": 0,
23+
"url": "https://api.github.com/repos/dependabot-fixtures/gradle-with-settings/contents/gradle?ref=main",
24+
"html_url": "https://github.com/dependabot-fixtures/gradle-with-settings/tree/main/gradle",
25+
"git_url": "https://api.github.com/repos/dependabot-fixtures/gradle-with-settings/git/trees/193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
26+
"download_url": null,
27+
"type": "dir",
28+
"_links": {
29+
"self": "https://api.github.com/repos/dependabot-fixtures/gradle-with-settings/contents/gradle?ref=main",
30+
"git": "https://api.github.com/repos/dependabot-fixtures/gradle-with-settings/git/trees/193ef2b05a8976c5ff11d575bc3f00b74dd1955e",
31+
"html": "https://github.com/dependabot-fixtures/gradle-with-settings/tree/main/gradle"
32+
}
33+
},
1834
{
1935
"name": "build.gradle",
2036
"path": "build.gradle",

0 commit comments

Comments
 (0)