Skip to content

Commit 45a1ef5

Browse files
authored
Fix JENKINS-71955 NullPointerException because getMergeRequestsEnabled is null (#456)
1 parent 9a03496 commit 45a1ef5

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public class GitLabSCMSource extends AbstractGitSCMSource {
116116
private String sshRemote;
117117
private String httpRemote;
118118
private transient Project gitlabProject;
119-
private long projectId;
119+
private Long projectId;
120120

121121
/**
122122
* The cache of {@link ObjectMetadataAction} instances for each open MR.
@@ -333,7 +333,8 @@ protected void retrieve(
333333
if (request.isFetchBranches()) {
334334
request.setBranches(gitLabApi.getRepositoryApi().getBranches(gitlabProject));
335335
}
336-
if (request.isFetchMRs() && gitlabProject.getMergeRequestsEnabled()) {
336+
boolean mergeRequestsEnabled = !Boolean.FALSE.equals(gitlabProject.getMergeRequestsEnabled());
337+
if (request.isFetchMRs() && mergeRequestsEnabled) {
337338
final boolean forkedFromProject = (gitlabProject.getForkedFromProject() != null);
338339
if (!ctx.buildMRForksNotMirror() && forkedFromProject) {
339340
listener.getLogger().format("%nIgnoring merge requests as project is a mirror...%n");
@@ -409,7 +410,7 @@ public SCMSourceCriteria.Probe create(
409410
}
410411
listener.getLogger().format("%n%d branches were processed%n", count);
411412
}
412-
if (request.isFetchMRs() && !request.isComplete() && gitlabProject.getMergeRequestsEnabled()) {
413+
if (request.isFetchMRs() && !request.isComplete() && mergeRequestsEnabled) {
413414
int count = 0;
414415
listener.getLogger().format("%nChecking merge requests..%n");
415416
HashMap<Long, String> forkMrSources = new HashMap<>();
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.jenkins.plugins.gitlabbranchsource;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.ArgumentMatchers.anyString;
6+
7+
import hudson.model.TaskListener;
8+
import hudson.security.AccessControlled;
9+
import hudson.util.StreamTaskListener;
10+
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper;
11+
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer;
12+
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers;
13+
import java.io.ByteArrayOutputStream;
14+
import java.io.IOException;
15+
import java.nio.charset.StandardCharsets;
16+
import java.util.Arrays;
17+
import java.util.Set;
18+
import jenkins.branch.BranchSource;
19+
import jenkins.scm.api.SCMHead;
20+
import org.gitlab4j.api.GitLabApi;
21+
import org.gitlab4j.api.GitLabApiException;
22+
import org.gitlab4j.api.MergeRequestApi;
23+
import org.gitlab4j.api.ProjectApi;
24+
import org.gitlab4j.api.RepositoryApi;
25+
import org.gitlab4j.api.models.Project;
26+
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
27+
import org.junit.ClassRule;
28+
import org.junit.Test;
29+
import org.jvnet.hudson.test.JenkinsRule;
30+
import org.mockito.MockedStatic;
31+
import org.mockito.Mockito;
32+
33+
public class GitLabSCMSourceTest {
34+
35+
private static final String SERVER = "server";
36+
private static final String PROJECT_NAME = "project";
37+
private static final String SOURCE_ID = "id";
38+
39+
@ClassRule
40+
public static JenkinsRule j = new JenkinsRule();
41+
42+
@Test
43+
public void retrieveMRWithEmptyProjectSettings() throws GitLabApiException, IOException, InterruptedException {
44+
GitLabApi gitLabApi = Mockito.mock(GitLabApi.class);
45+
ProjectApi projectApi = Mockito.mock(ProjectApi.class);
46+
RepositoryApi repoApi = Mockito.mock(RepositoryApi.class);
47+
MergeRequestApi mrApi = Mockito.mock(MergeRequestApi.class);
48+
Mockito.when(gitLabApi.getProjectApi()).thenReturn(projectApi);
49+
Mockito.when(gitLabApi.getMergeRequestApi()).thenReturn(mrApi);
50+
Mockito.when(gitLabApi.getRepositoryApi()).thenReturn(repoApi);
51+
Mockito.when(projectApi.getProject(any())).thenReturn(new Project());
52+
try (MockedStatic<GitLabHelper> utilities = Mockito.mockStatic(GitLabHelper.class)) {
53+
utilities
54+
.when(() -> GitLabHelper.apiBuilder(any(AccessControlled.class), anyString()))
55+
.thenReturn(gitLabApi);
56+
GitLabServers.get().addServer(new GitLabServer("", SERVER, ""));
57+
GitLabSCMSourceBuilder sb =
58+
new GitLabSCMSourceBuilder(SOURCE_ID, SERVER, "creds", "po", "group/project", "project");
59+
WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME);
60+
BranchSource source = new BranchSource(sb.build());
61+
source.getSource()
62+
.setTraits(Arrays.asList(new BranchDiscoveryTrait(0), new OriginMergeRequestDiscoveryTrait(1)));
63+
project.getSourcesList().add(source);
64+
ByteArrayOutputStream out = new ByteArrayOutputStream();
65+
final TaskListener listener = new StreamTaskListener(out, StandardCharsets.UTF_8);
66+
Set<SCMHead> scmHead = source.getSource().fetch(listener);
67+
assertEquals(0, scmHead.size());
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)