Skip to content

Commit

Permalink
Fix JENKINS-71955 NullPointerException because getMergeRequestsEnable…
Browse files Browse the repository at this point in the history
…d is null (#456)
  • Loading branch information
pbaumard authored Nov 8, 2024
1 parent 9a03496 commit 45a1ef5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public class GitLabSCMSource extends AbstractGitSCMSource {
private String sshRemote;
private String httpRemote;
private transient Project gitlabProject;
private long projectId;
private Long projectId;

/**
* The cache of {@link ObjectMetadataAction} instances for each open MR.
Expand Down Expand Up @@ -333,7 +333,8 @@ protected void retrieve(
if (request.isFetchBranches()) {
request.setBranches(gitLabApi.getRepositoryApi().getBranches(gitlabProject));
}
if (request.isFetchMRs() && gitlabProject.getMergeRequestsEnabled()) {
boolean mergeRequestsEnabled = !Boolean.FALSE.equals(gitlabProject.getMergeRequestsEnabled());

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 336 is only partially covered, one branch is missing
if (request.isFetchMRs() && mergeRequestsEnabled) {

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 337 is only partially covered, 2 branches are missing
final boolean forkedFromProject = (gitlabProject.getForkedFromProject() != null);
if (!ctx.buildMRForksNotMirror() && forkedFromProject) {
listener.getLogger().format("%nIgnoring merge requests as project is a mirror...%n");
Expand Down Expand Up @@ -409,7 +410,7 @@ public SCMSourceCriteria.Probe create(
}
listener.getLogger().format("%n%d branches were processed%n", count);
}
if (request.isFetchMRs() && !request.isComplete() && gitlabProject.getMergeRequestsEnabled()) {
if (request.isFetchMRs() && !request.isComplete() && mergeRequestsEnabled) {

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 413 is only partially covered, 3 branches are missing
int count = 0;
listener.getLogger().format("%nChecking merge requests..%n");
HashMap<Long, String> forkMrSources = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.jenkins.plugins.gitlabbranchsource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;

import hudson.model.TaskListener;
import hudson.security.AccessControlled;
import hudson.util.StreamTaskListener;
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper;
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer;
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Set;
import jenkins.branch.BranchSource;
import jenkins.scm.api.SCMHead;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.MergeRequestApi;
import org.gitlab4j.api.ProjectApi;
import org.gitlab4j.api.RepositoryApi;
import org.gitlab4j.api.models.Project;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

public class GitLabSCMSourceTest {

private static final String SERVER = "server";
private static final String PROJECT_NAME = "project";
private static final String SOURCE_ID = "id";

@ClassRule
public static JenkinsRule j = new JenkinsRule();

@Test
public void retrieveMRWithEmptyProjectSettings() throws GitLabApiException, IOException, InterruptedException {
GitLabApi gitLabApi = Mockito.mock(GitLabApi.class);
ProjectApi projectApi = Mockito.mock(ProjectApi.class);
RepositoryApi repoApi = Mockito.mock(RepositoryApi.class);
MergeRequestApi mrApi = Mockito.mock(MergeRequestApi.class);
Mockito.when(gitLabApi.getProjectApi()).thenReturn(projectApi);
Mockito.when(gitLabApi.getMergeRequestApi()).thenReturn(mrApi);
Mockito.when(gitLabApi.getRepositoryApi()).thenReturn(repoApi);
Mockito.when(projectApi.getProject(any())).thenReturn(new Project());
try (MockedStatic<GitLabHelper> utilities = Mockito.mockStatic(GitLabHelper.class)) {
utilities
.when(() -> GitLabHelper.apiBuilder(any(AccessControlled.class), anyString()))
.thenReturn(gitLabApi);
GitLabServers.get().addServer(new GitLabServer("", SERVER, ""));
GitLabSCMSourceBuilder sb =
new GitLabSCMSourceBuilder(SOURCE_ID, SERVER, "creds", "po", "group/project", "project");
WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME);
BranchSource source = new BranchSource(sb.build());
source.getSource()
.setTraits(Arrays.asList(new BranchDiscoveryTrait(0), new OriginMergeRequestDiscoveryTrait(1)));
project.getSourcesList().add(source);
ByteArrayOutputStream out = new ByteArrayOutputStream();
final TaskListener listener = new StreamTaskListener(out, StandardCharsets.UTF_8);
Set<SCMHead> scmHead = source.getSource().fetch(listener);
assertEquals(0, scmHead.size());
}
}
}

0 comments on commit 45a1ef5

Please sign in to comment.