Skip to content

Commit

Permalink
[JENKINS-42268] Bfa (#11)
Browse files Browse the repository at this point in the history
* [JENKINS-42268] Initial approach to detect whether BFA has been installed 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

* Enabling Check

* i18 support

* Removed imports

* Fixed asserts

* [JENKINS-42268] Using the full classname of the property

* [JENKINS-42268] Using Job class to avoid casing each jobtype

* Removed unused imports and speeding up BFA Test Case with the AbstractCheckerTestCase

* Fixed wrong TestCase name

* Added missing bits and pieces in the Messages and also UI Testing

* Fixed missing Jelly XSS security bit and also added the sortable class in the JobAction

* Added _blank attribute

* Fixed cosmetic message

* Fixed Log severity and trace
  • Loading branch information
v1v committed Jul 29, 2017
1 parent a581519 commit dca5fad
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 22 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
Expand Up @@ -5,19 +5,13 @@
import hudson.model.Node;
import hudson.model.RootAction;
import jenkins.model.Jenkins;
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.*;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import java.io.IOException;
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;

@ExportedBean
@Extension
Expand Down
@@ -1,7 +1,6 @@
package org.jenkins.ci.plugins.jenkinslint;

import hudson.model.Action;
import hudson.model.Api;
import hudson.model.Computer;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractAction;
import org.jenkins.ci.plugins.jenkinslint.model.InterfaceSlaveCheck;
Expand Down
@@ -1,11 +1,9 @@
package org.jenkins.ci.plugins.jenkinslint;

import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Computer;
import hudson.model.TransientComputerActionFactory;
import hudson.model.TransientProjectActionFactory;

import java.util.Collection;
import java.util.Collections;
Expand Down
@@ -0,0 +1,49 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.model.Item;
import hudson.model.Job;
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(Messages.BFACheckerDesc());
this.setSeverity(Messages.BFACheckerSeverity());
}

public boolean executeCheck(Item item) {
LOG.log(Level.FINE, "executeCheck " + item);
boolean found = false;
if (Jenkins.getInstance().pluginManager.getPlugin("build-failure-analyzer") != null) {
found = isDoNotScan(((Job) item).getProperty("com.sonyericsson.jenkins.plugins.bfa.model.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;
}

}
Expand Up @@ -36,7 +36,7 @@ public boolean executeCheck(Item item) {
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);
LOG.log(Level.FINEST, "Reference has been found: " + reference);
status = ((String) reference).isEmpty();
}
}
Expand Down
Expand Up @@ -76,6 +76,7 @@ protected void reloadCheckList() {
checkList.add(new GitRefChecker());
checkList.add(new TimerTriggerChecker());
checkList.add(new GitRefSubmoduleChecker());
checkList.add(new BFAChecker());
}

protected void reloadSlaveCheckList() {
Expand Down
@@ -1,5 +1,5 @@
<?jelly escape-by-default='false'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout">
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout">
<j:getStatic var="permission" className="hudson.model.Item" field="CREATE"/>
<l:layout norefresh="true" permission="${permission}" title="${%Jenkins.Lint.Title}">
<l:side-panel>
Expand Down Expand Up @@ -75,7 +75,7 @@
</thead>
<tbody id="jenkinsLintDescriptionTableBody">
<tr>
<td><b>Description:</b> ${checker.getDescription()}</td>
<td><b>Description:</b> <j:out value="${checker.getDescription()}"/></td>
</tr>
<tr>
<td><b>Severity:</b> ${checker.getSeverity()}</td>
Expand All @@ -97,7 +97,7 @@
</thead>
<tbody id="jenkinsLintDescriptionTableBody">
<tr>
<td><b>Description:</b> ${check.getDescription()}</td>
<td><b>Description:</b> <j:out value="${check.getDescription()}"/></td>
</tr>
<tr>
<td><b>Severity:</b> ${check.getSeverity()}</td>
Expand Down
Expand Up @@ -17,7 +17,7 @@
${%Jenkins.Lint.Description.1}
${%Jenkins.Lint.Description.2}
<br/>
<table class="pane bigtable" id="jenkinsLintDescriptionTable">
<table class="sortable pane bigtable" id="jenkinsLintDescriptionTable">
<thead>
<tr>
<th class="pane-header">Status</th>
Expand Down
Expand Up @@ -5,6 +5,13 @@ ArtifactCheckerDesc=\

ArtifactCheckerSeverity=Medium

BFACheckerDesc=\
Categorise and analyse failed builds automatically helps to knowledge share and collaborate within a team and other \
teams.<br/>There are some plugins which might help you to do so, for instance: \
<a href="https://plugins.jenkins.io/build-failure-analyzer" target="_blank"> Build Failure Analyzer</a>.\

BFACheckerSeverity=Low

CleanupWorkspaceCheckerDesc=\
There are some builds which demand a lot of disc space. Some builds \
might run out of space during the build itself and cause build errors. \
Expand Down Expand Up @@ -102,7 +109,7 @@ MavenJobTypeCheckerSeverity=High
MultibranchJobTypeCheckerDesc=\
Polling in Jenkins is inefficient, MultibranchJobProject polls for new branches \
and also every branch polls for new changes.<br/>It's strongly recommended \
to use another approach, such as: 1webhooks, Configuration as Code, ...\
to use another approach, such as: WebHooks, Configuration as Code, ...\

MultibranchJobTypeCheckerSeverity=High

Expand Down Expand Up @@ -138,13 +145,12 @@ SlaveVersionCheckerSeverity=Medium
TimeoutCheckerDesc=\
There are some builds which might hang forever and cause inaction in the \
build queue and slaves. It's recommended to control those time based<br/> on the \
<a href=https://wiki.jenkins-ci.org/display/JENKINS/Build-timeout+Plugin>\
build timeout plugin</a>.\
<a href=https://wiki.jenkins-ci.org/display/JENKINS/Build-timeout+Plugin> build timeout plugin</a>.\

TimeoutCheckerSeverity=Medium

TimerTriggerCheckerDesc=\
When setting Jenkins Jobs with some Timer trigger configuration use the symbol H (for hash) wherever possible \
When setting Jenkins Jobs with some Timer trigger configuration use the symbol H (for "hash") wherever possible \
<br/>to allow periodically scheduled tasks to produce even load on the system.\

TimerTriggerCheckerSeverity=Low
Expand Down
Expand Up @@ -84,6 +84,7 @@ public void testUITable() throws Exception {
assertTrue(content.contains(htmlLint("GitRefChecker", "JL-18")));
assertTrue(content.contains(htmlLint("TimerTriggerChecker", "JL-19")));
assertTrue(content.contains(htmlLint("GitRefSubmoduleChecker", "JL-20")));
assertTrue(content.contains(htmlLint("BFAChecker", "JL-21")));
}

private String htmlLint (String name, String id) {
Expand Down
@@ -0,0 +1,63 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty;
import hudson.matrix.MatrixProject;
import hudson.maven.MavenModuleSet;
import hudson.model.FreeStyleProject;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* BFAChecker Test Case.
*
* @author Victor Martinez
*/
public class BFACheckerTestCase extends AbstractCheckerTestCase {
private BFAChecker checker = new BFAChecker();

@Test public void testEmptyJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertTrue(checker.executeCheck(project));
}
@Test public void testDisabledBFAJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
project.addProperty(new ScannerJobProperty(true));
assertTrue(checker.executeCheck(project));
}
@Test public void testEnabledBFAJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
project.addProperty(new ScannerJobProperty(false));
project.save();
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 testMavenModule() 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 dca5fad

Please sign in to comment.