Skip to content

Commit

Permalink
Refactor git dependency #8 [JENKINS-29546] in order to avoid plugin d…
Browse files Browse the repository at this point in the history
…ependencies
  • Loading branch information
v1v committed Jul 21, 2015
1 parent 89ea91d commit 8f63aee
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -100,7 +100,7 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.3.4</version>
<optional>true</optional>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
Expand Up @@ -2,13 +2,13 @@

import hudson.model.Item;
import hudson.model.Project;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
import hudson.plugins.git.extensions.impl.CloneOption;
import hudson.util.DescribableList;
import jenkins.model.Jenkins;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

import java.lang.reflect.Method;
import java.util.AbstractList;
import java.util.logging.Level;

/**
* @author Victor Martinez
*/
Expand All @@ -23,15 +23,32 @@ public GitShallowChecker() {

public boolean executeCheck(Item item) {
if (item instanceof Project) {
if (((Project) item).getScm() instanceof hudson.plugins.git.GitSCM) {
DescribableList<GitSCMExtension, GitSCMExtensionDescriptor> extensionsList = ((GitSCM) ((Project) item).getScm()).getExtensions();
boolean status = true;
for (GitSCMExtension extension : extensionsList) {
if (extension instanceof hudson.plugins.git.extensions.impl.CloneOption && ((CloneOption) extension).isShallow()) {
if (Jenkins.getInstance().pluginManager.getPlugin("git")!=null) {
if (((Project) item).getScm().getClass().getName().endsWith("GitSCM")) {
boolean status = true;
try {
Method method = ((Project) item).getScm().getClass().getMethod("getExtensions", null);
Object extensionsList = method.invoke( ((Project) item).getScm());
if (extensionsList instanceof AbstractList) {
for (Object extension : ((AbstractList) extensionsList) ) {
if (extension.getClass().getName().endsWith("CloneOption")) {
Object isShallow = extension.getClass().getMethod("isShallow", null).invoke(extension);
if (isShallow instanceof Boolean) {
status = ! ((Boolean) isShallow).booleanValue();
}
}
}
}
} catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
status = false;
} finally {
return status;
}
}
return status;
} else {
LOG.log(Level.FINE, "Plugin GIT doesn't exist");
return false;
}
}
return false;
Expand Down
Expand Up @@ -49,6 +49,9 @@ public class GitShallowCheckerTestCase {
extensions.add(new CleanCheckout());
project.setScm(new hudson.plugins.git.GitSCM(null, null, false, null, null, "", extensions));
assertTrue(checker.executeCheck(project));
extensions.add(new CloneOption(false, "", 0));
project.setScm(new hudson.plugins.git.GitSCM(null, null, false, null, null, "", extensions));
assertTrue(checker.executeCheck(project));
}
@Test public void testGitSCMWithCloneOptionExtensionNoShallowJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
Expand Down

0 comments on commit 8f63aee

Please sign in to comment.