///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 { 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); System.exit(exitCode); } @Override public Integer call() throws Exception { try (GitLabApi gitLabApi = buildGitlabApi()) { List mergeRequestList = gitLabApi.getMergeRequestApi() .getMergeRequests(projectId, Constants.MergeRequestState.OPENED); List openedMRList = mergeRequestList.stream() .filter(mr -> !Boolean.TRUE.equals(mr.getRebaseInProgress())) .filter(mr -> mr.getLabels().contains(AUTOREBASE_LABEL)) .toList(); 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); gitLabApi.setIgnoreCertificateErrors(ignoreCertificateErrors); return gitLabApi; } }