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 JENKINS-71955 NullPointerException because getMergeRequestsEnabled is null #456

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -116,7 +116,7 @@
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 @@
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 @@
}
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.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.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.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.RestartableJenkinsRule;
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";

@Rule
public final RestartableJenkinsRule plan = new RestartableJenkinsRule();
Copy link
Member

Choose a reason for hiding this comment

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

Any particular reason to use RestartableJenkinsRule here over plain JenkinsRule?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No particular reason. I changed the test to use JenkinsRule.


@Test
public void retrieveMRWithEmptyProjectSettings() {
plan.then(j -> {
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());
}
});
}
}
Loading