Skip to content

Commit

Permalink
Protects /api/scores against plugins with null version (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecharp authored Jan 18, 2023
1 parent 23cb9ac commit fc06d83
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public int hashCode(VersionNumber x) {
@Override
public VersionNumber nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
final String value = rs.getString(position);
return rs.wasNull() || Objects.isNull(value) ?
null : new VersionNumber(value);
return Objects.isNull(value) ? null : new VersionNumber(value);
}

@Override
Expand All @@ -74,15 +73,12 @@ public void nullSafeSet(PreparedStatement st, VersionNumber value, int index, Sh

@Override
public VersionNumber deepCopy(VersionNumber value) {
if (Objects.isNull(value)) {
return null;
}
return new VersionNumber(value.toString());
return Objects.isNull(value) ? null : new VersionNumber(value.toString());
}

@Override
public boolean isMutable() {
return true;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Plugin {
private String name;

@Type(VersionNumberType.class)
@Column(name = "version", updatable = false)
@Column(name = "version", nullable = false)
private VersionNumber version;

@Column(name = "scm")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import io.jenkins.pluginhealth.scoring.probes.ProbeContext;
import io.jenkins.pluginhealth.scoring.repository.PluginRepository;

import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProbeService {
Expand All @@ -54,7 +54,7 @@ public List<Probe> getProbes() {
return probes;
}

@Transactional
@Transactional(readOnly = true)
public Map<String, Long> getProbesFinalResults() {
return probes.stream()
.filter(probe ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import io.jenkins.pluginhealth.scoring.model.Plugin;
import io.jenkins.pluginhealth.scoring.model.Score;
import io.jenkins.pluginhealth.scoring.model.ScoreResult;
import io.jenkins.pluginhealth.scoring.repository.ScoreRepository;
Expand Down Expand Up @@ -63,10 +64,19 @@ public Map<String, ScoreSummary> getLatestScoresSummaryMap() {
return repository.findLatestScoreForAllPlugins().stream()
.collect(Collectors.toMap(
score -> score.getPlugin().getName(),
score -> new ScoreSummary(score.getValue(), score.getPlugin().getVersion().toString(), score.getDetails(), score.getComputedAt())
ScoreSummary::fromScore
));
}

public record ScoreSummary(long value, String version, Set<ScoreResult> details, ZonedDateTime timestamp) {
public static ScoreSummary fromScore(Score score) {
final Plugin plugin = score.getPlugin();
return new ScoreSummary(
score.getValue(),
plugin.getVersion() == null ? "" : plugin.getVersion().toString(),
score.getDetails(),
score.getComputedAt()
);
}
}
}

0 comments on commit fc06d83

Please sign in to comment.