Skip to content

Commit

Permalink
[JENKINS-46162] Support Pipeline project to be linted
Browse files Browse the repository at this point in the history
  • Loading branch information
v1v committed Aug 14, 2017
1 parent 54efefb commit afb3f00
Show file tree
Hide file tree
Showing 24 changed files with 115 additions and 8 deletions.
Expand Up @@ -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);
}
Expand Down
Expand Up @@ -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
*/
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand Up @@ -79,4 +79,7 @@ public class ArtifactCheckerTestCase extends AbstractTestCase {
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -61,4 +61,8 @@ public class BFACheckerTestCase extends AbstractTestCase {
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
}
}
Expand Up @@ -67,4 +67,7 @@ public class CleanupWorkspaceCheckerTestCase extends AbstractTestCase {
assertFalse(checker.isIgnored(project.getDescription()));
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -107,4 +107,7 @@ public class GitRefCheckerTestCase extends AbstractTestCase {
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -70,4 +70,7 @@ public class GitRefSubmoduleCheckerTestCase extends AbstractTestCase {
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -118,4 +118,7 @@ public class GitShallowCheckerTestCase extends AbstractTestCase {
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -106,4 +106,7 @@ public class GradleWrapperCheckerTestCase extends AbstractTestCase {
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)));
}
}
Expand Up @@ -24,12 +24,22 @@ public class GroovySandboxCheckerTestCase extends AbstractTestCase {
"}";

@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
Expand Down
Expand Up @@ -215,6 +215,10 @@ public class GroovySystemExitCheckerTestCase extends AbstractTestCase {
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));
Expand Down
Expand Up @@ -161,6 +161,10 @@ public class HardcodedScriptCheckerTestCase extends AbstractTestCase {
assertFalse(getMatrixProject(MULTI_LINE_WITH_COMMENTS_BATCH, false));
checker.setIgnoreComment(false);
}
@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();
if (isUnix) {
Expand Down
Expand Up @@ -79,4 +79,7 @@ public class JavadocCheckerTestCase extends AbstractTestCase {
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -65,4 +65,7 @@ public class JobAssignedLabelCheckerTestCase extends AbstractTestCase {
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -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;

Expand Down Expand Up @@ -59,4 +60,12 @@ public class JobDescriptionCheckerTestCase extends AbstractTestCase {
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));
}
}
Expand Up @@ -36,4 +36,8 @@ public class JobLogRotatorCheckerTestCase extends AbstractTestCase {
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
}
}
Expand Up @@ -39,4 +39,8 @@ public class JobNameCheckerTestCase extends AbstractTestCase {
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
}
}
Expand Up @@ -34,4 +34,7 @@ public class MavenJobTypeCheckerTestCase extends AbstractTestCase {
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -31,4 +31,7 @@ public class MultibranchJobTypeCheckerTestCase extends AbstractTestCase {
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -62,4 +62,7 @@ public class NullSCMCheckerTestCase extends AbstractTestCase {
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -31,4 +31,7 @@ public class PollingSCMTriggerCheckerTestCase extends AbstractTestCase {
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
@Test public void testWorkflowJob() throws Exception {
assertFalse(checker.executeCheck(createWorkflow(null, true)));
}
}
Expand Up @@ -132,6 +132,10 @@ public class TimeoutCheckerTestCase extends AbstractTestCase {
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");
Expand Down
Expand Up @@ -142,4 +142,7 @@ public class TimerTriggerCheckerTestCase extends AbstractTestCase {
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 afb3f00

Please sign in to comment.