-
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
Nov 11, 2019
1 parent
e1344e1
commit 0355151
Showing
25 changed files
with
377 additions
and
330 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
28 changes: 28 additions & 0 deletions
28
src/main/java/pl/touk/sputnik/connector/gerrit/GerritScoreLabeler.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,28 @@ | ||
package pl.touk.sputnik.connector.gerrit; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import pl.touk.sputnik.engine.score.Score; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
class GerritScoreLabeler { | ||
private final String scorePassingKey; | ||
private final String scoreFailingKey; | ||
private final short scorePassingValue; | ||
private final short scoreFailingValue; | ||
|
||
Map<String, Short> getReviewLabel(Score score) { | ||
switch (score) { | ||
case PASS: | ||
return Collections.singletonMap(scorePassingKey, scorePassingValue); | ||
case FAIL: | ||
return Collections.singletonMap(scoreFailingKey, scoreFailingValue); | ||
default: | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
} |
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,9 @@ | ||
package pl.touk.sputnik.engine.score; | ||
|
||
public enum Score { | ||
PASS, | ||
FAIL, | ||
NONE | ||
|
||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/pl/touk/sputnik/engine/score/ScoreStrategies.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,46 @@ | ||
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 ScoreStrategies { | ||
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) { | ||
String scoreStrategy = configuration.getProperty(GeneralOption.SCORE_STRATEGY); | ||
notBlank(scoreStrategy); | ||
|
||
String myS = scoreStrategy.toUpperCase(); | ||
return getScoreStrategy(scoreStrategy, myS); | ||
} | ||
|
||
@NotNull | ||
private ScoreStrategy getScoreStrategy(String aScoreStrategy, String aS) { | ||
switch(aS) { | ||
case NOSCORE: | ||
return ScoreStrategy.NO_SCORE; | ||
|
||
case SCOREALWAYSPASS: | ||
return ScoreStrategy.ALWAYS_PASS; | ||
|
||
case SCOREPASSIFEMPTY: | ||
return ScoreStrategy.PASS_IF_EMPTY; | ||
|
||
case SCOREPASSIFNOERRORS: | ||
return ScoreStrategy.PASS_IF_NO_ERRORS; | ||
|
||
default: | ||
log.warn("Score strategy {} not found, using default ScoreAlwaysPass", aScoreStrategy); | ||
return ScoreStrategy.ALWAYS_PASS; | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
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,82 @@ | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.review.Review; | ||
import pl.touk.sputnik.review.Severity; | ||
|
||
import java.util.Set; | ||
|
||
public enum ScoreStrategy { | ||
NO_SCORE { | ||
@Override | ||
public Score score(@NotNull Review review) { | ||
return Score.NONE; | ||
} | ||
}, | ||
|
||
ALWAYS_PASS { | ||
@Override | ||
public Score score(@NotNull Review review) { | ||
return Score.PASS; | ||
} | ||
}, | ||
|
||
PASS_IF_EMPTY { | ||
@Override | ||
public Score score(@NotNull Review review) { | ||
if (review.getTotalViolationCount() == 0) { | ||
return Score.PASS; | ||
} | ||
|
||
return Score.FAIL; | ||
} | ||
}, | ||
|
||
PASS_IF_NO_ERRORS { | ||
@Override | ||
public Score score(@NotNull Review review) { | ||
Integer errorCount = review.getViolationCount().get(Severity.ERROR); | ||
if (errorCount == null || errorCount == 0) { | ||
return Score.PASS; | ||
} | ||
|
||
return Score.FAIL; | ||
} | ||
}; | ||
|
||
|
||
private static final String NOSCORE = "NOSCORE"; | ||
private static final String SCOREALWAYSPASS = "SCOREALWAYSPASS"; | ||
private static final String SCOREPASSIFEMPTY = "SCOREPASSIFEMPTY"; | ||
private static final String SCOREPASSIFNOERRORS = "SCOREPASSIFNOERRORS"; | ||
|
||
private static final Set<String> SCORE_STRATEGY_KEYS = ImmutableSet.of( | ||
NOSCORE, SCOREALWAYSPASS, SCOREPASSIFEMPTY, SCOREPASSIFNOERRORS); | ||
|
||
public static boolean isValidScoreStrategy(String strategy) { | ||
return SCORE_STRATEGY_KEYS.contains(strategy); | ||
} | ||
|
||
@NotNull | ||
public static ScoreStrategy of(String strategy) { | ||
switch(strategy) { | ||
case NOSCORE: | ||
return ScoreStrategy.NO_SCORE; | ||
|
||
case SCOREALWAYSPASS: | ||
return ScoreStrategy.ALWAYS_PASS; | ||
|
||
case SCOREPASSIFEMPTY: | ||
return ScoreStrategy.PASS_IF_EMPTY; | ||
|
||
case SCOREPASSIFNOERRORS: | ||
return ScoreStrategy.PASS_IF_NO_ERRORS; | ||
|
||
default: | ||
return ScoreStrategy.ALWAYS_PASS; | ||
} | ||
} | ||
|
||
abstract public Score score(@NotNull Review review); | ||
} |
Oops, something went wrong.