Skip to content

Commit

Permalink
Fixes adoptions scoring when last release is more recent than last co…
Browse files Browse the repository at this point in the history
…mmit (#486)
  • Loading branch information
alecharp authored Mar 15, 2024
1 parent 37736f9 commit ef679f2
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023-2024 Jenkins Infra
* Copyright (c) 2022-2024 Jenkins Infra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,7 +21,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.jenkins.pluginhealth.scoring.scores;

import java.time.Duration;
Expand All @@ -48,10 +47,9 @@ public class AdoptionScoring extends Scoring {

private abstract static class TimeSinceLastCommitScoringComponent implements ScoringComponent {
protected final Duration getTimeBetweenLastCommitAndDate(String lastCommitDateMessage, ZonedDateTime then) {
final ZonedDateTime commitDate = ZonedDateTime
.parse(lastCommitDateMessage, DateTimeFormatter.ISO_DATE_TIME)
.withZoneSameInstant(getZone());
return Duration.between(then, commitDate).abs();
final ZonedDateTime commitDate = ZonedDateTime.parse(lastCommitDateMessage, DateTimeFormatter.ISO_DATE_TIME)
.withZoneSameInstant(getZone());
return Duration.between(then, commitDate);
}

protected ZoneId getZone() {
Expand All @@ -67,70 +65,101 @@ public int getWeight() {
@Override
public List<ScoringComponent> getComponents() {
return List.of(
new ScoringComponent() {
@Override
public String getDescription() {
return "The plugin must not be marked as up for adoption.";
}

@Override
public ScoringComponentResult getScore(Plugin $, Map<String, ProbeResult> probeResults) {
final ProbeResult probeResult = probeResults.get(UpForAdoptionProbe.KEY);
if (probeResult == null || ProbeResult.Status.ERROR.equals(probeResult.status())) {
return new ScoringComponentResult(-1000, 1000, List.of("Cannot determine if the plugin is up for adoption."));
new ScoringComponent() {
@Override
public String getDescription() {
return "The plugin must not be marked as up for adoption.";
}

return switch (probeResult.message()) {
case "This plugin is not up for adoption." ->
new ScoringComponentResult(100, getWeight(), List.of("The plugin is not marked as up for adoption."));
case "This plugin is up for adoption." ->
new ScoringComponentResult(
-1000,
getWeight(),
List.of("The plugin is marked as up for adoption."),
List.of(
new Resolution("See adoption guidelines", "https://www.jenkins.io/doc/developer/plugin-governance/adopt-a-plugin/#plugins-marked-for-adoption")
)
);
default -> new ScoringComponentResult(-100, getWeight(), List.of());
};
}

@Override
public int getWeight() {
return 1;
}
},
new TimeSinceLastCommitScoringComponent() {
@Override
public String getDescription() {
return "There must be a reasonable time gap between last release and last commit.";
}

@Override
public ScoringComponentResult getScore(Plugin plugin, Map<String, ProbeResult> probeResults) {
final ProbeResult probeResult = probeResults.get(LastCommitDateProbe.KEY);
if (probeResult == null || ProbeResult.Status.ERROR.equals(probeResult.status())) {
return new ScoringComponentResult(-100, 100, List.of("Cannot determine the last commit date."));
}
final long days = getTimeBetweenLastCommitAndDate(probeResult.message(), plugin.getReleaseTimestamp().withZoneSameInstant(getZone())).toDays();
final String defaultReason = "There are %d days between last release and last commit.".formatted(days);
if (days < Duration.of(30 * 6, ChronoUnit.DAYS).toDays()) {
return new ScoringComponentResult(100, getWeight(), List.of(defaultReason, "Less than 6 months gap between last release and last commit."));
@Override
public ScoringComponentResult getScore(Plugin $, Map<String, ProbeResult> probeResults) {
final ProbeResult probeResult = probeResults.get(UpForAdoptionProbe.KEY);
if (probeResult == null || ProbeResult.Status.ERROR.equals(probeResult.status())) {
return new ScoringComponentResult(
-1000, 1000, List.of("Cannot determine if the plugin is up for adoption."));
}

return switch (probeResult.message()) {
case "This plugin is not up for adoption." -> new ScoringComponentResult(
100, getWeight(), List.of("The plugin is not marked as up for adoption."));
case "This plugin is up for adoption." -> new ScoringComponentResult(
-1000,
getWeight(),
List.of("The plugin is marked as up for adoption."),
List.of(
new Resolution(
"See adoption guidelines",
"https://www.jenkins.io/doc/developer/plugin-governance/adopt-a-plugin/#plugins-marked-for-adoption")));
default -> new ScoringComponentResult(-100, getWeight(), List.of());
};
}
if (days < Duration.of((30 * 12) + 1, ChronoUnit.DAYS).toDays()) {
return new ScoringComponentResult(60, getWeight(), List.of(defaultReason, "Less than a year between last release and last commit."));

@Override
public int getWeight() {
return 1;
}
if (days < Duration.of((30 * 12 * 2) + 1, ChronoUnit.DAYS).toDays()) {
return new ScoringComponentResult(20, getWeight(), List.of(defaultReason, "Less than 2 years between last release and last commit."));
},
new TimeSinceLastCommitScoringComponent() {
@Override
public String getDescription() {
return "There must be a reasonable time gap between last release and last commit.";
}
if (days < Duration.of((30 * 12 * 4) + 1, ChronoUnit.DAYS).toDays()) {
return new ScoringComponentResult(10, 2, List.of(defaultReason, "Less than 4 years between last release and last commit."));

@Override
public ScoringComponentResult getScore(Plugin plugin, Map<String, ProbeResult> probeResults) {
final ProbeResult probeResult = probeResults.get(LastCommitDateProbe.KEY);
if (probeResult == null || ProbeResult.Status.ERROR.equals(probeResult.status())) {
return new ScoringComponentResult(
-100, 100, List.of("Cannot determine the last commit date."));
}
final long days = getTimeBetweenLastCommitAndDate(
probeResult.message(),
plugin.getReleaseTimestamp().withZoneSameInstant(getZone()))
.toDays();
if (days < 0) {
return new ScoringComponentResult(
100,
getWeight(),
List.of("The latest release is more recent than the latest commit on the plugin."));
}
final String defaultReason =
"There are %d days between last release and last commit.".formatted(days);
if (days < Duration.of(30 * 6, ChronoUnit.DAYS).toDays()) {
return new ScoringComponentResult(
100,
getWeight(),
List.of(
defaultReason,
"Less than 6 months gap between last release and last commit."));
}
if (days < Duration.of((30 * 12) + 1, ChronoUnit.DAYS).toDays()) {
return new ScoringComponentResult(
60,
getWeight(),
List.of(defaultReason, "Less than a year between last release and last commit."));
}
if (days
< Duration.of((30 * 12 * 2) + 1, ChronoUnit.DAYS)
.toDays()) {
return new ScoringComponentResult(
20,
getWeight(),
List.of(defaultReason, "Less than 2 years between last release and last commit."));
}
if (days
< Duration.of((30 * 12 * 4) + 1, ChronoUnit.DAYS)
.toDays()) {
return new ScoringComponentResult(
10,
2,
List.of(defaultReason, "Less than 4 years between last release and last commit."));
}
return new ScoringComponentResult(
-1000,
getWeight(),
List.of("There is more than 4 years between the last release and the last commit."));
}
return new ScoringComponentResult(-1000, getWeight(), List.of("There is more than 4 years between the last release and the last commit."));
}
}
);
});
}

@Override
Expand All @@ -150,6 +179,6 @@ public String description() {

@Override
public int version() {
return 4;
return 5;
}
}
Loading

0 comments on commit ef679f2

Please sign in to comment.