Skip to content
Permalink
Browse files

[JENKINS-46162] Support Pipeline project to be linted

  • Loading branch information...
v1v committed Aug 13, 2017
1 parent 09bef1c commit d078eea270e307f693d247a6a53b2cad05768a40
Showing with 114 additions and 8 deletions.
  1. +1 −2 src/main/java/org/jenkins/ci/plugins/jenkinslint/check/GroovySandboxChecker.java
  2. +18 −2 src/main/java/org/jenkins/ci/plugins/jenkinslint/check/JobDescriptionChecker.java
  3. +10 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/AbstractTestCase.java
  4. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/ArtifactCheckerTestCase.java
  5. +4 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/BFACheckerTestCase.java
  6. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/CleanupWorkspaceCheckerTestCase.java
  7. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/GitRefCheckerTestCase.java
  8. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/GitRefSubmoduleCheckerTestCase.java
  9. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/GitShallowCheckerTestCase.java
  10. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/GradleWrapperCheckerTestCase.java
  11. +14 −4 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/GroovySandboxCheckerTestCase.java
  12. +4 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/GroovySystemExitCheckerTestCase.java
  13. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/HardcodedScriptCheckerTestCase.java
  14. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/JavadocCheckerTestCase.java
  15. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/JobAssignedLabelCheckerTestCase.java
  16. +9 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/JobDescriptionCheckerTestCase.java
  17. +4 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/JobLogRotatorCheckerTestCase.java
  18. +4 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/JobNameCheckerTestCase.java
  19. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/MavenJobTypeCheckerTestCase.java
  20. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/MultibranchJobTypeCheckerTestCase.java
  21. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/NullSCMCheckerTestCase.java
  22. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/PollingSCMTriggerCheckerTestCase.java
  23. +4 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/TimeoutCheckerTestCase.java
  24. +3 −0 src/test/java/org/jenkins/ci/plugins/jenkinslint/check/TimerTriggerCheckerTestCase.java
@@ -24,8 +24,7 @@ public boolean executeCheck(Item item) {
// Pipeline support
if (item.getClass().getSimpleName().equals("WorkflowJob")) {
try {
Method method = item.getClass().getMethod("getDefinition", null);
Object getDefinition = method.invoke(item);
Object getDefinition = item.getClass().getMethod("getDefinition", null).invoke(item);
if (getDefinition.getClass().getSimpleName().equals("CpsFlowDefinition")) {
return !isSandbox(getDefinition);
}
@@ -4,6 +4,9 @@
import hudson.model.Item;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

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

/**
* @author Victor Martinez
*/
@@ -17,9 +20,22 @@ public JobDescriptionChecker(boolean enabled) {

public boolean executeCheck(Item item) {
if (item instanceof AbstractItem) {
return (((AbstractItem) item).getDescription() == null
|| ((AbstractItem) item).getDescription().length() == 0);
return isDescription(((AbstractItem) item).getDescription());
}
if (item.getClass().getSimpleName().equals("WorkflowJob")) {
try {
Object getDescription = item.getClass().getMethod("getDescription", null).invoke(item);
if (getDescription instanceof String) {
return isDescription((String) getDescription);
}
} catch (Exception e) {
LOG.log(Level.FINE, "Exception " + e.getMessage(), e.getCause());
}
}
return false;
}

private boolean isDescription(String description) {
return (description == null || description.length() == 0);
}
}
@@ -1,6 +1,8 @@
package org.jenkins.ci.plugins.jenkinslint;

import hudson.model.Item;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Before;
import org.junit.ClassRule;
import org.jvnet.hudson.test.JenkinsRule;
@@ -22,4 +24,12 @@ public void setUp() throws IOException, InterruptedException {
i.delete();
}
}

protected WorkflowJob createWorkflow(String script, boolean sandbox) throws Exception {
WorkflowJob project = j.jenkins.createProject(WorkflowJob.class, "testPipeline");
if (script!=null && !script.isEmpty()) {
project.setDefinition(new CpsFlowDefinition(script, sandbox));
}
return project;
}
}
@@ -79,4 +79,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -61,4 +61,8 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertTrue(checker.executeCheck(createWorkflow(null, true)));
//TODO: missing assertFalse
}
}
@@ -67,4 +67,7 @@
assertFalse(checker.isIgnored(project.getDescription()));
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -107,4 +107,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -70,4 +70,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -118,4 +118,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -106,4 +106,7 @@
project.getPrebuilders().add(new hudson.plugins.gradle.Gradle("description","switches","tasks","rootBuildScriptDir","buildFile","gradleName", true, false, false, false));
assertFalse(checker.executeCheck(project));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -24,12 +24,22 @@
"}";

@Test
public void testGroovySandboxInPipeline() throws Exception {
WorkflowJob project = j.jenkins.createProject(WorkflowJob.class, "testSandbox");
project.setDefinition(new CpsFlowDefinition(PIPELINE, true));
public void testEmptyAndNullPipeline() throws Exception {
WorkflowJob project = createWorkflow(null, false);
assertFalse(checker.executeCheck(project));
project.delete();project = createWorkflow("", false);
assertFalse(checker.executeCheck(project));
project.setDefinition(new CpsFlowDefinition(PIPELINE, false));
project.delete();
}

@Test
public void testGroovySandboxInPipeline() throws Exception {
WorkflowJob project = createWorkflow(PIPELINE, false);
assertTrue(checker.executeCheck(project));
project.delete();
project = (createWorkflow(PIPELINE, true));
assertFalse(checker.executeCheck(project));
project.delete();
}

@Test
@@ -215,6 +215,10 @@
project.addProperty(createDynamicReferenceParameter("System.exit(0)"));
assertTrue(checker.executeCheck(project));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
//TODO: missing assertTrue
}

private ParametersDefinitionProperty createChoiceParameter(String content) {
GroovyScript script = new GroovyScript(createScript(content),createScript(content));
@@ -132,6 +132,9 @@
assertFalse(getMatrixProject(MULTI_LINE_WITH_BLANK1_BASH, true));
assertTrue(getMatrixProject(MULTI_LINE_WITH_BLANK2_BASH, true));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}

private boolean getMatrixProject(String shell, boolean isUnix) throws Exception {
MatrixProject project = j.createMatrixProject();
@@ -79,4 +79,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -65,4 +65,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -4,6 +4,7 @@
import hudson.maven.MavenModuleSet;
import hudson.model.FreeStyleProject;
import org.jenkins.ci.plugins.jenkinslint.AbstractTestCase;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

@@ -59,4 +60,12 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJobDescription() throws Exception {
WorkflowJob project =createWorkflow(null, true);
assertTrue(checker.executeCheck(project));
project.delete();
project = (createWorkflow(null, true));
project.setDescription("Some Description");
assertFalse(checker.executeCheck(project));
}
}
@@ -36,4 +36,8 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertTrue(checker.executeCheck(createWorkflow(null, true)));
//TODO: missing assertFalse
}
}
@@ -39,4 +39,8 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
//TODO: missing assertTrue
}
}
@@ -34,4 +34,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -31,4 +31,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -62,4 +62,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -31,4 +31,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
@@ -132,6 +132,10 @@
assertTrue(checker.executeCheck(project));
project.delete();
}
@Test public void testWorkflowJob() throws Exception {
assertTrue(checker.executeCheck(createWorkflow(null, true)));
//TODO: missing assertFalse
}

private BuildTimeoutWrapper createNoActivityTimeOut() {
NoActivityTimeOutStrategy strategy = new NoActivityTimeOutStrategy("120");
@@ -142,4 +142,7 @@
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}

0 comments on commit d078eea

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