Skip to content

Instantly share code, notes, and snippets.

Last active August 28, 2022 20:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Gitlab autorebase with jbang and picocli
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17+
//DEPS info.picocli:picocli:4.5.0
//DEPS org.gitlab4j:gitlab4j-api:5.0.1
import org.gitlab4j.api.Constants;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.models.MergeRequest;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import java.util.List;
import java.util.concurrent.Callable;
@Command(name = "autorebase", mixinStandardHelpOptions = true, version = "autorebase 0.1",
description = "autorebase made with jbang")
class autorebase implements Callable<Integer> {
public static final String AUTOREBASE_LABEL = "ci/autorebase";
@CommandLine.Option(names = {
"--project-id"}, description = "The gitlab project id", required = true)
private String projectId;
@CommandLine.Option(names = {"--gitlab-url"}, description = "The gitlab url", required = true)
private String gitlabUrl;
@CommandLine.Option(names = {"--token",
"-t"}, description = "The gitlab access token", required = true)
private String token;
@CommandLine.Option(names = {
"--ignore-certificate-errors"}, description = "ignore gitlab certificate errors", defaultValue = "true")
private boolean ignoreCertificateErrors;
@CommandLine.Option(names = {"--dry-run", "-d"}, defaultValue = "false")
private boolean dryRun;
public static void main(String... args) {
int exitCode = new CommandLine(new autorebase()).execute(args);
public Integer call() throws Exception {
try (GitLabApi gitLabApi = buildGitlabApi()) {
List<MergeRequest> mergeRequestList = gitLabApi.getMergeRequestApi()
.getMergeRequests(projectId, Constants.MergeRequestState.OPENED);
List<MergeRequest> openedMRList =
.filter(mr -> !Boolean.TRUE.equals(mr.getRebaseInProgress()))
.filter(mr -> mr.getLabels().contains(AUTOREBASE_LABEL))
for (MergeRequest mr : openedMRList) {
Long mrIid = mr.getIid();
System.out.println("Rebasing open MR [%s,%s]".formatted(mrIid, mr.getTitle()));
if (!dryRun) {
gitLabApi.getMergeRequestApi().rebaseMergeRequest(projectId, mrIid);
return 0;
private GitLabApi buildGitlabApi() {
GitLabApi gitLabApi = new GitLabApi(gitlabUrl, token);
return gitLabApi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment