Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handle cancelled jobs and deal with nested zips #155

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ private void handleCompleted(GHWorkflow workflow,
hideOutdatedWorkflowRunResults(buildReporterConfig, workflowContext, pullRequest,
workflow.getName(), gitHubGraphQLClient);

if (conclusion == Conclusion.SUCCESS && pullRequest.isDraft()) {
if ((conclusion == Conclusion.SUCCESS && pullRequest.isDraft())
|| conclusion == Conclusion.CANCELLED) {
return;
}

Expand Down Expand Up @@ -161,7 +162,7 @@ private void handleCompleted(GHWorkflow workflow,
workflowReport,
artifactsAvailable,
true,
hasOtherPendingCheckRuns(pullRequest, buildReporterConfig));
hasOtherPendingWorkflowRuns(pullRequest, buildReporterConfig));

if (reportCommentOptional.isEmpty()) {
return;
Expand Down Expand Up @@ -458,7 +459,7 @@ public List<GHArtifact> getArtifacts() {
}
}

private static boolean hasOtherPendingCheckRuns(GHPullRequest pullRequest, BuildReporterConfig buildReporterConfig) {
private static boolean hasOtherPendingWorkflowRuns(GHPullRequest pullRequest, BuildReporterConfig buildReporterConfig) {
try {
return pullRequest.getRepository().getCheckRuns(pullRequest.getHead().getSha()).toList().stream()
.anyMatch(cr -> cr.getStatus() == Status.QUEUED || cr.getStatus() == Status.IN_PROGRESS);
Expand Down
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
Loading