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

[JENKINS-63572] Avoid NPE if remote config is empty #954

Merged
Merged
Show file tree
Hide file tree
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
12 changes: 7 additions & 5 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -851,11 +851,13 @@ public GitClient createClient(TaskListener listener, EnvVars environment, Run<?,
String url = getParameterString(uc.getUrl(), environment);
chooser = new GitToolChooser(url, project, ucCredentialsId, gitTool, n, listener,unsupportedCommand.determineSupportForJGit());
}
listener.getLogger().println("The recommended git tool is: " + chooser.getGitTool());
String updatedGitExe = chooser.getGitTool();

if (!updatedGitExe.equals("NONE")) {
gitExe = updatedGitExe;
if (chooser != null) {
listener.getLogger().println("The recommended git tool is: " + chooser.getGitTool());
String updatedGitExe = chooser.getGitTool();

if (!updatedGitExe.equals("NONE")) {
gitExe = updatedGitExe;
}
}
}
Git git = Git.with(listener, environment).in(ws).using(gitExe);
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/jenkins/plugins/git/GitToolChooserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,24 @@ public void getCacheDirCreatesNoDirectory() throws Exception {
assertThat(expectedCacheDir, is(anExistingDirectory()));
}

/* Do not throw null pointer excception if remote configuration is empty. */
@Test
@Issue("JENKINS-63572")
public void testSizeEstimationWithNoRemoteConfig() throws Exception {
sampleRepo.init();

failAProject(sampleRepo);

List<TopLevelItem> list = jenkins.jenkins.getItems();

// Use JGit as the git tool for this NPE check
GitTool jgitTool = new JGitTool(Collections.<ToolProperty<?>>emptyList());

GitToolChooser sizeEstimator = new GitToolChooser(sampleRepo.toString(), list.get(0), null, jgitTool, null, TaskListener.NULL, true);

assertThat(sizeEstimator.getGitTool(), is("NONE"));
}

/*
A test extension implemented to clone the behavior of a plugin extending the capability of providing the size of
repo from a remote URL of "Github".
Expand Down Expand Up @@ -637,6 +655,18 @@ private void buildAProject(GitSampleRepoRule sampleRepo, boolean noCredentials)
}
}

/* Attempt to perform a checkout without defining a remote repository. Expected to fail, but should not report NPE */
private void failAProject(GitSampleRepoRule sampleRepo) throws Exception {
WorkflowJob p = jenkins.jenkins.createProject(WorkflowJob.class, "intentionally-failing-job-without-remote-config");
p.setDefinition(new CpsFlowDefinition("node {\n"
+ " checkout(\n"
+ " [$class: 'GitSCM']\n"
+ " )\n"
+ "}", true));
WorkflowRun b = jenkins.assertBuildStatus(hudson.model.Result.FAILURE, p.scheduleBuild2(0));
jenkins.waitForMessage("Couldn't find any revision to build", b);
}

private StandardCredentials createCredential(CredentialsScope scope, String id) {
return new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, "username", "password");
}
Expand Down