Skip to content

Commit

Permalink
Handle nested zips as we will need to use them
Browse files Browse the repository at this point in the history
This to avoid various bugs in upload-artifact and Node.js

actions/upload-artifact#240
actions/upload-artifact#39
  • Loading branch information
gsmet committed Jan 31, 2024
1 parent a65c94f commit cc11f9b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -48,29 +49,73 @@ interface TestResultsPath extends Comparable<TestResultsPath> {
String getModuleName(Path jobDirectory);
}

@Override
public String toString() {
return "BuildReports[\n"
+ " jobDirectory=" + jobDirectory + "\n"
+ " buildReportPath=" + buildReportPath + "\n"
+ " gradleBuildScanUrlPath=" + gradleBuildScanUrlPath + "\n"
+ " testResultsPaths=" + testResultsPaths + "\n"
+ "]";
}

static class Builder {

private final Path jobDirectory;
private Path buildReportPath;
private Path gradleBuildScanUrlPath;
private Set<TestResultsPath> testResultsPaths = new TreeSet<>();

private Set<Path> alreadyTreatedPaths = new HashSet<>();

Builder(Path jobDirectory) {
this.jobDirectory = jobDirectory;
}

void addPath(Path path) {
if (path.endsWith(WorkflowConstants.BUILD_REPORT_PATH)) {
buildReportPath = path;
} else if (path.endsWith(WorkflowConstants.GRADLE_BUILD_SCAN_URL_PATH)) {
return;
}
if (path.endsWith(WorkflowConstants.GRADLE_BUILD_SCAN_URL_PATH)) {
gradleBuildScanUrlPath = path;
} else if (path.endsWith(MAVEN_SUREFIRE_REPORTS_PATH)) {
return;
}

// when we upload the files directly with upload-artifact,
// we get entries for the directories so we can directly
// resolve them
if (addTestPath(path)) {
return;
}

// when we are building a zip with the zip command,
// we don't get so lucky so we need to resolve the parent
// directory
// all the test files are stored in the same parent directory,
// there is no nesting
addTestPath(path.getParent());
}

private boolean addTestPath(Path path) {
if (path == null || alreadyTreatedPaths.contains(path)) {
return true;
}

if (path.endsWith(MAVEN_SUREFIRE_REPORTS_PATH)) {
testResultsPaths.add(new SurefireTestResultsPath(path));
} else if (path.endsWith(MAVEN_FAILSAFE_REPORTS_PATH)) {
return true;
}
if (path.endsWith(MAVEN_FAILSAFE_REPORTS_PATH)) {
testResultsPaths.add(new FailsafeTestResultsPath(path));
} else if (path.endsWith(GRADLE_REPORTS_PATH)) {
return true;
}
if (path.endsWith(GRADLE_REPORTS_PATH)) {
testResultsPaths.add(new GradleTestResultsPath(path));
return true;
}

return false;
}

BuildReports build() {
Expand Down Expand Up @@ -101,6 +146,11 @@ public int compareTo(TestResultsPath o) {
return path.compareTo(o.getPath());
}

@Override
public String toString() {
return getClass().getName() + "[" + path + "]";
}

@Override
public int hashCode() {
return Objects.hash(path);
Expand Down Expand Up @@ -140,6 +190,11 @@ public String getModuleName(Path jobDirectory) {
return jobDirectory.relativize(path).getParent().getParent().toString();
}

@Override
public String toString() {
return getClass().getName() + "[" + path + "]";
}

@Override
public int compareTo(TestResultsPath o) {
return path.compareTo(o.getPath());
Expand Down Expand Up @@ -184,6 +239,11 @@ public String getModuleName(Path jobDirectory) {
return jobDirectory.relativize(path).getParent().getParent().getParent().toString();
}

@Override
public String toString() {
return getClass().getName() + "[" + path + "]";
}

@Override
public int compareTo(TestResultsPath o) {
return path.compareTo(o.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class BuildReportsUnarchiver {

private static final Logger LOG = Logger.getLogger(BuildReportsUnarchiver.class);

private static final String NESTED_ZIP_FILE_NAME = "build-reports.zip";

@Inject
UrlShortener urlShortener;

Expand Down Expand Up @@ -102,6 +104,8 @@ private BuildReports unzip(InputStream inputStream, Path jobDirectory) throws IO
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else if (NESTED_ZIP_FILE_NAME.equals(zipEntry.getName())) {
return unzip(zis, jobDirectory);
} else {
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
Expand All @@ -113,6 +117,7 @@ private BuildReports unzip(InputStream inputStream, Path jobDirectory) throws IO
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}

fos.close();
}
zipEntry = zis.getNextEntry();
Expand Down

0 comments on commit cc11f9b

Please sign in to comment.