Last active
August 28, 2022 20:22
-
-
Save mikomatic/8769da7f84a0da8749e9d166431d0d0a to your computer and use it in GitHub Desktop.
Gitlab autorebase with jbang and picocli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///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); | |
System.exit(exitCode); | |
} | |
@Override | |
public Integer call() throws Exception { | |
try (GitLabApi gitLabApi = buildGitlabApi()) { | |
List<MergeRequest> mergeRequestList = gitLabApi.getMergeRequestApi() | |
.getMergeRequests(projectId, Constants.MergeRequestState.OPENED); | |
List<MergeRequest> 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment