Skip to content

Commit 0ec8201

Browse files
Merge pull request #26 from scanoss/v0.10.1
V0.10.1
2 parents c7a815d + e9264cb commit 0ec8201

File tree

8 files changed

+72
-25
lines changed

8 files changed

+72
-25
lines changed

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Upcoming changes...
1313

14+
## [0.10.1] - 2025-02-20
15+
### Added
16+
- Add support to custom filtering rules
17+
- Change logic on remove rule. Mark file as non match instead of deleting the key.
18+
1419
## [0.10.0] - 2025-02-17
1520
### Added
1621
- Add support to skip rule
@@ -109,4 +114,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
109114
[0.8.0]: https://github.com/scanoss/scanoss.java/compare/v0.7.1...v0.8.0
110115
[0.8.1]: https://github.com/scanoss/scanoss.java/compare/v0.8.0...v0.8.1
111116
[0.9.0]: https://github.com/scanoss/scanoss.java/compare/v0.8.1...v0.9.0
112-
[0.10.0]: https://github.com/scanoss/scanoss.java/compare/v0.9.0...v0.10.0
117+
[0.10.0]: https://github.com/scanoss/scanoss.java/compare/v0.9.0...v0.10.0
118+
[0.10.1]: https://github.com/scanoss/scanoss.java/compare/v0.10.0...v0.10.1

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.scanoss</groupId>
88
<artifactId>scanoss</artifactId>
9-
<version>0.10.0</version>
9+
<version>0.10.1</version>
1010
<packaging>jar</packaging>
1111
<name>scanoss.java</name>
1212
<url>https://github.com/scanoss/scanoss.java</url>

src/main/java/com/scanoss/Scanner.java

-11
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,6 @@ public class Scanner {
107107
private Predicate<Path> fileFilter;
108108
private Predicate<Path> folderFilter;
109109

110-
//TODO: Once this Lombok PR is merged https://github.com/projectlombok/lombok/pull/3723#pullrequestreview-2617412643
111-
// Update Lombok dependency
112-
public static class ScannerBuilder {
113-
private ScannerBuilder folderFilter(Predicate<Path> folderFilter) {
114-
return this;
115-
}
116-
private ScannerBuilder fileFilter(Predicate<Path> fileFilter) {
117-
return this;
118-
}
119-
}
120-
121110
@SuppressWarnings("unused")
122111
private Scanner(Boolean skipSnippets, Boolean allExtensions, Boolean obfuscate, Boolean hpsm,
123112
Boolean hiddenFilesFolders, Boolean allFolders, Integer numThreads, Duration timeout,

src/main/java/com/scanoss/ScannerPostProcessor.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,22 @@ private ScanFileDetails createUpdatedResultDetails(ScanFileDetails existingCompo
266266
.build();
267267
}
268268

269+
/**
270+
* Marks all components in the list as non-matching by replacing each component
271+
* with a new instance that has MatchType.NONE while preserving the serverDetails
272+
* Modifies the input list in place using List.replaceAll().
273+
*
274+
* @param components List of scan file details to be marked as non-matching
275+
*/
276+
private void markComponentsAsNonMatch(List<ScanFileDetails> components) {
277+
components.replaceAll(component ->
278+
ScanFileDetails.builder()
279+
.matchType(MatchType.none)
280+
.serverDetails(component.getServerDetails())
281+
.build()
282+
);
283+
}
284+
269285
/**
270286
* Applies remove rules to scan results, filtering out matches based on certain criteria.
271287
* <p>
@@ -281,7 +297,9 @@ private ScanFileDetails createUpdatedResultDetails(ScanFileDetails existingCompo
281297
*/
282298
public void applyRemoveRules(@NonNull List<ScanFileResult> results, @NonNull List<RemoveRule> rules) {
283299
log.debug("Starting remove rules application to {} results", results.size());
284-
results.removeIf(result -> matchesRemovalCriteria(result, rules));
300+
results.stream()
301+
.filter(result -> matchesRemovalCriteria(result, rules))
302+
.forEach(result -> markComponentsAsNonMatch(result.getFileDetails()));
285303
log.debug("Remove rules application completed. Results remaining: {}", results.size());
286304
}
287305

src/main/java/com/scanoss/filters/BaseFilter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ protected BaseFilter(FilterConfig config) {
4848
this.config = config;
4949
this.gitIgnoreFilter = GitIgnoreFilter.builder().patterns(config.getGitIgnorePatterns()).build();
5050
this.antFilter = AntFilter.builder().patterns(config.getAntPatterns()).build();
51-
this.baseSkipFilter = this.antFilter.get().or(this.gitIgnoreFilter.get());
51+
this.baseSkipFilter = this.antFilter.get().or(this.gitIgnoreFilter.get()).or(this.config.getCustomFilter());
5252
}
5353
}

src/main/java/com/scanoss/filters/FilterConfig.java

+6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424

2525
import lombok.Builder;
2626
import lombok.Getter;
27+
28+
import java.nio.file.Path;
2729
import java.util.ArrayList;
2830
import java.util.List;
31+
import java.util.function.Predicate;
2932

3033
/**
3134
* Configuration class for the SCANOSS filtering system that defines various filtering rules and patterns.
@@ -51,4 +54,7 @@ public class FilterConfig {
5154

5255
@Builder.Default
5356
private final Boolean allExtensions = false;
57+
58+
@Builder.Default
59+
private final Predicate<Path> customFilter = path -> false;
5460
}

src/test/java/com/scanoss/TestScanner.java

+23
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,27 @@ public void TestScannerSkipAntPattern() {
358358

359359
log.info("Finished {} -->", methodName);
360360
}
361+
362+
@Test
363+
public void TestScannerCustomFilterConfig() {
364+
String methodName = new Object() {
365+
}.getClass().getEnclosingMethod().getName();
366+
log.info("<-- Starting {}", methodName);
367+
368+
String f;
369+
List<String> wfps;
370+
371+
log.info("Testing GitIgnore Pattern: ");
372+
f = "testing/data/folder-ends-with-nbproject";
373+
374+
FilterConfig filterConfig = FilterConfig.builder()
375+
.customFilter(p -> true)
376+
.build();
377+
378+
Scanner scanner = Scanner.builder().filterConfig(filterConfig).build();
379+
wfps = scanner.wfpFolder(f);
380+
assertTrue("There is no fingerprint since custom filter it's always true", wfps.isEmpty());
381+
382+
log.info("Finished {} -->", methodName);
383+
}
361384
}

src/test/java/com/scanoss/TestScannerPostProcessor.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.google.gson.JsonObject;
2626
import com.scanoss.dto.ScanFileResult;
27+
import com.scanoss.dto.enums.MatchType;
2728
import com.scanoss.settings.Bom;
2829
import com.scanoss.settings.RemoveRule;
2930
import com.scanoss.settings.ReplaceRule;
@@ -73,6 +74,16 @@ public void Setup() throws URISyntaxException, IOException {
7374

7475
}
7576

77+
private Boolean hasOnlyNonMatchingDetailsForPath(List<ScanFileResult> results, String path) {
78+
Optional<ScanFileResult> r = results.stream().filter(result -> result.getFilePath().matches(path)).findFirst();
79+
80+
if (r.isEmpty()) {
81+
fail(String.format("Should have found file %s", path));
82+
}
83+
84+
return r.get().getFileDetails().stream().noneMatch(c -> c.getMatchType() != MatchType.none);
85+
86+
}
7687

7788
/**
7889
* TESTING REMOVE RULES
@@ -94,7 +105,7 @@ public void TestRemoveRuleWithPathAndPurl() {
94105
List<ScanFileResult> results = scannerPostProcessor.process(sampleScanResults, bom);
95106

96107
// Verify
97-
assertEquals("Should have one result less after removal", sampleScanResults.size() - 1, results.size());
108+
assertTrue(hasOnlyNonMatchingDetailsForPath(results, "CMSsite/admin/js/npm.js"));
98109
log.info("Finished {} -->", methodName);
99110
}
100111

@@ -116,13 +127,7 @@ public void TestRemoveRuleWithPurlOnly() {
116127
// Process results
117128
List<ScanFileResult> results = scannerPostProcessor.process(sampleScanResults, bom);
118129

119-
// Verify
120-
assertEquals("Size should decrease by 1 after removal",
121-
sampleScanResults.size() - 1,
122-
results.size());
123-
124-
assertFalse("Should remove file CMSsite/admin/js/npm.js",
125-
results.stream().anyMatch(r -> r.getFilePath().matches("CMSsite/admin/js/npm.js")));
130+
assertTrue(hasOnlyNonMatchingDetailsForPath(results, "CMSsite/admin/js/npm.js"));
126131

127132
log.info("Finished {} -->", methodName);
128133
}
@@ -258,7 +263,7 @@ public void testRemoveRuleWithOverlappingLineRanges() {
258263
List<ScanFileResult> results = scannerPostProcessor.process(sampleScanResults, bom);
259264

260265
// Verify - should remove because lines overlap
261-
assertEquals("Should have one result less after removal", sampleScanResults.size() - 1, results.size());
266+
assertTrue(hasOnlyNonMatchingDetailsForPath(results, "src/spdx.c"));
262267

263268
log.info("Finished {} -->", methodName);
264269
}
@@ -289,7 +294,7 @@ public void testMultipleRemoveRulesWithMixedLineRanges() {
289294
// Process results
290295
List<ScanFileResult> results = scannerPostProcessor.process(sampleScanResults, bom);
291296

292-
assertEquals("Should have one result less after removal", sampleScanResults.size() - 1, results.size());
297+
assertTrue(hasOnlyNonMatchingDetailsForPath(results, "src/spdx.c"));
293298

294299
log.info("Finished {} -->", methodName);
295300
}

0 commit comments

Comments
 (0)