Skip to content
Open
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
implementation 'io.quarkus:quarkus-arc'
implementation 'org.gitlab4j:gitlab4j-api:6.0.0-rc.8'
implementation 'eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging:6.0.0'
implementation 'se.sawano.java:alphanumeric-comparator:2.0.0'
testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'
testImplementation 'com.github.tomakehurst:wiremock-jre8:2.33.2'
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/enums/MergeStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package enums;

public enum MergeStrategy {
CONFIGURATION_FILE,
PROTECTED_BRANCHES

}
86 changes: 79 additions & 7 deletions src/main/java/service/GitLabService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.MergeRequestParams;
import org.gitlab4j.api.models.ProtectedBranch;
import org.gitlab4j.models.Constants.MergeRequestState;

import controller.model.CascadeResult;
import controller.model.DeleteBranchResult;
import controller.model.MergeRequestResult;
import controller.model.MergeRequestSimple;
import controller.model.MergeRequestUcascadeState;
import enums.MergeStrategy;
import io.quarkus.info.BuildInfo;
import io.quarkus.info.GitInfo;
import io.quarkus.logging.Log;
import io.quarkus.vertx.ConsumeEvent;
import io.smallrye.common.annotation.Blocking;
import io.vertx.mutiny.core.eventbus.EventBus;
import se.sawano.java.text.AlphanumericComparator;
import util.ConfigurationUtils;

@ApplicationScoped
Expand All @@ -60,12 +63,16 @@ public class GitLabService {
@ConfigProperty(name = "gitlab.api.token.approver")
Optional<String> apiTokenApprover;

@ConfigProperty(name = "merge.strategy", defaultValue = "configuration_file")
MergeStrategy mergeStrategy;

@Inject
GitInfo gitInfo;

@Inject
BuildInfo buildInfo;

private static final String BRANCH_SEARCH_QUERY_PARAM_TEMPLATE = "^%s$";
private static final String UCASCADE_CONFIGURATION_FILE = "ucascade.json";
private static final String UCASCADE_TAG = "[ucascade]";
private static final String UCASCADE_BRANCH_PATTERN_PREFIX = "^mr(\\d+)_";
Expand Down Expand Up @@ -207,14 +214,8 @@ private void mergePreviousAutoMr(CascadeResult result, String gitlabEventUUID, L
}

private void createAutoMr(CascadeResult result, String gitlabEventUUID, Long projectId, String prevMrSourceBranch, String sourceBranch, Long mrNumber, String mergeSha) {
String branchModel = getBranchModelConfigurationFile(gitlabEventUUID, projectId, mergeSha);
String nextMainBranch = ConfigurationUtils.getNextTargetBranch(branchModel, sourceBranch);

String nextMainBranch = getNextTargetBranch(gitlabEventUUID, projectId, sourceBranch, mergeSha);
if (nextMainBranch != null) {
Branch branch = getBranch(gitlabEventUUID, projectId, nextMainBranch);
if (!Branch.isValid(branch)) {
throw new IllegalStateException(String.format("GitlabEvent: '%s' | Branch named '%s' does not exist in project '%d'. Please check the ucascade configuration file.", gitlabEventUUID, nextMainBranch, projectId));
}
if (haveDiff(gitlabEventUUID, projectId, mergeSha, nextMainBranch)) {
String tmpBranchName = "mr" + mrNumber + "_" + sourceBranch;
createBranch(gitlabEventUUID, projectId, tmpBranchName, mergeSha);
Expand Down Expand Up @@ -570,6 +571,77 @@ private String getBranchModelConfigurationFile(String gitlabEventUUID, Long proj
}
}

private String getNextTargetBranch(String gitlabEventUUID, Long projectId, String sourceBranch, String mergeSha) {
return switch (mergeStrategy) {
case CONFIGURATION_FILE -> getNextTargetBranchFromConfigurationFile(gitlabEventUUID, projectId, sourceBranch, mergeSha);
case PROTECTED_BRANCHES -> getNextTargetBranchFromProtectedBranches(gitlabEventUUID, projectId, sourceBranch, mergeSha);
};
}

private String getNextTargetBranchFromConfigurationFile(String gitlabEventUUID, Long projectId, String sourceBranch, String mergeSha) {
String branchModel = getBranchModelConfigurationFile(gitlabEventUUID, projectId, mergeSha);
String nextMainBranch = ConfigurationUtils.getNextTargetBranch(branchModel, sourceBranch);
String targetBranch = null;
if (nextMainBranch != null) {
Branch branch = getBranch(gitlabEventUUID, projectId, nextMainBranch);
if (!Branch.isValid(branch)) {
throw new IllegalStateException(String.format("GitlabEvent: '%s' | Branch named '%s' does not exist in projectId '%d'. Please check the ucascade configuration file.", gitlabEventUUID, nextMainBranch, projectId));
}
targetBranch = branch.getName();
}
return targetBranch;
}

private String getNextTargetBranchFromProtectedBranches(String gitlabEventUUID, Long projectId, String sourceBranch, String mergeSha) {
try {
List<ProtectedBranch> protectedBranches = gitlab.getProtectedBranchesApi().getProtectedBranches(projectId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned about relying on the sorting provided by the API call.

Gitlab is returning the branches alphabetically sorted, but natural sorting should be preferred. For instance, release/10.x.x comes before release/9.x.x, which is not a natural merge path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not convinced by this sorting approach.

I don't see this "blind" branch sorting working reliably on repositories without very strict branch naming rules. I actually find it quite dangerous, and I can think of some ways it can mess-up repositories. For instance, hotfix/ branches are typically protected and created on demand. Another problem are branches named dev, develop, main, etc. So unless we find a sorting algorithm that respects git branch conventions, I don't think we should assume we can auto discover merge paths.

If we do find that sorting algorithm, we should be able reduce the API calls here. The list branches call already returns the list of branches with the information if the branch is protected or not. We should try to use that, filter the protected ones, and move on from there.

Copy link
Author

@leonevich leonevich Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, I think it's worth removing the block for direct merging of protected branches and leaving only the merge strategy within branch groups. The search within branch groups will always follow the wildcard rules like release/*hotfix/*, etc., and cascade merges will only occur within the group that contains the source branch.

If we do not use wildcard rules in the project settings and instead specify specific branches such as release/1.2.3hotfix/some-patchdevelop, etc., then cascade merging will not be performed since the project configuration does not include branch grouping rules. On the other hand, we can move the wildcard rule to the configuration and specify that branch groups can be combined based on name templates, similar to how it is configured in the project settings.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to automate the merging between branches, for example, hotfix/* -> release/*?
If so, it is also possible to set up prioritization between groups and then organize a continuous merge process based on the sequence: fix -> hotfix/5.10.1 -> release/5.10.1 -> (hotfix/5.10.2 if it exists) -> release/5.10.2 -> ...

Copy link
Author

@leonevich leonevich Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevertheless, all these attempts to establish a strict sequence for cascading merges do not yield reliable results, since defining the sequence is only possible when the branch name contains a version. Therefore, the cascade merge strategy cannot be based solely on a list of protected branches. To ensure transparent system behavior in all cases, the merge strategy should be built around a list of branch groups formed by branch name patterns, which can be ordered alphanumerically. This list is specified in the configuration as (release/*, hotfix/*, ...). Thus, the responsibility for selecting branches for cascade merging remains with the end user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your analysis, and that's why we ended up with a configuration file to define the merge paths.

Copy link
Author

@leonevich leonevich Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution is not universal, but in my case, where only release branches are protected, it works great

image

List<String> sortedProtectedBranchNames = protectedBranches.stream()
.map(ProtectedBranch::getName)
.sorted(new AlphanumericComparator())
.toList();
boolean sourceBranchFounded = false;
String targetBranch = null;
boolean withWildcardRules = protectedBranches.stream().anyMatch(pb -> pb.getName().contains("*"));
if (withWildcardRules) {
outerLoop: for (ProtectedBranch protectedBranch : protectedBranches) {
String search = BRANCH_SEARCH_QUERY_PARAM_TEMPLATE.formatted(protectedBranch.getName());
List<Branch> branches = gitlab.getRepositoryApi().getBranches(projectId, search);
List<String> sortedBranchNames = branches.stream()
.filter(Branch::isValid)
.map(Branch::getName)
.sorted(new AlphanumericComparator())
.toList();
for (String branch : sortedBranchNames) {
if (sourceBranchFounded) {
targetBranch = branch;
break outerLoop;
}
if (branch.equals(sourceBranch)) {
sourceBranchFounded = true;
}
}
sourceBranchFounded = false;
}
} else {
for (String protectedBranch : sortedProtectedBranchNames) {
Branch branch = gitlab.getRepositoryApi().getBranch(projectId, protectedBranch);
if (Branch.isValid(branch)) {
if (sourceBranchFounded) {
targetBranch = branch.getName();
break;
}
if (branch.getName().equals(sourceBranch)) {
sourceBranchFounded = true;
}
}
}
}
return targetBranch;
} catch (GitLabApiException e) {
throw new IllegalStateException(String.format("GitlabEvent: '%s' | Cannot retrieve protected branch '%s'", gitlabEventUUID, sourceBranch), e);
}
}

private boolean hasPipeline(String gitlabEventUUID, MergeRequest mr) {
try {
MergeRequestApi mrApi = gitlab.getMergeRequestApi();
Expand Down