Skip to content

Commit df39d43

Browse files
yrodieremarko-bekhta
authored andcommitted
Handle merge commits in develocity build scan retrieval
1 parent 993e1f7 commit df39d43

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/main/java/org/hibernate/infra/bot/ExtractDevelocityBuildScans.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
import java.io.IOException;
44
import java.net.URI;
55
import java.util.ArrayList;
6+
import java.util.HashSet;
67
import java.util.List;
8+
import java.util.Set;
79
import java.util.function.Predicate;
10+
import java.util.stream.Collectors;
811

912
import jakarta.inject.Inject;
1013

1114
import org.hibernate.infra.bot.config.DeploymentConfig;
1215
import org.hibernate.infra.bot.config.RepositoryConfig;
1316
import org.hibernate.infra.bot.develocity.DevelocityCIBuildScan;
1417
import org.hibernate.infra.bot.develocity.DevelocityReportFormatter;
18+
import org.hibernate.infra.bot.util.GitHubActionsRunId;
19+
import org.hibernate.infra.bot.util.JenkinsRunId;
1520

1621
import com.gradle.develocity.api.BuildsApi;
1722
import com.gradle.develocity.model.Build;
@@ -66,24 +71,29 @@ void checkRunCompleted(@CheckRun.Completed GHEventPayload.CheckRun payload,
6671
}
6772
var repository = payload.getRepository();
6873
var checkRun = payload.getCheckRun();
69-
if ( checkRun.getApp().getId() != deploymentConfig.jenkins().githubAppId()
70-
&& !"github-actions".equals( checkRun.getApp().getSlug() ) ) {
74+
if ( DEVELOCITY_CHECK_RUN_NAME.equals( checkRun.getName() ) ) {
75+
// Don't react to our own checks.
7176
return;
7277
}
73-
String sha = checkRun.getHeadSha();
78+
var sha = payload.getCheckRun().getHeadSha();
7479
extractCIBuildScans( repository, buildScanConfig, sha );
7580
}
7681

7782
private void extractCIBuildScans(GHRepository repository, RepositoryConfig.Develocity.BuildScan config,
7883
String sha) {
7984
try {
85+
var checkRuns = repository.getCheckRuns( sha ).toList();
86+
if ( checkRuns.stream().noneMatch( this::isJobOrWorkflow ) ) {
87+
return;
88+
}
8089
long checkId = createDevelocityCheck( repository, sha );
8190
Throwable failure = null;
8291
List<DevelocityCIBuildScan> buildScans = new ArrayList<>();
8392
try {
93+
String query = createBuildScansQuery( checkRuns );
8494
for ( Build build : develocityBuildsApi.getBuilds( new BuildsQuery.BuildsQueryQueryParam()
8595
.fromInstant( 0L )
86-
.query( "tag:CI value:\"Git commit id=%s\"".formatted( sha ) )
96+
.query( query )
8797
.models( List.of( BuildModelName.GRADLE_MINUS_ATTRIBUTES,
8898
BuildModelName.MAVEN_MINUS_ATTRIBUTES ) ) ) ) {
8999
try {
@@ -118,6 +128,37 @@ private void extractCIBuildScans(GHRepository repository, RepositoryConfig.Devel
118128
}
119129
}
120130

131+
private String createBuildScansQuery(List<GHCheckRun> checkRuns) throws IOException {
132+
Set<String> queries = new HashSet<>();
133+
for ( GHCheckRun checkRun : checkRuns ) {
134+
if ( isJenkinsBuild( checkRun ) ) {
135+
var runId = JenkinsRunId.parse( checkRun.getExternalId() );
136+
queries.add( "value:\"CI job=%s\" and value:\"CI build number=%s\""
137+
.formatted( runId.job(), runId.run() ) );
138+
}
139+
else if ( isGitHubActionsWorkflow( checkRun ) ) {
140+
var runId = GitHubActionsRunId.parse( checkRun.getDetailsUrl() );
141+
queries.add( "value:\"CI run=%s\""
142+
.formatted( runId.run() ) );
143+
}
144+
// else: unsupported check, ignore
145+
}
146+
return queries.stream().collect( Collectors.joining( ") or (", "(", ")" ) );
147+
}
148+
149+
private boolean isJobOrWorkflow(GHCheckRun checkRun) {
150+
return isGitHubActionsWorkflow( checkRun ) || isJenkinsBuild( checkRun );
151+
}
152+
153+
private boolean isGitHubActionsWorkflow(GHCheckRun checkRun) {
154+
return "github-actions".equals( checkRun.getApp().getSlug() );
155+
}
156+
157+
private boolean isJenkinsBuild(GHCheckRun checkRun) {
158+
return checkRun.getApp().getId() == deploymentConfig.jenkins().githubAppId()
159+
&& checkRun.getExternalId() != null && !checkRun.getExternalId().isBlank();
160+
}
161+
121162
private DevelocityCIBuildScan toCIBuildScan(Build build) {
122163
URI buildScanURI = deploymentConfig.develocity().uri().resolve( "/s/" + build.getId() );
123164
List<BuildAttributesValue> customValues;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.hibernate.infra.bot.util;
2+
3+
import java.net.URL;
4+
import java.util.regex.Pattern;
5+
6+
public record GitHubActionsRunId(long run) {
7+
8+
private static final Pattern FORMAT = Pattern.compile( "actions/runs/(\\d+)/" );
9+
10+
public static GitHubActionsRunId parse(URL detailsUrl) {
11+
var matcher = FORMAT.matcher( detailsUrl.toString() );
12+
if ( !matcher.find() ) {
13+
throw new IllegalArgumentException( "Invalid format for a GitHub Actions run URL: " + detailsUrl );
14+
}
15+
return new GitHubActionsRunId( Long.parseLong( matcher.group( 1 ) ) );
16+
}
17+
}

0 commit comments

Comments
 (0)