|
3 | 3 | import java.io.IOException;
|
4 | 4 | import java.net.URI;
|
5 | 5 | import java.util.ArrayList;
|
| 6 | +import java.util.HashSet; |
6 | 7 | import java.util.List;
|
| 8 | +import java.util.Set; |
7 | 9 | import java.util.function.Predicate;
|
| 10 | +import java.util.stream.Collectors; |
8 | 11 |
|
9 | 12 | import jakarta.inject.Inject;
|
10 | 13 |
|
11 | 14 | import org.hibernate.infra.bot.config.DeploymentConfig;
|
12 | 15 | import org.hibernate.infra.bot.config.RepositoryConfig;
|
13 | 16 | import org.hibernate.infra.bot.develocity.DevelocityCIBuildScan;
|
14 | 17 | import org.hibernate.infra.bot.develocity.DevelocityReportFormatter;
|
| 18 | +import org.hibernate.infra.bot.util.GitHubActionsRunId; |
| 19 | +import org.hibernate.infra.bot.util.JenkinsRunId; |
15 | 20 |
|
16 | 21 | import com.gradle.develocity.api.BuildsApi;
|
17 | 22 | import com.gradle.develocity.model.Build;
|
@@ -66,24 +71,29 @@ void checkRunCompleted(@CheckRun.Completed GHEventPayload.CheckRun payload,
|
66 | 71 | }
|
67 | 72 | var repository = payload.getRepository();
|
68 | 73 | 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. |
71 | 76 | return;
|
72 | 77 | }
|
73 |
| - String sha = checkRun.getHeadSha(); |
| 78 | + var sha = payload.getCheckRun().getHeadSha(); |
74 | 79 | extractCIBuildScans( repository, buildScanConfig, sha );
|
75 | 80 | }
|
76 | 81 |
|
77 | 82 | private void extractCIBuildScans(GHRepository repository, RepositoryConfig.Develocity.BuildScan config,
|
78 | 83 | String sha) {
|
79 | 84 | try {
|
| 85 | + var checkRuns = repository.getCheckRuns( sha ).toList(); |
| 86 | + if ( checkRuns.stream().noneMatch( this::isJobOrWorkflow ) ) { |
| 87 | + return; |
| 88 | + } |
80 | 89 | long checkId = createDevelocityCheck( repository, sha );
|
81 | 90 | Throwable failure = null;
|
82 | 91 | List<DevelocityCIBuildScan> buildScans = new ArrayList<>();
|
83 | 92 | try {
|
| 93 | + String query = createBuildScansQuery( checkRuns ); |
84 | 94 | for ( Build build : develocityBuildsApi.getBuilds( new BuildsQuery.BuildsQueryQueryParam()
|
85 | 95 | .fromInstant( 0L )
|
86 |
| - .query( "tag:CI value:\"Git commit id=%s\"".formatted( sha ) ) |
| 96 | + .query( query ) |
87 | 97 | .models( List.of( BuildModelName.GRADLE_MINUS_ATTRIBUTES,
|
88 | 98 | BuildModelName.MAVEN_MINUS_ATTRIBUTES ) ) ) ) {
|
89 | 99 | try {
|
@@ -118,6 +128,37 @@ private void extractCIBuildScans(GHRepository repository, RepositoryConfig.Devel
|
118 | 128 | }
|
119 | 129 | }
|
120 | 130 |
|
| 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 | + |
121 | 162 | private DevelocityCIBuildScan toCIBuildScan(Build build) {
|
122 | 163 | URI buildScanURI = deploymentConfig.develocity().uri().resolve( "/s/" + build.getId() );
|
123 | 164 | List<BuildAttributesValue> customValues;
|
|
0 commit comments