Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
fix: 🐛 don't add empty files to bundles [ROAD-612] (#29)
Browse files Browse the repository at this point in the history
* fix: 🐛 don't add empty files to bundles

* refactor: ♻️ move empty file check to DeepCodeUtilsBase.isSupportedFileFormat [ROAD-612]

* refactor: ⚡f️moved file size check down for performance reasons [ROAD-612]

* refactor: ⚡ adjusted filesize check to only recommendations from PR [ROAD-612]
  • Loading branch information
bastiandoetsch authored Dec 14, 2021
1 parent 6c539df commit e19252b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [2.2.1] - 2020-12-10
- fix: don't upload empty files

## [2.2.0] - 2020-11-29
- feat: update to latest snyk-code api

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

group = "io.snyk.code.sdk"
archivesBaseName = "snyk-code-client"
version = "2.2.0"
version = "2.2.1"

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,11 @@ private List<String> createBundleStep(
progress, PREPARE_FILES_TEXT + fileCounter + " of " + totalFiles + " files done.");

final String path = pdUtils.getDeepCodedFilePath(file);

// info("getHash requested");
final String hash = hashContentUtils.getHash(file);
if (fileCounter == 1)
dcLogger.logInfo("First file to proceed: \npath = " + path + "\nhash = " + hash);
dcLogger.logInfo("First file to process: \npath = " + path + "\nhash = " + hash);

hashRequest.put(path, hash);
sizePath2Hash += (path.length() + hash.length()) * 2L; // rough estimation of bytes occupied
Expand Down
81 changes: 38 additions & 43 deletions src/main/java/ai/deepcode/javaclient/core/DeepCodeUtilsBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

public abstract class DeepCodeUtilsBase {
Expand All @@ -23,11 +17,11 @@ public abstract class DeepCodeUtilsBase {
private final DCLoggerBase dcLogger;

protected DeepCodeUtilsBase(
@NotNull AnalysisDataBase analysisData,
@NotNull DeepCodeParamsBase deepCodeParams,
@NotNull DeepCodeIgnoreInfoHolderBase ignoreInfoHolder,
@NotNull PlatformDependentUtilsBase pdUtils,
@NotNull DCLoggerBase dcLogger) {
@NotNull AnalysisDataBase analysisData,
@NotNull DeepCodeParamsBase deepCodeParams,
@NotNull DeepCodeIgnoreInfoHolderBase ignoreInfoHolder,
@NotNull PlatformDependentUtilsBase pdUtils,
@NotNull DCLoggerBase dcLogger) {
this.analysisData = analysisData;
this.deepCodeParams = deepCodeParams;
this.ignoreInfoHolder = ignoreInfoHolder;
Expand All @@ -40,7 +34,7 @@ protected DeepCodeUtilsBase(
protected static Set<String> supportedConfigFiles = Collections.emptySet();

public List<Object> getAllSupportedFilesInProject(
@NotNull Object project, boolean scanAllMissedIgnoreFile, @Nullable Object progress) {
@NotNull Object project, boolean scanAllMissedIgnoreFile, @Nullable Object progress) {
final Collection<Object> allProjectFiles = allProjectFiles(project);
if (allProjectFiles.isEmpty()) {
dcLogger.logWarn("Empty files list for project: " + project);
Expand All @@ -55,7 +49,7 @@ public List<Object> getAllSupportedFilesInProject(
final List<Object> result = new ArrayList<>();
for (Object file : allProjectFiles) {
pdUtils.progressSetText(
progress, "Checked if supported " + counter + " files of " + totalSize);
progress, "Checked if supported " + counter + " files of " + totalSize);
pdUtils.progressSetFraction(progress, ((double) counter++ / totalSize));
if (isSupportedFileFormat(file)) {
result.add(file);
Expand All @@ -77,12 +71,11 @@ public List<Object> getAllSupportedFilesInProject(
public boolean isSupportedFileFormat(@NotNull Object file) {
// DCLogger.getInstance().info("isSupportedFileFormat started for " + psiFile.getName());
if (ignoreInfoHolder.isIgnoredFile(file) || isGitIgnoredExternalCheck(file)) return false;
final boolean result =
getFileLength(file) < MAX_FILE_SIZE
&& (supportedExtensions.contains(getFileExtention(file))
|| supportedConfigFiles.contains(pdUtils.getFileName(file)));
long fileLength = getFileLength(file);
boolean supported = 0 < fileLength && fileLength < MAX_FILE_SIZE &&
(supportedExtensions.contains(getFileExtention(file)) || supportedConfigFiles.contains(pdUtils.getFileName(file)));
// DCLogger.getInstance().info("isSupportedFileFormat ends for " + psiFile.getName());
return result;
return supported;
}

protected abstract long getFileLength(@NotNull Object file);
Expand All @@ -91,41 +84,43 @@ public boolean isSupportedFileFormat(@NotNull Object file) {

protected abstract boolean isGitIgnoredExternalCheck(@NotNull Object file);

/** Potentially <b>Heavy</b> network request! */
/**
* Potentially <b>Heavy</b> network request!
*/
private void initSupportedExtentionsAndConfigFiles() {
GetFiltersResponse filtersResponse =
DeepCodeRestApi.getFilters(deepCodeParams.getSessionToken());
DeepCodeRestApi.getFilters(deepCodeParams.getSessionToken());
if (filtersResponse.getStatusCode() == 200) {
supportedExtensions =
filtersResponse.getExtensions().stream()
.map(s -> s.substring(1)) // remove preceding `.` (`.js` -> `js`)
.collect(Collectors.toSet());
filtersResponse.getExtensions().stream()
.map(s -> s.substring(1)) // remove preceding `.` (`.js` -> `js`)
.collect(Collectors.toSet());
supportedConfigFiles = new HashSet<>(filtersResponse.getConfigFiles());
dcLogger.logInfo("Supported extensions: " + supportedExtensions);
dcLogger.logInfo("Supported configFiles: " + supportedConfigFiles);
} else {
dcLogger.logWarn(
"Can't retrieve supported file extensions and config files from the server. Fallback to default set.\n"
+ filtersResponse.getStatusCode()
+ " "
+ filtersResponse.getStatusDescription());
"Can't retrieve supported file extensions and config files from the server. Fallback to default set.\n"
+ filtersResponse.getStatusCode()
+ " "
+ filtersResponse.getStatusDescription());
supportedExtensions =
new HashSet<>(
Arrays.asList(
"cc", "htm", "cpp", "cxx", "c", "vue", "h", "hpp", "hxx", "es6", "js", "py", "es",
"jsx", "java", "tsx", "html", "ts"));
new HashSet<>(
Arrays.asList(
"cc", "htm", "cpp", "cxx", "c", "vue", "h", "hpp", "hxx", "es6", "js", "py", "es",
"jsx", "java", "tsx", "html", "ts"));
supportedConfigFiles =
new HashSet<>(
Arrays.asList(
"pylintrc",
"ruleset.xml",
".eslintrc.json",
".pylintrc",
".eslintrc.js",
"tslint.json",
".pmdrc.xml",
".ruleset.xml",
".eslintrc.yml"));
new HashSet<>(
Arrays.asList(
"pylintrc",
"ruleset.xml",
".eslintrc.json",
".pylintrc",
".eslintrc.js",
"tslint.json",
".pmdrc.xml",
".ruleset.xml",
".eslintrc.yml"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void removeProjectHashContent(@NotNull Object project) {

// ?? com.intellij.openapi.util.text.StringUtil.toHexString
// https://www.baeldung.com/sha-256-hashing-java#message-digest
@SuppressWarnings("DuplicatedCode") // it's the test code that triggers the warning
private static String bytesToHex(byte[] hash) {
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
Expand Down

0 comments on commit e19252b

Please sign in to comment.