Skip to content

Commit

Permalink
Merge pull request #954 from MarkEWaite/fix-npe-on-empty-remote-config
Browse files Browse the repository at this point in the history
[JENKINS-63572] Avoid NPE if remote config is empty
  • Loading branch information
MarkEWaite authored Sep 12, 2020
2 parents 2d1d96b + 9b36b09 commit 22be427
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
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

0 comments on commit 22be427

Please sign in to comment.