Skip to content
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 @@ -277,6 +277,77 @@ class TransportVersionGenerationFuncTest extends AbstractTransportVersionFuncTes
assertUpperBound("9.2", "second_tv,8124000")
}

def "update flag works with current"() {
given:
referableAndReferencedTransportVersion("new_tv", "8123000")
file("myserver/src/main/resources/transport/latest/9.2.csv").text =
"""
<<<<<<< HEAD
existing_92,8123000
=======
new_tv,8123000
>>>>>> name
""".strip()

when:
def result = runGenerateAndValidateTask("--resolve-conflict").build()

then:
assertGenerateAndValidateSuccess(result)
assertReferableDefinition("existing_92", "8123000,8012001")
assertReferableDefinition("new_tv", "8124000")
assertUpperBound("9.2", "new_tv,8124000")
}

def "update flag works with multiple branches"() {
given:
referableAndReferencedTransportVersion("new_tv", "8123000,8012001,7123001")
file("myserver/src/main/resources/transport/latest/9.2.csv").text =
"""
<<<<<<< HEAD
existing_92,8123000
=======
new_tv,8123000
>>>>>> name
""".strip()
file("myserver/src/main/resources/transport/latest/9.1.csv").text =
"""
<<<<<<< HEAD
existing_92,8012001
=======
new_tv,8012001
>>>>>> name
""".strip()
file("myserver/src/main/resources/transport/latest/8.19.csv").text =
"""
<<<<<<< HEAD
initial_8.19.7,7123001
=======
new_tv,7123001
>>>>>> name
""".strip()

when:
def result = runGenerateAndValidateTask("--resolve-conflict").build()

then:
assertGenerateAndValidateSuccess(result)
assertReferableDefinition("existing_92", "8123000,8012001")
assertUnreferableDefinition("initial_8.19.7", "7123001")
assertReferableDefinition("new_tv", "8124000,8012002,7123002")
assertUpperBound("9.2", "new_tv,8124000")
assertUpperBound("9.1", "new_tv,8012002")
assertUpperBound("8.19", "new_tv,7123002")
}

def "update flag cannot be used with backport branches"() {
when:
def result = runGenerateTask("--resolve-conflict", "--backport-branches=9.1").buildAndFail()

then:
assertGenerateFailure(result, "Cannot use --resolve-conflict with --backport-branches")
}

def "branches param order does not matter"() {
given:
referencedTransportVersion("test_tv")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public void run() throws IOException {
var definition = new TransportVersionDefinition(initialDefinitionName, List.of(id));
resources.writeUnreferableDefinition(definition);
var newUpperBound = new TransportVersionUpperBound(upperBoundName, initialDefinitionName, id);
resources.writeUpperBound(newUpperBound);
resources.writeUpperBound(newUpperBound, false);

if (releaseVersion.getRevision() == 0) {
Version currentVersion = getCurrentVersion().get();
String currentUpperBoundName = getUpperBoundName(currentVersion);
var currentUpperBound = new TransportVersionUpperBound(currentUpperBoundName, initialDefinitionName, id);
resources.writeUpperBound(currentUpperBound);
resources.writeUpperBound(currentUpperBound, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public abstract class GenerateTransportVersionDefinitionTask extends DefaultTask
@Option(option = "increment", description = "The amount to increment the id from the current upper bounds file by")
public abstract Property<Integer> getIncrement();

@Input
@Optional
@Option(
option = "resolve-conflict",
description = "Regenerate the transport version currently being added to upstream to resolve a merge conflict"
)
public abstract Property<Boolean> getResolveConflict();

/**
* The name of the upper bounds file which will be used at runtime on the current branch. Normally
* this equates to VersionProperties.getElasticsearchVersion().
Expand All @@ -95,7 +103,7 @@ public void run() throws IOException {
String targetDefinitionName = getTargetDefinitionName(resources, referencedNames, changedDefinitionNames);

List<TransportVersionUpperBound> upstreamUpperBounds = resources.getUpperBoundsFromUpstream();
Set<String> targetUpperBoundNames = getTargetUpperBoundNames(upstreamUpperBounds);
Set<String> targetUpperBoundNames = getTargetUpperBoundNames(resources, upstreamUpperBounds, targetDefinitionName);

getLogger().lifecycle("Generating transport version name: " + targetDefinitionName);
if (targetDefinitionName.isEmpty()) {
Expand All @@ -121,6 +129,7 @@ private List<TransportVersionId> updateUpperBounds(
throw new IllegalArgumentException("Invalid increment " + increment + ", must be a positive integer");
}
List<TransportVersionId> ids = new ArrayList<>();
boolean stageInGit = getResolveConflict().getOrElse(false);

TransportVersionDefinition existingDefinition = resources.getReferableDefinitionFromUpstream(definitionName);
for (TransportVersionUpperBound existingUpperBound : existingUpperBounds) {
Expand All @@ -134,12 +143,12 @@ private List<TransportVersionId> updateUpperBounds(
int targetIncrement = upperBoundName.equals(currentUpperBoundName) ? increment : 1;
targetId = createTargetId(existingUpperBound, targetIncrement);
var newUpperBound = new TransportVersionUpperBound(upperBoundName, definitionName, targetId);
resources.writeUpperBound(newUpperBound);
resources.writeUpperBound(newUpperBound, stageInGit);
}
ids.add(targetId);
} else {
// Default case: we're not targeting this branch so reset it
resources.writeUpperBound(existingUpperBound);
resources.writeUpperBound(existingUpperBound, false);
}
}

Expand Down Expand Up @@ -180,7 +189,19 @@ private String getTargetDefinitionName(
}
}

private Set<String> getTargetUpperBoundNames(List<TransportVersionUpperBound> upstreamUpperBounds) {
private Set<String> getTargetUpperBoundNames(
TransportVersionResourcesService resources,
List<TransportVersionUpperBound> upstreamUpperBounds,
String targetDefinitionName
) throws IOException {
if (getResolveConflict().getOrElse(false)) {
if (getBackportBranches().isPresent()) {
throw new IllegalArgumentException("Cannot use --resolve-conflict with --backport-branches");
}

return getUpperBoundNamesFromDefinition(resources, upstreamUpperBounds, targetDefinitionName);
}

Set<String> targetUpperBoundNames = new HashSet<>();
targetUpperBoundNames.add(getCurrentUpperBoundName().get());
if (getBackportBranches().isPresent()) {
Expand All @@ -204,9 +225,32 @@ private Set<String> getTargetUpperBoundNames(List<TransportVersionUpperBound> up
return targetUpperBoundNames;
}

private Set<String> getUpperBoundNamesFromDefinition(
TransportVersionResourcesService resources,
List<TransportVersionUpperBound> upstreamUpperBounds,
String targetDefinitionName
) throws IOException {
TransportVersionDefinition definition = resources.getReferableDefinition(targetDefinitionName);
Set<String> upperBoundNames = new HashSet<>();
upperBoundNames.add(getCurrentUpperBoundName().get());

// skip the primary id as that is current, which we always add
for (int i = 1; i < definition.ids().size(); ++i) {
TransportVersionId id = definition.ids().get(i);
// we have a small number of upper bound files, so just scan for the ones we want
for (TransportVersionUpperBound upperBound : upstreamUpperBounds) {
if (upperBound.definitionId().base() == id.base()) {
upperBoundNames.add(upperBound.name());
}
}
}

return upperBoundNames;
}

private void resetAllUpperBounds(TransportVersionResourcesService resources) throws IOException {
for (TransportVersionUpperBound upperBound : resources.getUpperBoundsFromUpstream()) {
resources.writeUpperBound(upperBound);
resources.writeUpperBound(upperBound, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ Map<String, TransportVersionDefinition> getReferableDefinitions() throws IOExcep
return readDefinitions(transportResourcesDir.resolve(REFERABLE_DIR));
}

/** Return a single referable definition by name */
TransportVersionDefinition getReferableDefinition(String name) throws IOException {
Path resourcePath = transportResourcesDir.resolve(getReferableDefinitionRelativePath(name));
return TransportVersionDefinition.fromString(resourcePath, Files.readString(resourcePath, StandardCharsets.UTF_8));
}

/** Get a referable definition from upstream if it exists there, or null otherwise */
TransportVersionDefinition getReferableDefinitionFromUpstream(String name) {
Path resourcePath = getReferableDefinitionRelativePath(name);
Expand Down Expand Up @@ -218,10 +224,14 @@ List<TransportVersionUpperBound> getUpperBoundsFromUpstream() throws IOException
}

/** Write the given upper bound to a file in the transport resources */
void writeUpperBound(TransportVersionUpperBound upperBound) throws IOException {
void writeUpperBound(TransportVersionUpperBound upperBound, boolean stageInGit) throws IOException {
Path path = transportResourcesDir.resolve(getUpperBoundRelativePath(upperBound.name()));
logger.debug("Writing upper bound [" + upperBound + "] to [" + path + "]");
Files.writeString(path, upperBound.definitionName() + "," + upperBound.definitionId().complete() + "\n", StandardCharsets.UTF_8);

if (stageInGit) {
gitCommand("add", path.toString());
}
}

/** Return the path within the repository of the given latest */
Expand Down