This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from philipa/master
Jacoco coverage plugin
- Loading branch information
Showing
8 changed files
with
472 additions
and
7 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
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
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
131 changes: 131 additions & 0 deletions
131
src/main/java/hudson/plugins/cigame/rules/plugins/jacoco/DefaultJacocoRule.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,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(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/hudson/plugins/cigame/rules/plugins/jacoco/JacocoRuleSet.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,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)); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/resources/hudson/plugins/cigame/rules/plugins/jacoco/Messages.properties
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,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 |
Oops, something went wrong.