-
Notifications
You must be signed in to change notification settings - Fork 119
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.
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); | ||
} |
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.
30 changes: 0 additions & 30 deletions
30
src/main/java/pl/touk/sputnik/engine/visitor/score/ScorePassIfEmpty.java
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
src/main/java/pl/touk/sputnik/engine/visitor/score/ScorePassIfNoErrors.java
This file was deleted.
Oops, something went wrong.
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
21 changes: 21 additions & 0 deletions
21
src/test/java/pl/touk/sputnik/engine/score/ScoreAlwaysPassTest.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,21 @@ | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import pl.touk.sputnik.TestEnvironment; | ||
import pl.touk.sputnik.review.Review; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ScoreAlwaysPassTest extends TestEnvironment { | ||
|
||
@Test | ||
void shouldAddScoreToReview() { | ||
Review review = review(); | ||
|
||
Score myPassingScore = new Score("Sputnik-Pass", 1); | ||
Score myScore = new ScoreAlwaysPass(myPassingScore).score(review); | ||
|
||
assertThat(myScore).isEqualTo(myPassingScore); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/pl/touk/sputnik/engine/score/ScorePassIfEmptyTest.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,35 @@ | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import pl.touk.sputnik.review.Review; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class ScorePassIfEmptyTest { | ||
private static final Score PASSING_SCORE = new Score("Sputnik-Pass", 1); | ||
private static final Score FAILING_SCORE = new Score("Code-Review", -2); | ||
|
||
private Review reviewMock = mock(Review.class); | ||
private static final ScorePassIfEmpty SCORE_PASS_IF_EMPTY = new ScorePassIfEmpty(PASSING_SCORE, FAILING_SCORE); | ||
|
||
@Test | ||
void shouldPassIfViolationsIsEmpty() { | ||
when(reviewMock.getTotalViolationCount()).thenReturn(0); | ||
|
||
Score score = SCORE_PASS_IF_EMPTY.score(reviewMock); | ||
|
||
assertThat(score).isEqualTo(PASSING_SCORE); | ||
} | ||
|
||
@Test | ||
void shouldFailIfViolationsIsNotEmpty() { | ||
when(reviewMock.getTotalViolationCount()).thenReturn(1); | ||
|
||
Score score = SCORE_PASS_IF_EMPTY.score(reviewMock); | ||
|
||
assertThat(score).isEqualTo(FAILING_SCORE); | ||
} | ||
|
||
} |
24 changes: 11 additions & 13 deletions
24
...isitor/score/ScorePassIfNoErrorsTest.java → ...engine/score/ScorePassIfNoErrorsTest.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,48 +1,46 @@ | ||
package pl.touk.sputnik.engine.visitor.score; | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.jupiter.api.Test; | ||
import pl.touk.sputnik.review.Review; | ||
import pl.touk.sputnik.review.Severity; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
class ScorePassIfNoErrorsTest { | ||
private static final Map<String, Short> PASSING_SCORE = ImmutableMap.of("Sputnik-Pass", (short) 1); | ||
private static final Map<String, Short> FAILING_SCORE = ImmutableMap.of("Code-Review", (short) -2); | ||
private static final Score PASSING_SCORE = new Score("Sputnik-Pass", 1); | ||
private static final Score FAILING_SCORE = new Score("Code-Review", -2); | ||
|
||
private static final ScorePassIfNoErrors SCORE_PASS_IF_EMPTY = new ScorePassIfNoErrors(PASSING_SCORE, FAILING_SCORE); | ||
private Review reviewMock = mock(Review.class, RETURNS_DEEP_STUBS); | ||
|
||
@Test | ||
void shouldPassIfErrorCountIsNull() { | ||
when(reviewMock.getViolationCount().get(Severity.ERROR)).thenReturn(null); | ||
|
||
new ScorePassIfNoErrors(PASSING_SCORE, FAILING_SCORE).afterReview(reviewMock); | ||
Score score = SCORE_PASS_IF_EMPTY.score(reviewMock); | ||
|
||
verify(reviewMock).setScores(PASSING_SCORE); | ||
assertThat(score).isEqualTo(PASSING_SCORE); | ||
} | ||
|
||
@Test | ||
void shouldPassIfErrorCountIsZero() { | ||
when(reviewMock.getViolationCount().get(Severity.ERROR)).thenReturn(0); | ||
|
||
new ScorePassIfNoErrors(PASSING_SCORE, FAILING_SCORE).afterReview(reviewMock); | ||
Score score = SCORE_PASS_IF_EMPTY.score(reviewMock); | ||
|
||
verify(reviewMock).setScores(PASSING_SCORE); | ||
assertThat(score).isEqualTo(PASSING_SCORE); | ||
} | ||
|
||
@Test | ||
void shouldFailIfErrorCountIsNotZero() { | ||
when(reviewMock.getViolationCount().get(Severity.ERROR)).thenReturn(1); | ||
|
||
new ScorePassIfNoErrors(PASSING_SCORE, FAILING_SCORE).afterReview(reviewMock); | ||
Score score = SCORE_PASS_IF_EMPTY.score(reviewMock); | ||
|
||
verify(reviewMock).setScores(FAILING_SCORE); | ||
assertThat(score).isEqualTo(FAILING_SCORE); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/test/java/pl/touk/sputnik/engine/score/ScoreStrategyFactoryTest.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,71 @@ | ||
package pl.touk.sputnik.engine.score; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.jupiter.api.Test; | ||
import pl.touk.sputnik.configuration.Configuration; | ||
import pl.touk.sputnik.configuration.ConfigurationSetup; | ||
import pl.touk.sputnik.configuration.GeneralOption; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ScoreStrategyFactoryTest { | ||
@Test | ||
void shouldBuildNoScoreVisitor() { | ||
Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( | ||
GeneralOption.SCORE_STRATEGY.getKey(), "NOscore" | ||
)); | ||
|
||
assertThat(new ScoreStrategies().buildScoreStrategy(config)) | ||
.isEqualTo(new NoScore()); | ||
} | ||
|
||
@Test | ||
void shouldBuildScoreAlwaysPassVisitor() { | ||
Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( | ||
GeneralOption.SCORE_STRATEGY.getKey(), "scoreAlwaysPass", | ||
GeneralOption.SCORE_PASSING_KEY.getKey(), "Verified", | ||
GeneralOption.SCORE_PASSING_VALUE.getKey(), "2" | ||
)); | ||
|
||
assertThat(new ScoreStrategies().buildScoreStrategy(config)) | ||
.isEqualTo(new ScoreAlwaysPass(new Score("Verified", 2))); | ||
} | ||
|
||
@Test | ||
void shouldBuildScorePassIfEmptyVisitor() { | ||
Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( | ||
GeneralOption.SCORE_STRATEGY.getKey(), "SCOREPASSIFEMPTY", | ||
GeneralOption.SCORE_PASSING_KEY.getKey(), "Verified", | ||
GeneralOption.SCORE_PASSING_VALUE.getKey(), "3", | ||
GeneralOption.SCORE_FAILING_KEY.getKey(), "Verified", | ||
GeneralOption.SCORE_FAILING_VALUE.getKey(), "-3" | ||
)); | ||
|
||
assertThat(new ScoreStrategies().buildScoreStrategy(config)) | ||
.isEqualTo(new ScorePassIfEmpty(new Score("Verified", 3), new Score("Verified", -3))); | ||
} | ||
|
||
@Test | ||
void shouldBuildScorePassIfNoErrorsVisitor() { | ||
Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( | ||
GeneralOption.SCORE_STRATEGY.getKey(), "SCOREPassIfNoErrors", | ||
GeneralOption.SCORE_PASSING_KEY.getKey(), "Code-Review", | ||
GeneralOption.SCORE_PASSING_VALUE.getKey(), "1", | ||
GeneralOption.SCORE_FAILING_KEY.getKey(), "Code-Review", | ||
GeneralOption.SCORE_FAILING_VALUE.getKey(), "-2" | ||
)); | ||
|
||
assertThat(new ScoreStrategies().buildScoreStrategy(config)) | ||
.isEqualTo(new ScorePassIfNoErrors(new Score("Code-Review", 1), new Score("Code-Review", -2))); | ||
} | ||
|
||
@Test | ||
void shouldBuildDefaultScoreAlwaysPassIfStrategyIsUnknown() { | ||
Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( | ||
GeneralOption.SCORE_STRATEGY.getKey(), "mySimpleStrategy" | ||
)); | ||
|
||
assertThat(new ScoreStrategies().buildScoreStrategy(config)) | ||
.isEqualTo(new ScoreAlwaysPass(new Score("Code-Review", 1))); | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
src/test/java/pl/touk/sputnik/engine/visitor/score/ScoreAlwaysPassTest.java
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
src/test/java/pl/touk/sputnik/engine/visitor/score/ScorePassIfEmptyTest.java
This file was deleted.
Oops, something went wrong.