forked from v1v/jenkinslint-plugin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[JENKINS-42268] Initial approach to detect whether BFA has been insta…
…lled and enabled in every job unless using the control comment to force some way of declaring why it was not enabled [JENKINS-42268] Added Maven and Matrix projects
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/org/jenkins/ci/plugins/jenkinslint/check/BFAChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.jenkins.ci.plugins.jenkinslint.check; | ||
|
||
import hudson.matrix.MatrixProject; | ||
import hudson.maven.MavenModuleSet; | ||
import hudson.model.Descriptor; | ||
import hudson.model.Item; | ||
import hudson.model.Project; | ||
import hudson.tasks.ArtifactArchiver; | ||
import hudson.tasks.Publisher; | ||
import hudson.util.DescribableList; | ||
import jenkins.model.Jenkins; | ||
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck; | ||
|
||
import java.util.logging.Level; | ||
|
||
/** | ||
* @author Victor Martinez | ||
*/ | ||
public class BFAChecker extends AbstractCheck { | ||
|
||
public BFAChecker() { | ||
super(); | ||
this.setDescription("When setting Jenkins Jobs with Archive Artifact post build you should either set which "+ | ||
"artifacts or remove this unused publisher phase.<br/>" + | ||
"Otherwise the archive artifact phase may not match what you expect."); | ||
this.setSeverity("Low"); | ||
} | ||
|
||
public boolean executeCheck(Item item) { | ||
LOG.log(Level.FINE, "executeCheck " + item); | ||
boolean found = false; | ||
if (Jenkins.getInstance().pluginManager.getPlugin("build-failure-analyzer") != null) { | ||
if (Jenkins.getInstance().pluginManager.getPlugin("maven-plugin")!=null) { | ||
if (item instanceof MavenModuleSet) { | ||
found = isDoNotScan(((MavenModuleSet) item).getProperty("ScannerJobProperty")); | ||
} | ||
} | ||
if (item instanceof Project) { | ||
found = isDoNotScan(((Project) item).getProperty("ScannerJobProperty")); | ||
} | ||
if (item instanceof MatrixProject) { | ||
found = isDoNotScan(((MatrixProject) item).getProperty("ScannerJobProperty")); | ||
} | ||
} | ||
return found; | ||
} | ||
|
||
private boolean isDoNotScan (Object property) { | ||
boolean status = false; | ||
if (property != null) { | ||
try { | ||
Object isDoNotScan = property.getClass().getMethod("isDoNotScan", null).invoke(property); | ||
if (isDoNotScan instanceof Boolean) { | ||
status = ((Boolean) isDoNotScan).booleanValue(); | ||
LOG.log(Level.FINE, "isDoNotScan " + status); | ||
} | ||
} catch (Exception e) { | ||
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause()); | ||
status = false; | ||
} | ||
} else { | ||
status = true; | ||
} | ||
return status; | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/test/java/org/jenkins/ci/plugins/jenkinslint/check/BFACheckerTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.jenkins.ci.plugins.jenkinslint.check; | ||
|
||
import hudson.matrix.MatrixProject; | ||
import hudson.maven.MavenModuleSet; | ||
import hudson.model.FreeStyleProject; | ||
import com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* BFAChecker Test Case. | ||
* | ||
* @author Victor Martinez | ||
*/ | ||
public class BFACheckerTestCase { | ||
private BFAChecker checker = new BFAChecker(); | ||
|
||
@Rule public JenkinsRule j = new JenkinsRule(); | ||
@Test public void testEmptyJob() throws Exception { | ||
FreeStyleProject project = j.createFreeStyleProject(); | ||
assertFalse(checker.executeCheck(project)); | ||
} | ||
@Test public void testDisabledBFAJob() throws Exception { | ||
FreeStyleProject project = j.createFreeStyleProject(); | ||
project.addProperty(new ScannerJobProperty(true)); | ||
assertFalse(checker.executeCheck(project)); | ||
} | ||
@Test public void testEnabledBFAJob() throws Exception { | ||
FreeStyleProject project = j.createFreeStyleProject(); | ||
project.addProperty(new ScannerJobProperty(false)); | ||
assertFalse(checker.executeCheck(project)); | ||
} | ||
@Test public void testMatrixProject() throws Exception { | ||
MatrixProject project = j.createMatrixProject("doNotScan"); | ||
project.addProperty(new ScannerJobProperty(true)); | ||
project.save(); | ||
assertTrue(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createMatrixProject("Scan"); | ||
project.addProperty(new ScannerJobProperty(false)); | ||
project.save(); | ||
assertFalse(checker.executeCheck(project)); | ||
} | ||
@Test public void testMavenModuleJobbWithGroovy() throws Exception { | ||
MavenModuleSet project = j.createMavenProject(); | ||
project.addProperty(new ScannerJobProperty(true)); | ||
project.save(); | ||
assertTrue(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createMavenProject("Scan"); | ||
project.addProperty(new ScannerJobProperty(false)); | ||
project.save(); | ||
assertFalse(checker.executeCheck(project)); | ||
} | ||
@Test public void testControlComment() throws Exception { | ||
FreeStyleProject project = j.createFreeStyleProject(); | ||
assertFalse(checker.isIgnored(project.getDescription())); | ||
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName()); | ||
assertTrue(checker.isIgnored(project.getDescription())); | ||
} | ||
} |