-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exit sputnik with error code if checks fail
This allows scripts/tools to take the return result into account. Furthermore, Extensions of sputnik like the sputnik-maven-plugin can use a return value of the Engine to communicate the result of a run and do something like fail a build. Implement this by adding a Score object (with the review label and the score value as fields) and return that in Engine.run(). If a failing score value (< 0) is returned, then call System.exit with the score as the error status.
- Loading branch information
Marquis Wong
authored and
Marquis Wong
committed
Oct 31, 2019
1 parent
e1344e1
commit 3e0dc9d
Showing
23 changed files
with
297 additions
and
283 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.review.Review; | ||
|
||
@Slf4j | ||
@EqualsAndHashCode | ||
public class NoScore implements ScoreStrategy { | ||
|
||
@Override | ||
public Score score(@NotNull Review review) { | ||
log.info("No score for review"); | ||
return null; | ||
} | ||
} |
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 pl.touk.sputnik.engine.score; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
@EqualsAndHashCode | ||
@ToString | ||
public class Score { | ||
private final String label; | ||
private final int score; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/pl/touk/sputnik/engine/score/ScoreAlwaysPass.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,22 @@ | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.review.Review; | ||
|
||
@Slf4j | ||
@Getter | ||
@EqualsAndHashCode | ||
@AllArgsConstructor | ||
public class ScoreAlwaysPass implements ScoreStrategy { | ||
private final Score passingScore; | ||
|
||
@Override | ||
public Score score(@NotNull Review review) { | ||
log.info("Adding static passing score {} to review", passingScore); | ||
return passingScore; | ||
} | ||
} |
20 changes: 9 additions & 11 deletions
20
...ngine/visitor/score/ScorePassIfEmpty.java → ...putnik/engine/score/ScorePassIfEmpty.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 |
---|---|---|
@@ -1,30 +1,28 @@ | ||
package pl.touk.sputnik.engine.visitor.score; | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.engine.visitor.AfterReviewVisitor; | ||
import pl.touk.sputnik.review.Review; | ||
|
||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Getter | ||
@EqualsAndHashCode | ||
@AllArgsConstructor | ||
public class ScorePassIfEmpty implements AfterReviewVisitor { | ||
private final Map<String, Short> passingScore; | ||
private final Map<String, Short> failingScore; | ||
public class ScorePassIfEmpty implements ScoreStrategy { | ||
private final Score passingScore; | ||
private final Score failingScore; | ||
|
||
@Override | ||
public void afterReview(@NotNull Review review) { | ||
public Score score(@NotNull Review review) { | ||
if (review.getTotalViolationCount() == 0) { | ||
log.info("Adding passing score {} for no violation(s) found", passingScore); | ||
review.setScores(passingScore); | ||
return; | ||
return passingScore; | ||
} | ||
|
||
log.info("Adding failing score {} for {} violations found", failingScore, review.getTotalViolationCount()); | ||
review.setScores(failingScore); | ||
return failingScore; | ||
} | ||
} |
20 changes: 9 additions & 11 deletions
20
...ne/visitor/score/ScorePassIfNoErrors.java → ...nik/engine/score/ScorePassIfNoErrors.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 |
---|---|---|
@@ -1,32 +1,30 @@ | ||
package pl.touk.sputnik.engine.visitor.score; | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.engine.visitor.AfterReviewVisitor; | ||
import pl.touk.sputnik.review.Review; | ||
import pl.touk.sputnik.review.Severity; | ||
|
||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Getter | ||
@EqualsAndHashCode | ||
@AllArgsConstructor | ||
public class ScorePassIfNoErrors implements AfterReviewVisitor { | ||
private final Map<String, Short> passingScore; | ||
private final Map<String, Short> failingScore; | ||
public class ScorePassIfNoErrors implements ScoreStrategy { | ||
private final Score passingScore; | ||
private final Score failingScore; | ||
|
||
@Override | ||
public void afterReview(@NotNull Review review) { | ||
public Score score(@NotNull Review review) { | ||
Integer errorCount = review.getViolationCount().get(Severity.ERROR); | ||
if (errorCount == null || errorCount == 0) { | ||
log.info("Adding passing score {} for no errors found", passingScore); | ||
review.setScores(passingScore); | ||
return; | ||
return passingScore; | ||
} | ||
|
||
log.info("Adding failing score {} for {} errors found", failingScore, errorCount); | ||
review.setScores(failingScore); | ||
return failingScore; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/pl/touk/sputnik/engine/score/ScoreStrategy.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,8 @@ | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.review.Review; | ||
|
||
public interface ScoreStrategy { | ||
Score score(@NotNull Review review); | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/pl/touk/sputnik/engine/score/ScoreStrategyFactory.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,48 @@ | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.configuration.Configuration; | ||
import pl.touk.sputnik.configuration.GeneralOption; | ||
|
||
import static org.apache.commons.lang3.Validate.notBlank; | ||
|
||
@Slf4j | ||
public class ScoreStrategyFactory { | ||
private static final String NOSCORE = "NOSCORE"; | ||
private static final String SCOREALWAYSPASS = "SCOREALWAYSPASS"; | ||
private static final String SCOREPASSIFEMPTY = "SCOREPASSIFEMPTY"; | ||
private static final String SCOREPASSIFNOERRORS = "SCOREPASSIFNOERRORS"; | ||
|
||
@NotNull | ||
public ScoreStrategy buildScoreStrategy(Configuration configuration) { | ||
Score passingScore = new Score( | ||
configuration.getProperty(GeneralOption.SCORE_PASSING_KEY), | ||
Short.valueOf(configuration.getProperty(GeneralOption.SCORE_PASSING_VALUE)) | ||
); | ||
Score failingScore = new Score( | ||
configuration.getProperty(GeneralOption.SCORE_FAILING_KEY), | ||
Short.valueOf(configuration.getProperty(GeneralOption.SCORE_FAILING_VALUE)) | ||
); | ||
String scoreStrategy = configuration.getProperty(GeneralOption.SCORE_STRATEGY); | ||
notBlank(scoreStrategy); | ||
|
||
switch(scoreStrategy.toUpperCase()) { | ||
case NOSCORE: | ||
return new NoScore(); | ||
|
||
case SCOREALWAYSPASS: | ||
return new ScoreAlwaysPass(passingScore); | ||
|
||
case SCOREPASSIFEMPTY: | ||
return new ScorePassIfEmpty(passingScore, failingScore); | ||
|
||
case SCOREPASSIFNOERRORS: | ||
return new ScorePassIfNoErrors(passingScore, failingScore); | ||
|
||
default: | ||
log.warn("Score strategy {} not found, using default ScoreAlwaysPass", scoreStrategy); | ||
return new ScoreAlwaysPass(passingScore); | ||
} | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
src/main/java/pl/touk/sputnik/engine/visitor/score/NoScore.java
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
src/main/java/pl/touk/sputnik/engine/visitor/score/ScoreAlwaysPass.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.