Skip to content

Commit

Permalink
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
v1v committed Jul 29, 2017
1 parent a581519 commit 73fb88f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -174,6 +174,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.sonyericsson.jenkins.plugins.bfa</groupId>
<artifactId>build-failure-analyzer</artifactId>
<version>1.17.2</version>
<scope>test</scope>
</dependency>

<!-- JENKINS-29427 -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
@@ -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;
}

}
@@ -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()));
}
}

0 comments on commit 73fb88f

Please sign in to comment.