Skip to content

Commit

Permalink
ZIP all source files before copying into build folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Jan 15, 2024
1 parent cd005a4 commit d873719
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> propertyFormatter) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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) {
Expand All @@ -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();
}
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
}
}
}

0 comments on commit d873719

Please sign in to comment.