Skip to content

Commit

Permalink
Merge pull request #157 from gsmet/adjustments-2
Browse files Browse the repository at this point in the history
Various small adjustments
  • Loading branch information
gsmet authored Jan 31, 2024
2 parents 75f04fd + ef0770a commit a9bd4ca
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.quarkus.bot.buildreporter.githubactions;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
Expand All @@ -9,6 +12,8 @@
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;
Expand Down Expand Up @@ -71,14 +76,26 @@ private Map<String, Optional<BuildReports>> prepareBuildReportMap(Path buildRepo

BuildReports.Builder buildReportsBuilder = new BuildReports.Builder(jobBuildReportsDirectory);

try (Stream<Path> jobBuildReportsFilesStream = Files.walk(jobBuildReportsDirectory)) {
jobBuildReportsFilesStream.forEach(p -> buildReportsBuilder.addPath(p));

buildReportsMap.put(jobName, Optional.of(buildReportsBuilder.build()));
} catch (IOException e) {
LOG.error("Unable to extract build reports from subdirectory " + jobBuildReportsDirectory, e);

buildReportsMap.put(jobName, Optional.empty());
Path buildReportsArchive = jobBuildReportsDirectory.resolve(BuildReportsUnarchiver.NESTED_ZIP_FILE_NAME);
if (Files.isReadable(buildReportsArchive)) {
try {
try (InputStream buildReportsZipIs = Files.newInputStream(buildReportsArchive)) {
buildReportsMap.put(jobName, Optional.of(unzip(buildReportsZipIs,
Files.createTempDirectory("build-reports-analyzer-action-" + jobName))));
}
} catch (IOException e) {
LOG.error("Unable to extract build reports from archive " + buildReportsArchive, e);
}
} else {
try (Stream<Path> jobBuildReportsFilesStream = Files.walk(jobBuildReportsDirectory)) {
jobBuildReportsFilesStream.forEach(p -> buildReportsBuilder.addPath(p));

buildReportsMap.put(jobName, Optional.of(buildReportsBuilder.build()));
} catch (IOException e) {
LOG.error("Unable to extract build reports from subdirectory " + jobBuildReportsDirectory, e);

buildReportsMap.put(jobName, Optional.empty());
}
}
});

Expand All @@ -89,4 +106,52 @@ private Map<String, Optional<BuildReports>> prepareBuildReportMap(Path buildRepo
return Map.of();
}
}

private BuildReports unzip(InputStream inputStream, Path jobDirectory) throws IOException {
BuildReports.Builder buildReportsBuilder = new BuildReports.Builder(jobDirectory);

try (final ZipInputStream zis = new ZipInputStream(inputStream)) {
final byte[] buffer = new byte[1024];
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
final Path newPath = getZipEntryPath(jobDirectory, zipEntry);
final File newFile = newPath.toFile();

buildReportsBuilder.addPath(newPath);

if (zipEntry.isDirectory()) {
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else {
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory " + parent);
}

final FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}

fos.close();
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
}

return buildReportsBuilder.build();
}

private static Path getZipEntryPath(Path destinationDirectory, ZipEntry zipEntry) throws IOException {
Path destinationFile = destinationDirectory.resolve(zipEntry.getName());

if (!destinationFile.toAbsolutePath().startsWith(destinationDirectory.toAbsolutePath())) {
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
}

return destinationFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BuildReportsUnarchiver {

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

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

@Inject
UrlShortener urlShortener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public boolean isCancelled() {
}

public boolean isFailure() {
return Conclusion.FAILURE.equals(conclusion);
return Conclusion.FAILURE.equals(conclusion) || hasJobsFailing();
}

public String getWorkflowRunUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public String getConclusionEmoji() {
}

public boolean isFailing() {
return !Conclusion.SUCCESS.equals(conclusion) &&
!Conclusion.SKIPPED.equals(conclusion);
return (!Conclusion.SUCCESS.equals(conclusion) && !Conclusion.SKIPPED.equals(conclusion))
|| hasReportedFailures();
}

public boolean isSkipped() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ It should be safe to merge provided you have a look at the other checks in the s
{/if}

{#if report.flakyTests}

---

## Flaky tests

{#for job in report.jobsWithFlakyTests}
Expand Down

0 comments on commit a9bd4ca

Please sign in to comment.