From d873719deb78770491e6a42e191bab6a108a632e Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Sat, 13 Jan 2024 17:34:57 +0100 Subject: [PATCH] ZIP all source files before copying into build folder. --- .../core/model/PropertyStatistics.java | 14 ++-- .../core/util/AffectedFilesResolver.java | 70 +++++++++++++++++-- 2 files changed, 70 insertions(+), 14 deletions(-) diff --git a/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java b/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java index 7669696a42..2181260f92 100644 --- a/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java +++ b/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java @@ -35,7 +35,7 @@ public class PropertyStatistics { * @param property * the property to show the details for * @param propertyFormatter - * the formatter that show the property + * the formatter that shows the property */ PropertyStatistics(final Report report, final Report newIssues, final String property, final Function propertyFormatter) { @@ -57,9 +57,9 @@ public int getTotal() { } /** - * Returns the amount of issues introduced since the last build. + * Returns the number of issues introduced since the last build. * - * @return the amount of new issues + * @return the number of new issues */ public int getTotalNewIssues() { return totalNewIssues; @@ -151,7 +151,7 @@ public long getNewCount(final String key) { * @param key * the property instance * - * @return the number of high severity issues + * @return the number of high-severity issues */ public long getErrorsCount(final String key) { return getReportFor(key).getSizeOf(Severity.ERROR); @@ -163,7 +163,7 @@ public long getErrorsCount(final String key) { * @param key * the property instance * - * @return the number of high severity issues + * @return the number of high-severity issues */ public long getHighCount(final String key) { return getReportFor(key).getSizeOf(Severity.WARNING_HIGH); @@ -175,7 +175,7 @@ public long getHighCount(final String key) { * @param key * the property instance * - * @return the number of normal severity issues + * @return the number of normal-severity issues */ public long getNormalCount(final String key) { return getReportFor(key).getSizeOf(Severity.WARNING_NORMAL); @@ -187,7 +187,7 @@ public long getNormalCount(final String key) { * @param key * the property instance * - * @return the number of low severity issues + * @return the number of low-severity issues */ public long getLowCount(final String key) { return getReportFor(key).getSizeOf(Severity.WARNING_LOW); diff --git a/plugin/src/main/java/io/jenkins/plugins/analysis/core/util/AffectedFilesResolver.java b/plugin/src/main/java/io/jenkins/plugins/analysis/core/util/AffectedFilesResolver.java index 292ac6173a..be2c91efa9 100644 --- a/plugin/src/main/java/io/jenkins/plugins/analysis/core/util/AffectedFilesResolver.java +++ b/plugin/src/main/java/io/jenkins/plugins/analysis/core/util/AffectedFilesResolver.java @@ -7,6 +7,9 @@ import java.util.Set; import java.util.stream.Collectors; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.StringUtils; + import edu.hm.hafner.analysis.Issue; import edu.hm.hafner.analysis.Report; import edu.hm.hafner.util.FilteredLog; @@ -25,8 +28,10 @@ * @author Ullrich Hafner */ public class AffectedFilesResolver { - /** Sub folder with the affected files. */ + /** Folder with the affected files within Jenkins' build results. */ public static final String AFFECTED_FILES_FOLDER_NAME = "files-with-issues"; + private static final String ZIP_EXTENSION = ".zip"; + private static final String TEXT_EXTENSION = ".tmp"; /** * Returns whether the affected file in Jenkins' build folder does exist and is readable. @@ -39,7 +44,8 @@ public class AffectedFilesResolver { * @return the file */ public static boolean hasAffectedFile(final Run run, final Issue issue) { - return canAccess(getFile(run, issue.getFileName())); + return canAccess(getFile(run, issue.getFileName())) + || canAccess(getZipFile(run, issue.getFileName())); } private static boolean canAccess(final Path file) { @@ -59,7 +65,35 @@ private static boolean canAccess(final Path file) { * if the file could not be found */ static InputStream asStream(final Run build, final String fileName) throws IOException { - return Files.newInputStream(getFile(build, fileName)); + try { + var file = getFile(build, fileName); + if (canAccess(file)) { + return Files.newInputStream(file); + } + + return extractFromZip(build, fileName); + } + catch (InterruptedException e) { + throw new IOException(e); + } + } + + private static InputStream extractFromZip(final Run build, final String fileName) + throws IOException, InterruptedException { + Path tempDir = Files.createTempDirectory(AFFECTED_FILES_FOLDER_NAME); + FilePath unzippedSourcesDir = new FilePath(tempDir.toFile()); + try { + var zipFile = getZipFile(build, fileName); + FilePath inputZipFile = new FilePath(zipFile.toFile()); + inputZipFile.unzip(unzippedSourcesDir); + StringUtils.removeEnd(zipFile.toString(), ZIP_EXTENSION); + var sourceFile = tempDir.resolve(FilenameUtils.getName(fileName)); + + return Files.newInputStream(sourceFile); + } + finally { + unzippedSourcesDir.deleteRecursive(); + } } /** @@ -73,9 +107,27 @@ static InputStream asStream(final Run build, final String fileName) throws * @return the file */ public static Path getFile(final Run run, final String fileName) { + return getPath(run, getTempName(fileName)); // Warnings plugin < 11.0.0 + } + + /** + * Returns the affected file in Jenkins' build folder. + * + * @param run + * the run referencing the build folder + * @param fileName + * the file name in the folder of affected files + * + * @return the file + */ + public static Path getZipFile(final Run run, final String fileName) { + return getPath(run, getZipName(fileName)); + } + + private static Path getPath(final Run run, final String zipName) { return run.getRootDir().toPath() .resolve(AFFECTED_FILES_FOLDER_NAME) - .resolve(getTempName(fileName)); + .resolve(zipName); } /** @@ -87,7 +139,11 @@ public static Path getFile(final Run run, final String fileName) { * @return the temporary name */ private static String getTempName(final String fileName) { - return Integer.toHexString(fileName.hashCode()) + ".tmp"; + return Integer.toHexString(fileName.hashCode()) + TEXT_EXTENSION; + } + + private static String getZipName(final String fileName) { + return getTempName(fileName) + ZIP_EXTENSION; } /** @@ -194,7 +250,7 @@ boolean isInWorkspace(final String fileName) { } public void copy(final String from, final String to) throws IOException, InterruptedException { - createFile(from).copyTo(computeBuildFolderFileName(to)); + createFile(from).zip(computeBuildFolderFileName(to)); } public boolean existsInBuildFolder(final String fileName) { @@ -207,7 +263,7 @@ public boolean existsInBuildFolder(final String fileName) { } private FilePath computeBuildFolderFileName(final String fileName) { - return buildFolder.child(getTempName(fileName)); + return buildFolder.child(getZipName(fileName)); } } }