Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #16 from philipa/master
Browse files Browse the repository at this point in the history
Jacoco coverage plugin
  • Loading branch information
simschla committed Jun 30, 2016
2 parents 983cc63 + c86cc2c commit 8dbf1a8
Show file tree
Hide file tree
Showing 8 changed files with 472 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -16,6 +16,7 @@ Note that the plugin version 1.13+ requires Hudson 1.320 to work and the followi
- Warnings 3.0
- Violations 0.5.4
- analysis-core 1.0
- Jacoco 1.0.18

The plugin is not activated for all jobs at start, each separate job has to activate the game. The game can also be de-activated in one job if some large merge activity is going to take place. To activate the game for a job, go to the job configuration page, click "Add post-build action" and select "Continuous integration game" from the list of available post-build actions.

Expand Down Expand Up @@ -69,6 +70,8 @@ Rules that depend on other plugins:
#####Checkstyle Plugin. [link](https://wiki.jenkins-ci.org/display/JENKINS/Checkstyle+Plugin)
- Adding/removing a checkstyle warning = -1/+1.

#####Jacoco Plugin. [link](https://wiki.jenkins-ci.org/display/JENKINS/Jacoco+Plugin)
- Reducing/increasing coverage = -10/+10 for each percentage of line coverage, with a minimum of -1/+1.

###Adding rules to the game

Expand Down
20 changes: 13 additions & 7 deletions pom.xml
Expand Up @@ -141,7 +141,19 @@
<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>analysis-core</artifactId>
<version>1.58</version>
<version>1.58</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jacoco</artifactId>
<version>1.0.18</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand All @@ -154,12 +166,6 @@
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/cigame/GameDescriptor.java
Expand Up @@ -11,6 +11,7 @@
import hudson.plugins.cigame.rules.build.BuildRuleSet;
import hudson.plugins.cigame.rules.plugins.checkstyle.CheckstyleRuleSet;
import hudson.plugins.cigame.rules.plugins.findbugs.FindBugsRuleSet;
import hudson.plugins.cigame.rules.plugins.jacoco.JacocoRuleSet;
import hudson.plugins.cigame.rules.plugins.opentasks.OpenTasksRuleSet;
import hudson.plugins.cigame.rules.plugins.pmd.PmdRuleSet;
import hudson.plugins.cigame.rules.plugins.violation.ViolationsRuleSet;
Expand Down Expand Up @@ -58,6 +59,7 @@ public RuleBook getRuleBook() {
addRuleSetIfAvailable(rulebook, new FindBugsRuleSet());
addRuleSetIfAvailable(rulebook, new WarningsRuleSet());
addRuleSetIfAvailable(rulebook, new CheckstyleRuleSet());
addRuleSetIfAvailable(rulebook, new JacocoRuleSet());
}
return rulebook;
}
Expand Down
@@ -0,0 +1,131 @@
package hudson.plugins.cigame.rules.plugins.jacoco;

import static java.lang.Math.ceil;
import static java.lang.Math.floor;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.plugins.cigame.model.AggregatableRule;
import hudson.plugins.cigame.model.RuleResult;
import hudson.plugins.cigame.util.ActionRetriever;
import hudson.plugins.jacoco.JacocoBuildAction;
import hudson.plugins.jacoco.model.Coverage;

import java.util.Collection;
import java.util.List;

/**
* Default rule for the Jacoco plugin.
*
* @author Philip Aston
*/
public class DefaultJacocoRule implements AggregatableRule<Double> {

private static final RuleResult<Double> EMPTY_RESULT =
new RuleResult<Double>(0.0, "", 0d);

private final int pointsForIncreasingCoverage;
private final int pointsForReducingCoverage;

public DefaultJacocoRule(
int pointsForReducingCoveragePerCent,
int pointsForIncreasingCoveragePerCent) {
this.pointsForReducingCoverage = pointsForReducingCoveragePerCent;
this.pointsForIncreasingCoverage = pointsForIncreasingCoveragePerCent;
}

@Override
public RuleResult<?> aggregate(Collection<RuleResult<Double>> results) {
double score = 0.0;
double sumOfPercentageChanges = 0d;

for (RuleResult<Double> result : results) {
score += result.getPoints();
sumOfPercentageChanges += result.getAdditionalData();
}

if (sumOfPercentageChanges >= 0) {
return new RuleResult<Void>(
score,
Messages.JacocoRuleSet_DefaultRule_IncreasedCoverage(sumOfPercentageChanges * 0.01));
}
else {
return new RuleResult<Void>(
score,
Messages.JacocoRuleSet_DefaultRule_ReducedCoverage(sumOfPercentageChanges * -0.01));
}
}

@Override
public RuleResult<Double> evaluate(AbstractBuild<?, ?> previousBuild,
AbstractBuild<?, ?> build) {

if (build == null ||
build.getResult() == null ||
build.getResult().isWorseOrEqualTo(Result.FAILURE)) {
return EMPTY_RESULT;
}

if (previousBuild == null ||
previousBuild.getResult().isWorseOrEqualTo(Result.FAILURE)) {
return EMPTY_RESULT;
}

final List<JacocoBuildAction> currentActions =
ActionRetriever.getResult(build, Result.UNSTABLE, JacocoBuildAction.class);

final Coverage currentCoverage = getLineCoverage(currentActions);

if (!currentCoverage.isInitialized()) {
return EMPTY_RESULT;
}

final List<JacocoBuildAction> previousActions =
ActionRetriever.getResult(previousBuild, Result.UNSTABLE, JacocoBuildAction.class);

final Coverage previousCoverage = getLineCoverage(previousActions);

if (!previousCoverage.isInitialized()) {
return EMPTY_RESULT;
}

if (previousCoverage.equals(currentCoverage)) {
return EMPTY_RESULT;
}

final double percentage = currentCoverage.getPercentageFloat() - previousCoverage.getPercentageFloat();

if (percentage >= 0) {
return new RuleResult<Double>(
ceil(percentage * pointsForIncreasingCoverage),
Messages.JacocoRuleSet_DefaultRule_IncreasedCoverage(percentage * 0.01),
percentage);
}
else {
return new RuleResult<Double>(
floor(percentage * -1 * pointsForReducingCoverage),
Messages.JacocoRuleSet_DefaultRule_ReducedCoverage(percentage * -0.01),
percentage);
}
}

@Override
public RuleResult<Double> evaluate(AbstractBuild<?, ?> build) {
throw new UnsupportedOperationException();
}

private static Coverage getLineCoverage(List<? extends JacocoBuildAction> actions) {
final Coverage totalCoverage = new Coverage();

for (JacocoBuildAction action : actions) {
final Coverage lineCoverage = action.getLineCoverage();
totalCoverage.accumulatePP(lineCoverage.getMissed(), lineCoverage.getCovered());
}

return totalCoverage;
}

@Override
public String getName() {
return Messages.JacocoRuleSet_DefaultRule_Name();
}
}
@@ -0,0 +1,15 @@
package hudson.plugins.cigame.rules.plugins.jacoco;

import hudson.plugins.cigame.rules.plugins.PluginRuleSet;

public class JacocoRuleSet extends PluginRuleSet {

public JacocoRuleSet() {
super("jacoco", Messages.JacocoRuleSet_Title()); //$NON-NLS-1$ //$NON-NLS-2$
}

@Override
protected void loadRules() {
add(new DefaultJacocoRule(-10, +10));
}
}
@@ -0,0 +1,4 @@
JacocoRuleSet.DefaultRule.Name=Jacoco coverage
JacocoRuleSet.DefaultRule.IncreasedCoverage=Coverage was increased by {0,number,##0.0#%}
JacocoRuleSet.DefaultRule.ReducedCoverage=Coverage was reduced by {0,number,##0.0#%}
JacocoRuleSet.Title=Jacoco coverage

0 comments on commit 8dbf1a8

Please sign in to comment.