Skip to content

Commit 094f04d

Browse files
authored
feat: add gradle kotlin dsl support (#110)
## Description Add support for Gradle Kotlin DSL **Related issue:** - Fixes: [TC-2293](https://issues.redhat.com/browse/TC-2293) - Related: guacsec/trustify-da-javascript-client#157 ## Checklist - [x] I have followed this repository's contributing guidelines. - [x] I will adhere to the project's code of conduct. Signed-off-by: Ruben Romero Montes <[email protected]>
1 parent 1a7c364 commit 094f04d

File tree

102 files changed

+23465
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+23465
-52
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
target
2424
.flattened-pom.xml
2525

26+
# Gradle
27+
.gradle
28+
2629
# Cache Files
2730
*.cache
2831

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ encrypted-token-will-appear-here
7777
</li>
7878

7979
<li>
80-
<em>Gradle</em> users, add a <em>maven-type repository</em> definition in <em>build.gradle</em>
80+
<em>Gradle</em> users, add a <em>maven-type repository</em> definition in <em>build.gradle</em> (Groovy DSL) or <em>build.gradle.kts</em> (Kotlin DSL)
8181

8282
```groovy
8383
repositories {

src/main/java/com/redhat/exhort/providers/GradleProvider.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141

4242
/**
4343
* Concrete implementation of the {@link Provider} used for converting dependency trees for Gradle
44-
* projects (gradle.build) into a content Dot Graphs for Stack analysis or Json for Component
45-
* analysis.
44+
* projects (gradle.build / gradle.build.kts) into a content Dot Graphs for Stack analysis or Json
45+
* for Component analysis.
4646
*/
4747
public final class GradleProvider extends BaseJavaProvider {
4848

@@ -155,8 +155,9 @@ private String getDepInfo(String dependencyLine) {
155155

156156
private String getDepFromNotation(String dependency, Path manifestPath) throws IOException {
157157
// Extract everything after "libs."
158-
String alias = dependency.substring(dependency.indexOf("libs.") + "libs.".length());
159-
alias = alias.replace(".", "-");
158+
String alias = dependency.substring(dependency.indexOf("libs.") + "libs.".length()).trim();
159+
alias = alias.replace(".", "-").replace(")", "");
160+
160161
// Read and parse the TOML file
161162
TomlParseResult toml = Toml.parse(getLibsVersionsTomlPath(manifestPath));
162163
TomlTable librariesTable = toml.getTable("libraries");

src/main/java/com/redhat/exhort/tools/Ecosystem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public static Provider getProvider(final String manifestType) {
7575
case "requirements.txt":
7676
return new PythonPipProvider();
7777
case "build.gradle":
78+
case "build.gradle.kts":
7879
return new GradleProvider();
7980

8081
default:

src/test/java/com/redhat/exhort/impl/ExhortApiIT.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.ArrayList;
4343
import java.util.Arrays;
4444
import java.util.Collection;
45+
import java.util.List;
4546
import java.util.Map;
4647
import java.util.Optional;
4748
import java.util.Set;
@@ -54,7 +55,9 @@
5455
import org.junit.jupiter.api.Test;
5556
import org.junit.jupiter.api.extension.ExtendWith;
5657
import org.junit.jupiter.params.ParameterizedTest;
58+
import org.junit.jupiter.params.provider.Arguments;
5759
import org.junit.jupiter.params.provider.EnumSource;
60+
import org.junit.jupiter.params.provider.MethodSource;
5861
import org.mockito.MockedStatic;
5962
import org.mockito.Mockito;
6063
import org.mockito.invocation.InvocationOnMock;
@@ -85,8 +88,10 @@ static void beforeAll() {
8588
"package.json",
8689
"pypi",
8790
"requirements.txt",
88-
"gradle",
89-
"build.gradle");
91+
"gradle-groovy",
92+
"build.gradle",
93+
"gradle-kotlin",
94+
"build.gradle.kts");
9095
}
9196

9297
@Tag("IntegrationTest")
@@ -97,17 +102,21 @@ static void afterAll() {
97102
api = null;
98103
}
99104

105+
private static List<Arguments> scenarios() {
106+
return ecoSystemsManifestNames.entrySet().stream()
107+
.map(e -> Arguments.of(e.getKey(), e.getValue()))
108+
.collect(Collectors.toList());
109+
}
110+
100111
@Tag("IntegrationTest")
101-
@ParameterizedTest
102-
@EnumSource(
103-
value = Ecosystem.Type.class,
104-
names = {"GOLANG", "MAVEN", "NPM", "PYTHON", "GRADLE"})
105-
void Integration_Test_End_To_End_Stack_Analysis(Ecosystem.Type packageManager)
112+
@ParameterizedTest(name = "StackAnalysis for: {0} with manifest: {1}")
113+
@MethodSource("scenarios")
114+
void Integration_Test_End_To_End_Stack_Analysis(String useCase, String manifestFileName)
106115
throws IOException, ExecutionException, InterruptedException {
107-
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType());
116+
var provider = Ecosystem.getProvider(manifestFileName);
117+
var packageManager = provider.ecosystem;
108118
String pathToManifest =
109-
getFileFromResource(
110-
manifestFileName, "tst_manifests", "it", packageManager.getType(), manifestFileName);
119+
getFileFromResource(manifestFileName, "tst_manifests", "it", useCase, manifestFileName);
111120
preparePythonEnvironment(packageManager);
112121
// Github action runner with all maven and java versions seems to enter infinite loop in
113122
// integration tests of
@@ -126,16 +135,14 @@ private void releaseStaticMock(Ecosystem.Type packageManager) {
126135
}
127136

128137
@Tag("IntegrationTest")
129-
@ParameterizedTest
130-
@EnumSource(
131-
value = Ecosystem.Type.class,
132-
names = {"GOLANG", "MAVEN", "NPM", "PYTHON", "GRADLE"})
133-
void Integration_Test_End_To_End_Stack_Analysis_Mixed(Ecosystem.Type packageManager)
138+
@ParameterizedTest(name = "StackAnalysis Mixed for: {0} with manifest: {1}")
139+
@MethodSource("scenarios")
140+
void Integration_Test_End_To_End_Stack_Analysis_Mixed(String useCase, String manifestFileName)
134141
throws IOException, ExecutionException, InterruptedException {
135-
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType());
142+
var provider = Ecosystem.getProvider(manifestFileName);
143+
var packageManager = provider.ecosystem;
136144
String pathToManifest =
137-
getFileFromResource(
138-
manifestFileName, "tst_manifests", "it", packageManager.getType(), manifestFileName);
145+
getFileFromResource(manifestFileName, "tst_manifests", "it", useCase, manifestFileName);
139146
preparePythonEnvironment(packageManager);
140147
// Github action runner with all maven and java versions seems to enter infinite loop in
141148
// integration tests of
@@ -150,16 +157,14 @@ void Integration_Test_End_To_End_Stack_Analysis_Mixed(Ecosystem.Type packageMana
150157
}
151158

152159
@Tag("IntegrationTest")
153-
@ParameterizedTest
154-
@EnumSource(
155-
value = Ecosystem.Type.class,
156-
names = {"GOLANG", "MAVEN", "NPM", "PYTHON", "GRADLE"})
157-
void Integration_Test_End_To_End_Stack_Analysis_Html(Ecosystem.Type packageManager)
160+
@ParameterizedTest(name = "StackAnalysis HTML for: {0} with manifest: {1}")
161+
@MethodSource("scenarios")
162+
void Integration_Test_End_To_End_Stack_Analysis_Html(String useCase, String manifestFileName)
158163
throws IOException, ExecutionException, InterruptedException {
159-
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType());
164+
var provider = Ecosystem.getProvider(manifestFileName);
165+
var packageManager = provider.ecosystem;
160166
String pathToManifest =
161-
getFileFromResource(
162-
manifestFileName, "tst_manifests", "it", packageManager.getType(), manifestFileName);
167+
getFileFromResource(manifestFileName, "tst_manifests", "it", useCase, manifestFileName);
163168
preparePythonEnvironment(packageManager);
164169
// Github action runner with all maven and java versions seems to enter infinite loop in
165170
// integration tests of
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright © 2023 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.redhat.exhort.providers;
17+
18+
import org.junit.jupiter.api.extension.ExtendWith;
19+
import org.mockito.junit.jupiter.MockitoExtension;
20+
21+
@ExtendWith(HelperExtension.class)
22+
@ExtendWith(MockitoExtension.class)
23+
class Gradle_Groovy_Provider_Test extends Gradle_Provider_Test {
24+
25+
@Override
26+
String getManifestName() {
27+
return "build.gradle";
28+
}
29+
30+
@Override
31+
String getProviderFolder() {
32+
return "gradle-groovy";
33+
}
34+
35+
@Override
36+
String getSettingsName() {
37+
return "settings.gradle";
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright © 2023 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.redhat.exhort.providers;
17+
18+
import org.junit.jupiter.api.extension.ExtendWith;
19+
import org.mockito.junit.jupiter.MockitoExtension;
20+
21+
@ExtendWith(HelperExtension.class)
22+
@ExtendWith(MockitoExtension.class)
23+
class Gradle_Kotlin_Provider_Test extends Gradle_Provider_Test {
24+
25+
@Override
26+
String getManifestName() {
27+
return "build.gradle.kts";
28+
}
29+
30+
@Override
31+
String getProviderFolder() {
32+
return "gradle-kotlin";
33+
}
34+
35+
@Override
36+
String getSettingsName() {
37+
return "settings.gradle.kts";
38+
}
39+
}

0 commit comments

Comments
 (0)