Skip to content

Commit

Permalink
Handle run attempt number in build-reports artifact names
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Aug 19, 2024
1 parent 4ee2626 commit edc05be
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class BuildReporterActionHandler {

public Optional<String> generateReport(String workflowName, GHWorkflowRun workflowRun, Path buildReportsArtifactsPath,
BuildReporterConfig buildReporterConfig) throws IOException {
Map<String, Optional<BuildReports>> buildReportsMap = prepareBuildReportMap(buildReportsArtifactsPath);
Map<String, Optional<BuildReports>> buildReportsMap = prepareBuildReportMap(buildReportsArtifactsPath,
workflowRun.getRunAttempt());

WorkflowContext workflowContext = new WorkflowContext(workflowRun);
List<GHWorkflowJob> jobs = workflowRun.listJobs().toList()
Expand All @@ -63,19 +64,31 @@ public Optional<String> generateReport(String workflowName, GHWorkflowRun workfl
workflowReportOptional.get(), true, false, false);
}

private Map<String, Optional<BuildReports>> prepareBuildReportMap(Path buildReportsArtifactsPath) {
private Map<String, Optional<BuildReports>> prepareBuildReportMap(Path buildReportsArtifactsPath, long runAttempt) {
if (!Files.exists(buildReportsArtifactsPath) || !Files.isDirectory(buildReportsArtifactsPath)) {
return Map.of();
}

boolean useNewBuildReportsArtifactNamePattern = false;
try (Stream<Path> jobBuildReportsDirectoriesStream = Files.list(buildReportsArtifactsPath).filter(Files::isDirectory)) {
useNewBuildReportsArtifactNamePattern = jobBuildReportsDirectoriesStream
.anyMatch(d -> WorkflowUtils.matchesNewBuildReportsArtifactNamePattern(d.getFileName().toString()));
} catch (IOException e) {
LOG.error("Unable to extract build reports from directory " + buildReportsArtifactsPath, e);
return Map.of();
}

Map<String, Optional<BuildReports>> buildReportsMap = new HashMap<>();
String buildReportsArtifactPrefix = useNewBuildReportsArtifactNamePattern
? WorkflowConstants.BUILD_REPORTS_ARTIFACT_PREFIX + runAttempt + "-"
: WorkflowConstants.BUILD_REPORTS_ARTIFACT_PREFIX;

try (Stream<Path> jobBuildReportsDirectoriesStream = Files.list(buildReportsArtifactsPath).filter(Files::isDirectory)
.filter(d -> d.getFileName().toString().startsWith(WorkflowConstants.BUILD_REPORTS_ARTIFACT_PREFIX))) {
.filter(d -> d.getFileName().toString().startsWith(buildReportsArtifactPrefix))) {

jobBuildReportsDirectoriesStream.forEach(jobBuildReportsDirectory -> {
String jobName = jobBuildReportsDirectory.getFileName().toString()
.replace(WorkflowConstants.BUILD_REPORTS_ARTIFACT_PREFIX, "");
.replace(buildReportsArtifactPrefix, "");

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -142,7 +143,7 @@ private void handleCompleted(GHWorkflow workflow,

Map<String, Optional<BuildReports>> buildReportsMap = downloadBuildReports(workflowContext,
allBuildReportsDirectory,
artifacts, artifactsAvailable);
artifacts, artifactsAvailable, workflowRun.getRunAttempt());
List<GHWorkflowJob> jobs = workflowRun.listJobs().toList()
.stream()
.sorted(buildReporterConfig.getJobNameComparator())
Expand Down Expand Up @@ -225,7 +226,7 @@ private void handleCompleted(GHWorkflow workflow,

Map<String, Optional<BuildReports>> buildReportsMap = downloadBuildReports(workflowContext,
allBuildReportsDirectory,
artifacts, artifactsAvailable);
artifacts, artifactsAvailable, workflowRun.getRunAttempt());

List<GHWorkflowJob> jobs = workflowRun.listJobs().toList()
.stream()
Expand Down Expand Up @@ -337,22 +338,20 @@ private Optional<GHIssue> getAssociatedReportIssue(GitHub gitHub, GHWorkflowRun

private Map<String, Optional<BuildReports>> downloadBuildReports(WorkflowContext workflowContext,
Path allBuildReportsDirectory,
List<GHArtifact> artifacts, boolean artifactsAvailable) throws IOException {
List<GHArtifact> artifacts, boolean artifactsAvailable, long runAttempt) throws IOException {
if (!artifactsAvailable) {
return Collections.emptyMap();
}

Map<String, Optional<BuildReports>> buildReportsMap = new HashMap<>();

List<GHArtifact> buildReportsArtifacts = artifacts
.stream()
.filter(a -> a.getName().startsWith(WorkflowConstants.BUILD_REPORTS_ARTIFACT_PREFIX))
.sorted((a1, a2) -> a1.getName().compareTo(a2.getName()))
.collect(Collectors.toList());
Map<String, GHArtifact> buildReportsArtifacts = WorkflowUtils.getBuildReportsArtifacts(artifacts, runAttempt);

Map<String, Optional<BuildReports>> buildReportsMap = new HashMap<>();
Set<String> alreadyHandledArtifacts = new HashSet<>();

for (GHArtifact artifact : buildReportsArtifacts) {
for (Entry<String, GHArtifact> artifactEntry : buildReportsArtifacts.entrySet()) {
String jobName = artifactEntry.getKey();
GHArtifact artifact = artifactEntry.getValue();

if (alreadyHandledArtifacts.contains(artifact.getName())) {
continue;
}
Expand All @@ -362,8 +361,7 @@ private Map<String, Optional<BuildReports>> downloadBuildReports(WorkflowContext
Optional<BuildReports> buildReportsOptional = buildReportsUnarchiver.getBuildReports(workflowContext,
artifact, jobDirectory);

buildReportsMap.put(artifact.getName().replace(WorkflowConstants.BUILD_REPORTS_ARTIFACT_PREFIX, ""),
buildReportsOptional);
buildReportsMap.put(jobName, buildReportsOptional);

alreadyHandledArtifacts.add(artifact.getName());
}
Expand Down Expand Up @@ -468,7 +466,15 @@ private ArtifactsAreReady(GHWorkflowRun workflowRun) {
@Override
public Boolean call() throws Exception {
artifacts = workflowRun.listArtifacts().toList();
return !artifacts.isEmpty();

boolean useNewBuildReportsArtifactNamePattern = artifacts.stream()
.anyMatch(a -> WorkflowUtils.matchesNewBuildReportsArtifactNamePattern(a.getName()));

String buildReportsArtifactNamePrefix = useNewBuildReportsArtifactNamePattern
? WorkflowConstants.BUILD_REPORTS_ARTIFACT_PREFIX + workflowRun.getRunAttempt() + "-"
: WorkflowConstants.BUILD_REPORTS_ARTIFACT_PREFIX;

return artifacts.stream().anyMatch(a -> a.getName().startsWith(buildReportsArtifactNamePrefix));
}

public List<GHArtifact> getArtifacts() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package io.quarkus.bot.buildreporter.githubactions;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.kohsuke.github.GHArtifact;

public final class WorkflowUtils {

private static final Pattern BUILD_REPORTS_ARTIFACT_NAME_PATTERN = Pattern
.compile(Pattern.quote(WorkflowConstants.BUILD_REPORTS_ARTIFACT_PREFIX) + "(?:([0-9]+)-)?(.*)");

public static String getFilePath(String moduleName, String fullClassName) {
String classPath = fullClassName.replace(".", "/");
int dollarIndex = classPath.indexOf('$');
Expand All @@ -24,6 +35,40 @@ public static String getOldActiveStatusCommentMarker(String workflowName) {
return String.format(WorkflowConstants.OLD_MESSAGE_ID_ACTIVE_FOR_WORKFLOW, workflowName);
}

public static Map<String, GHArtifact> getBuildReportsArtifacts(List<GHArtifact> artifacts, long runAttempt) {
Map<String, GHArtifact> mappedArtifacts = new TreeMap<>();

for (GHArtifact artifact : artifacts) {
Matcher matcher = BUILD_REPORTS_ARTIFACT_NAME_PATTERN.matcher(artifact.getName());

if (!matcher.matches()) {
continue;
}

String artifactRunAttempt = matcher.group(1);
if (artifactRunAttempt != null) {
// the file is following the new pattern including the run attempt
if (runAttempt == Long.parseLong(artifactRunAttempt)) {
mappedArtifacts.put(matcher.group(2), artifact);
}
} else {
mappedArtifacts.put(matcher.group(2), artifact);
}
}

return mappedArtifacts;
}

public static boolean matchesNewBuildReportsArtifactNamePattern(String artifactName) {
Matcher matcher = BUILD_REPORTS_ARTIFACT_NAME_PATTERN.matcher(artifactName);

if (matcher.matches() && matcher.group(1) != null) {
return true;
}

return false;
}

private WorkflowUtils() {
}
}

0 comments on commit edc05be

Please sign in to comment.