Skip to content

Commit f6fedd5

Browse files
committed
Allow changing branch.json location in GlobalBuildInfoPlugin
By default, it now reads branches.json from main branch of Elasticsearch (through GitHub raw link) . This can be overridden for testing or when there is a problem with remote call.
1 parent 1c1a531 commit f6fedd5

File tree

3 files changed

+124
-20
lines changed

3 files changed

+124
-20
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/info/GlobalBuildInfoPlugin.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
*/
99
package org.elasticsearch.gradle.internal.info;
1010

11-
import com.fasterxml.jackson.databind.JsonNode;
1211
import com.fasterxml.jackson.databind.ObjectMapper;
1312

1413
import org.apache.commons.io.IOUtils;
15-
import org.elasticsearch.gradle.Version;
1614
import org.elasticsearch.gradle.VersionProperties;
1715
import org.elasticsearch.gradle.internal.BwcVersions;
1816
import org.elasticsearch.gradle.internal.conventions.GitInfoPlugin;
@@ -52,8 +50,8 @@
5250
import java.io.InputStream;
5351
import java.io.InputStreamReader;
5452
import java.io.UncheckedIOException;
53+
import java.net.URI;
5554
import java.nio.file.Files;
56-
import java.util.ArrayList;
5755
import java.util.List;
5856
import java.util.Locale;
5957
import java.util.Random;
@@ -68,12 +66,14 @@
6866
public class GlobalBuildInfoPlugin implements Plugin<Project> {
6967
private static final Logger LOGGER = Logging.getLogger(GlobalBuildInfoPlugin.class);
7068
private static final String DEFAULT_VERSION_JAVA_FILE_PATH = "server/src/main/java/org/elasticsearch/Version.java";
69+
private static final String DEFAULT_BRANCHES_FILE_URL = "https://raw.githubusercontent.com/elastic/elasticsearch/master/branches.json";
70+
private static final String BRANCHES_FILE_LOCATION_PROPERTY = "org.elasticsearch.build.branches-file-location";
7171

7272
private ObjectFactory objectFactory;
7373
private final JavaInstallationRegistry javaInstallationRegistry;
7474
private final JvmMetadataDetector metadataDetector;
7575
private final ProviderFactory providers;
76-
private final ObjectMapper objectMapper;
76+
private final BranchesFileParser branchesFileParser;
7777
private JavaToolchainService toolChainService;
7878
private Project project;
7979

@@ -88,7 +88,7 @@ public GlobalBuildInfoPlugin(
8888
this.javaInstallationRegistry = javaInstallationRegistry;
8989
this.metadataDetector = new ErrorTraceMetadataDetector(metadataDetector);
9090
this.providers = providers;
91-
this.objectMapper = new ObjectMapper();
91+
this.branchesFileParser = new BranchesFileParser(new ObjectMapper());
9292
}
9393

9494
@Override
@@ -204,24 +204,25 @@ private BwcVersions resolveBwcVersions() {
204204
}
205205

206206
private List<DevelopmentBranch> getDevelopmentBranches() {
207-
List<DevelopmentBranch> branches = new ArrayList<>();
208-
// TODO jozala: change to get branches.json from raw GitHub link (where to keep the URL?)
209-
File branchesFile = new File(Util.locateElasticsearchWorkspace(project.getGradle()), "branches.json");
210-
try (InputStream is = new FileInputStream(branchesFile)) {
211-
JsonNode json = objectMapper.readTree(is);
212-
for (JsonNode node : json.get("branches")) {
213-
branches.add(
214-
new DevelopmentBranch(
215-
node.get("branch").asText(),
216-
Version.fromString(node.get("version").asText())
217-
)
218-
);
207+
String branchesFileLocation = project.getProviders().gradleProperty(BRANCHES_FILE_LOCATION_PROPERTY)
208+
.getOrElse(DEFAULT_BRANCHES_FILE_URL);
209+
LOGGER.info("Reading branches.json from {}", branchesFileLocation);
210+
byte[] branchesBytes;
211+
if (branchesFileLocation.startsWith("http")) {
212+
try (InputStream in = URI.create(branchesFileLocation).toURL().openStream()) {
213+
branchesBytes = in.readAllBytes();
214+
} catch (IOException e) {
215+
throw new UncheckedIOException("Failed to download branches.json from: " + branchesFileLocation, e);
216+
}
217+
} else {
218+
try {
219+
branchesBytes = Files.readAllBytes(new File(branchesFileLocation).toPath());
220+
} catch (IOException e) {
221+
throw new UncheckedIOException("Failed to read branches.json from: " + branchesFileLocation, e);
219222
}
220-
} catch (IOException e) {
221-
throw new UncheckedIOException(e);
222223
}
223224

224-
return branches;
225+
return branchesFileParser.parse(branchesBytes);
225226
}
226227

227228
private void logGlobalBuildInfo(BuildParameterExtension buildParams) {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.info
11+
12+
import groovy.json.JsonOutput
13+
import spock.lang.Specification
14+
import spock.lang.TempDir
15+
16+
import org.elasticsearch.gradle.Version
17+
import org.elasticsearch.gradle.internal.BwcVersions
18+
import org.gradle.api.Project
19+
import org.gradle.api.provider.Provider
20+
import org.gradle.api.provider.ProviderFactory
21+
import org.gradle.testfixtures.ProjectBuilder
22+
23+
import java.nio.file.Path
24+
25+
class GlobalBuildInfoPluginSpec extends Specification {
26+
27+
@TempDir
28+
File projectRoot
29+
30+
Project project
31+
32+
def setup() {
33+
project = ProjectBuilder.builder()
34+
.withProjectDir(projectRoot)
35+
.withName("bwcTestProject")
36+
.build()
37+
project = Spy(project)
38+
project.getRootProject() >> project
39+
40+
File versionFileDir = new File(projectRoot, "server/src/main/java/org/elasticsearch")
41+
versionFileDir.mkdirs()
42+
new File(versionFileDir, "Version.java").text = """
43+
package org.elasticsearch;
44+
public class Version {
45+
public static final Version V_8_17_8 = new Version(8_17_08_99);
46+
public static final Version V_8_18_0 = new Version(8_18_00_99);
47+
public static final Version V_8_18_1 = new Version(8_18_01_99);
48+
public static final Version V_8_18_2 = new Version(8_18_02_99);
49+
public static final Version V_8_18_3 = new Version(8_18_03_99);
50+
public static final Version V_8_19_0 = new Version(8_19_00_99);
51+
public static final Version V_9_0_0 = new Version(9_00_00_99);
52+
public static final Version V_9_0_1 = new Version(9_00_01_99);
53+
public static final Version V_9_0_2 = new Version(9_00_02_99);
54+
public static final Version V_9_0_3 = new Version(9_00_03_99);
55+
public static final Version V_9_1_0 = new Version(9_01_00_99);
56+
public static final Version CURRENT = V_9_1_0;
57+
58+
}
59+
"""
60+
}
61+
62+
def "resolve unreleased versions from branches file set by Gradle property"() {
63+
given:
64+
ProviderFactory providerFactorySpy = Spy(project.getProviders())
65+
Path branchesJsonPath = projectRoot.toPath().resolve("myBranches.json")
66+
Provider<String> gradleBranchesLocationProvider = project.providers.provider { return branchesJsonPath.toString() }
67+
providerFactorySpy.gradleProperty("org.elasticsearch.build.branches-file-location") >> gradleBranchesLocationProvider
68+
project.getProviders() >> providerFactorySpy
69+
branchesJsonPath.text = branchesJson(
70+
[
71+
new DevelopmentBranch("main", Version.fromString("9.1.0")),
72+
new DevelopmentBranch("9.0", Version.fromString("9.0.3")),
73+
new DevelopmentBranch("8.19", Version.fromString("8.19.1")),
74+
new DevelopmentBranch("8.18", Version.fromString("8.18.2")),
75+
]
76+
)
77+
78+
when:
79+
project.objects.newInstance(GlobalBuildInfoPlugin).apply(project)
80+
BuildParameterExtension ext = project.extensions.getByType(BuildParameterExtension)
81+
BwcVersions bwcVersions = ext.bwcVersions
82+
83+
then:
84+
bwcVersions != null
85+
bwcVersions.unreleased.toSet() == ["9.1.0", "9.0.3", "8.19.1", "8.18.2"].collect { Version.fromString(it) }.toSet()
86+
}
87+
88+
String branchesJson(List<DevelopmentBranch> branches) {
89+
Map<String, Object> branchesFileContent = [
90+
branches: branches.collect { branch ->
91+
[
92+
branch: branch.name(),
93+
version: branch.version().toString(),
94+
]
95+
}
96+
]
97+
return JsonOutput.prettyPrint(JsonOutput.toJson(branchesFileContent))
98+
}
99+
}

gradle.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ org.gradle.java.installations.fromEnv=RUNTIME_JAVA_HOME
2222

2323
# if configuration cache enabled then enable parallel support too
2424
org.gradle.configuration-cache.parallel=true
25+
26+
# This is needed for testing changes in BwC before version field is introduced in branches.json on the main branch
27+
# TODO remove before merging to main
28+
org.elasticsearch.build.branches-file-location=https://raw.githubusercontent.com/elastic/elasticsearch/bwc-explicit-branches-version/branches.json

0 commit comments

Comments
 (0)