Skip to content
Permalink
Browse files

[JENKINS-41528] Git ref sub (#4)

* Initial approach: suggest to use git ref repos in submodules... only when using git over 2.4+

* Using the right method name and fixed some syntax errors

* Added further tests and using the  right plugin version validation

* Still WIP

* Fixed wrong asserts

* Speeding up tests and using the AbstractAction pattern

* Added missing import

* Fixed test asserts

* Git Plugin 2.4.1 version required another Jenkins Core Version which didn't have the MatrixProject bundled within the core and thereofre it required an explicit version, and I didn't want to depend on it therefore I used the reflection to avoid ti

* Fixed flake test since it is not the right assert to test

* Visualise Test output

* Fixed unused imports

* Added UI Test

* Fixed hardcoded strings
  • Loading branch information...
v1v committed Jul 29, 2017
1 parent 4c3fbc4 commit a581519c6d00a69ce55304bce391476c38f50b3f
18 pom.xml
@@ -11,8 +11,8 @@
</parent>

<properties>
<jenkins.version>1.554.1</jenkins.version>
<jenkins-test-harness.version>1.554.1</jenkins-test-harness.version>
<jenkins.version>1.580</jenkins.version>
<jenkins-test-harness.version>1.580</jenkins-test-harness.version>
<!--TODO: make true after the code cleanup-->
<findbugs.failOnError>false</findbugs.failOnError>
</properties>
@@ -60,6 +60,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.20</version>
</plugin>
</plugins>
</reporting>

@@ -130,7 +135,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.3.4</version>
<version>2.4.1</version>
<scope>test</scope>
</dependency>

@@ -162,6 +167,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>

<!-- JENKINS-29427 -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
@@ -5,45 +5,19 @@
import hudson.model.Node;
import hudson.model.RootAction;
import jenkins.model.Jenkins;
import org.jenkins.ci.plugins.jenkinslint.check.ArtifactChecker;
import org.jenkins.ci.plugins.jenkinslint.check.CleanupWorkspaceChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GitRefChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GitShallowChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GradleWrapperChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GroovySystemExitChecker;
import org.jenkins.ci.plugins.jenkinslint.check.HardcodedScriptChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JavadocChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JobAssignedLabelChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JobDescriptionChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JobLogRotatorChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JobNameChecker;
import org.jenkins.ci.plugins.jenkinslint.check.TimerTriggerChecker;
import org.jenkins.ci.plugins.jenkinslint.check.MasterLabelChecker;
import org.jenkins.ci.plugins.jenkinslint.check.MavenJobTypeChecker;
import org.jenkins.ci.plugins.jenkinslint.check.MultibranchJobTypeChecker;
import org.jenkins.ci.plugins.jenkinslint.check.NullSCMChecker;
import org.jenkins.ci.plugins.jenkinslint.check.PollingSCMTriggerChecker;
import org.jenkins.ci.plugins.jenkinslint.check.SlaveDescriptionChecker;
import org.jenkins.ci.plugins.jenkinslint.check.SlaveLabelChecker;
import org.jenkins.ci.plugins.jenkinslint.check.SlaveVersionChecker;
import org.jenkins.ci.plugins.jenkinslint.check.TimeoutChecker;
import org.jenkins.ci.plugins.jenkinslint.check.WindowsSlaveLaunchChecker;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractAction;
import org.jenkins.ci.plugins.jenkinslint.model.InterfaceCheck;
import org.jenkins.ci.plugins.jenkinslint.model.InterfaceSlaveCheck;
import org.jenkins.ci.plugins.jenkinslint.model.Job;
import org.jenkins.ci.plugins.jenkinslint.model.Lint;
import org.jenkins.ci.plugins.jenkinslint.model.Slave;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import hudson.model.Api;

@ExportedBean
@Extension
@@ -0,0 +1,64 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.PluginWrapper;
import hudson.model.Item;
import hudson.model.Project;
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
*/
public class GitRefSubmoduleChecker extends AbstractCheck {

public GitRefSubmoduleChecker() {
super();
this.setDescription(Messages.GitRefSubmoduleCheckerDesc());
this.setSeverity(Messages.GitRefSubmoduleCheckerSeverity());
}

public boolean executeCheck(Item item) {
if (item instanceof Project) {
PluginWrapper plugin = Jenkins.getInstance().pluginManager.getPlugin("git");
if (plugin!=null && plugin.getVersionNumber().isNewerThan(new hudson.util.VersionNumber("2.4.0"))) {
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("SubmoduleOption")) {
Object reference = extension.getClass().getMethod("getReference",null).invoke(extension);
if (reference instanceof String) {
LOG.log(Level.WARNING, "foundXXX " + reference);
status = ((String) reference).isEmpty();
}
}
}
}
} catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
status = false;
} finally {
return status;
}
}
} else {
LOG.log(Level.FINE, "Plugin GIT hasn't been configured in this project");
return false;
}
} else {
LOG.log(Level.FINE, "Plugin GIT doesn't exist");
return false;
}

}
return false;
}
}
@@ -1,6 +1,5 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.matrix.MatrixProject;
import hudson.model.Item;
import hudson.model.Project;
import hudson.tasks.Builder;
@@ -37,10 +36,16 @@ public boolean executeCheck(Item item) {
if (item instanceof Project) {
found = isGradlew(((Project) item).getBuilders());
}
if (item instanceof MatrixProject) {
found = isGradlew(((MatrixProject) item).getBuilders());
if (item.getClass().getSimpleName().equals("MatrixProject")) {
try {
Object getBuilders = item.getClass().getMethod("getBuilders", null).invoke(item);
if (getBuilders instanceof List) {
found = isGradlew((List) getBuilders);
}
}catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
}
}

}
return found;
}
@@ -1,6 +1,5 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.matrix.MatrixProject;
import hudson.model.Item;
import hudson.model.Project;
import hudson.tasks.Builder;
@@ -38,10 +37,16 @@ public boolean executeCheck(Item item) {
if (item instanceof Project) {
found = isSystemExit(((Project) item).getBuilders());
}
if (item instanceof MatrixProject) {
found = isSystemExit(((MatrixProject) item).getBuilders());
if (item.getClass().getSimpleName().equals("MatrixProject")) {
try {
Object getBuilders = item.getClass().getMethod("getBuilders", null).invoke(item);
if (getBuilders instanceof List) {
found = isSystemExit((List) getBuilders);
}
}catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
}
}

} else {
LOG.log(Level.INFO, "Groovy is not installed");
}
@@ -1,11 +1,9 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.matrix.MatrixProject;
import hudson.model.Item;
import hudson.model.Project;
import hudson.tasks.Builder;
import hudson.tasks.CommandInterpreter;
import jenkins.model.Jenkins;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

import java.util.List;
@@ -41,8 +39,15 @@ public boolean executeCheck(Item item) {
if (item instanceof Project) {
found = isBuilderHarcoded (((Project)item).getBuilders());
}
if (item instanceof MatrixProject) {
found = isBuilderHarcoded (((MatrixProject)item).getBuilders());
if (item.getClass().getSimpleName().equals("MatrixProject")) {
try {
Object getBuilders = item.getClass().getMethod("getBuilders", null).invoke(item);
if (getBuilders instanceof List) {
found = isBuilderHarcoded((List) getBuilders);
}
}catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
}
}
return found;
}
@@ -1,14 +1,12 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.matrix.MatrixProject;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.Project;
import hudson.tasks.BuildWrapper;
import hudson.util.DescribableList;
import jenkins.model.Jenkins;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

import java.lang.reflect.Method;
import java.util.List;
import java.util.logging.Level;
@@ -40,8 +38,15 @@ public boolean executeCheck(Item item) {
if (item instanceof Project) {
notfound = !isTimeout(((Project) item).getBuildWrappersList());
}
if (item instanceof MatrixProject) {
notfound = !isTimeout(((MatrixProject) item).getBuildWrappersList());
if (item.getClass().getSimpleName().equals("MatrixProject")) {
try {
Object getBuildWrappersList = item.getClass().getMethod("getBuildWrappersList", null).invoke(item);
if (getBuildWrappersList instanceof List) {
notfound = !isTimeout((List) getBuildWrappersList);
}
}catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
}
}
}
return notfound;
@@ -2,29 +2,7 @@

import hudson.model.Api;
import org.jenkins.ci.plugins.jenkinslint.Messages;
import org.jenkins.ci.plugins.jenkinslint.check.ArtifactChecker;
import org.jenkins.ci.plugins.jenkinslint.check.CleanupWorkspaceChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GitRefChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GitShallowChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GradleWrapperChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GroovySystemExitChecker;
import org.jenkins.ci.plugins.jenkinslint.check.HardcodedScriptChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JavadocChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JobAssignedLabelChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JobDescriptionChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JobLogRotatorChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JobNameChecker;
import org.jenkins.ci.plugins.jenkinslint.check.MasterLabelChecker;
import org.jenkins.ci.plugins.jenkinslint.check.MavenJobTypeChecker;
import org.jenkins.ci.plugins.jenkinslint.check.MultibranchJobTypeChecker;
import org.jenkins.ci.plugins.jenkinslint.check.NullSCMChecker;
import org.jenkins.ci.plugins.jenkinslint.check.PollingSCMTriggerChecker;
import org.jenkins.ci.plugins.jenkinslint.check.SlaveDescriptionChecker;
import org.jenkins.ci.plugins.jenkinslint.check.SlaveLabelChecker;
import org.jenkins.ci.plugins.jenkinslint.check.SlaveVersionChecker;
import org.jenkins.ci.plugins.jenkinslint.check.TimeoutChecker;
import org.jenkins.ci.plugins.jenkinslint.check.TimerTriggerChecker;
import org.jenkins.ci.plugins.jenkinslint.check.WindowsSlaveLaunchChecker;
import org.jenkins.ci.plugins.jenkinslint.check.*;

import java.io.IOException;
import java.util.ArrayList;
@@ -97,6 +75,7 @@ protected void reloadCheckList() {
checkList.add(new GroovySystemExitChecker());
checkList.add(new GitRefChecker());
checkList.add(new TimerTriggerChecker());
checkList.add(new GitRefSubmoduleChecker());
}

protected void reloadSlaveCheckList() {
@@ -18,6 +18,12 @@ GitRefCheckerDesc=\
GitRefCheckerSeverity=Low
GitRefSubmoduleCheckerDesc=\
When setting Jenkins Jobs with Git SCM you might speed up the cloning time if \
you use git ref repos when cloning submodules.\
GitRefSubmoduleCheckerSeverity=Low
GitShallowCheckerDesc=\
When setting Jenkins Jobs with Git SCM Artifact you might speed up the \
cloning time if you use shallow cloning.\
@@ -83,6 +83,7 @@ public void testUITable() throws Exception {
assertTrue(content.contains(htmlLint("GroovySystemExitChecker", "JL-17")));
assertTrue(content.contains(htmlLint("GitRefChecker", "JL-18")));
assertTrue(content.contains(htmlLint("TimerTriggerChecker", "JL-19")));
assertTrue(content.contains(htmlLint("GitRefSubmoduleChecker", "JL-20")));
}

private String htmlLint (String name, String id) {
Oops, something went wrong.

0 comments on commit a581519

Please sign in to comment.
You can’t perform that action at this time.