Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix filter for draft merge request #403

Merged
merged 1 commit into from
Feb 9, 2024
Merged
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 @@ -48,10 +48,10 @@
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jenkins.model.Jenkins;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.plugins.git.GitTagSCMRevision;
Expand Down Expand Up @@ -86,6 +86,7 @@
import jenkins.scm.impl.trait.Selection;
import org.apache.commons.lang.StringUtils;
import org.gitlab4j.api.Constants;
import org.gitlab4j.api.Constants.MergeRequestState;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.models.AccessLevel;
Expand Down Expand Up @@ -332,41 +333,33 @@
if (request.isFetchBranches()) {
request.setBranches(gitLabApi.getRepositoryApi().getBranches(gitlabProject));
}
Predicate<MergeRequest> filter = ignore -> false;
if (ctx.alwaysIgnoreMRWorkInProgress()) {
filter = mr -> !mr.getWorkInProgress();
}
if (request.isFetchMRs() && gitlabProject.getMergeRequestsEnabled()) {
// If not authenticated GitLabApi cannot detect if it is a fork
// If `forkedFromProject` is null it doesn't mean anything
if (gitlabProject.getForkedFromProject() == null) {
listener.getLogger()
.format("%nUnable to detect if it is a mirror or not still fetching MRs anyway...%n");
List<MergeRequest> mrs = gitLabApi
.getMergeRequestApi()
.getMergeRequests(gitlabProject, Constants.MergeRequestState.OPENED);
mrs = mrs.stream()
.filter(mr -> mr.getSourceProjectId() != null)
.filter(filter)
.collect(Collectors.toList());
request.setMergeRequests(mrs);
} else if (ctx.buildMRForksNotMirror()) {
listener.getLogger()
.format("%nCollecting MRs for fork except those that target its upstream...%n");
List<MergeRequest> mrs = gitLabApi
.getMergeRequestApi()
.getMergeRequests(gitlabProject, Constants.MergeRequestState.OPENED);
mrs = mrs.stream()
.filter(mr -> mr.getSourceProjectId() != null
&& !mr.getTargetProjectId()
.equals(gitlabProject
.getForkedFromProject()
.getId()))
.filter(filter)
.collect(Collectors.toList());
request.setMergeRequests(mrs);
} else {
if (!ctx.buildMRForksNotMirror() && gitlabProject.getForkedFromProject() != null) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this method could use a bit of code clean but gotta start somewhere.

listener.getLogger().format("%nIgnoring merge requests as project is a mirror...%n");
} else {
// If not authenticated GitLabApi cannot detect if it is a fork
// If `forkedFromProject` is null it doesn't mean anything
listener.getLogger()
.format(
gitlabProject.getForkedFromProject() == null
? "%nUnable to detect if it is a mirror or not still fetching MRs anyway...%n"
: "%nCollecting MRs for fork except those that target its upstream...%n");
Stream<MergeRequest> mrs =
gitLabApi
.getMergeRequestApi()
.getMergeRequests(gitlabProject, MergeRequestState.OPENED)
.stream()
.filter(mr -> mr.getSourceProjectId() != null);
if (ctx.buildMRForksNotMirror()) {
mrs = mrs.filter(mr -> !mr.getTargetProjectId()
.equals(gitlabProject.getForkedFromProject().getId()));
}

if (ctx.alwaysIgnoreMRWorkInProgress()) {
mrs = mrs.filter(mr -> !mr.getWorkInProgress());
}

request.setMergeRequests(mrs.collect(Collectors.toList()));

Check warning on line 362 in src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 337-362 are not covered by tests
}
}
if (request.isFetchTags()) {
Expand Down