diff options
Diffstat (limited to 'sonar-batch/src/main')
3 files changed, 38 insertions, 15 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java index 152dacce0f1..97886481de5 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java @@ -54,7 +54,7 @@ public class PastSnapshot { } public boolean isRelatedToSnapshot() { - return projectSnapshot!=null; + return projectSnapshot != null; } public Snapshot getProjectSnapshot() { @@ -62,7 +62,7 @@ public class PastSnapshot { } public Date getDate() { - return (projectSnapshot!=null ? projectSnapshot.getCreatedAt() : null); + return (projectSnapshot != null ? projectSnapshot.getCreatedAt() : null); } public String getMode() { @@ -86,12 +86,10 @@ public class PastSnapshot { return (projectSnapshot != null ? projectSnapshot.getQualifier() : null); } - public Date getTargetDate() { return targetDate; } - @Override public String toString() { if (StringUtils.equals(mode, PastSnapshotFinderByVersion.MODE)) { @@ -102,16 +100,20 @@ public class PastSnapshot { if (isRelatedToSnapshot()) { label += ", analysis of " + getDate(); } - label+=")"; + label += ")"; return label; } if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS)) { - return String.format("Compare to previous analysis (%s)", DateUtils.formatDate(getDate())); + String label = "Compare to previous analysis"; + if (isRelatedToSnapshot()) { + label += String.format(" (%s)", DateUtils.formatDate(getDate())); + } + return label; } if (StringUtils.equals(mode, PastSnapshotFinderByDate.MODE)) { String label = "Compare to date " + DateUtils.formatDate(getTargetDate()); if (isRelatedToSnapshot()) { - label += String.format("(analysis of %s)", DateUtils.formatDate(getDate())); + label += String.format(" (analysis of %s)", DateUtils.formatDate(getDate())); } return label; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java index d3eb23519e9..5f8ca55592c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java @@ -27,6 +27,7 @@ import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.DateUtils; import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; public class PastSnapshotFinderByPreviousAnalysis implements BatchExtension { @@ -37,8 +38,15 @@ public class PastSnapshotFinderByPreviousAnalysis implements BatchExtension { this.session = session; } + /** + * See SONAR-2428 : even if past snapshot does not exist, we should perform comparison. + * In this case as a value for target date we use moment just before current snapshot + * to be able to perform strict comparison of dates. + * Also note that ruby side (resource viewer) has precision in one minute. + */ PastSnapshot findByPreviousAnalysis(Snapshot projectSnapshot) { - String hql = "from " + Snapshot.class.getSimpleName() + " where createdAt<:date AND resourceId=:resourceId AND status=:status and last=:last and qualifier<>:lib order by createdAt desc"; + String hql = "from " + Snapshot.class.getSimpleName() + + " where createdAt<:date AND resourceId=:resourceId AND status=:status and last=:last and qualifier<>:lib order by createdAt desc"; List<Snapshot> snapshots = session.createQuery(hql) .setParameter("date", projectSnapshot.getCreatedAt()) .setParameter("resourceId", projectSnapshot.getResourceId()) @@ -48,12 +56,16 @@ public class PastSnapshotFinderByPreviousAnalysis implements BatchExtension { .setMaxResults(1) .getResultList(); + Snapshot snapshot; + Date targetDate; if (snapshots.isEmpty()) { - return null; + snapshot = null; + targetDate = new Date(projectSnapshot.getCreatedAt().getTime() - 1000 * 60); + } else { + snapshot = snapshots.get(0); + targetDate = snapshot.getCreatedAt(); } - Snapshot snapshot = snapshots.get(0); SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT); - return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, snapshot.getCreatedAt(), snapshot).setModeParameter(format.format(snapshot.getCreatedAt())); + return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, targetDate, snapshot).setModeParameter(format.format(targetDate)); } - } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java index 9bf15e1414c..ed353e975d7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java @@ -24,6 +24,7 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Qualifiers; +import java.util.Date; import java.util.List; public class PastSnapshotFinderByVersion implements BatchExtension { @@ -36,6 +37,9 @@ public class PastSnapshotFinderByVersion implements BatchExtension { this.session = session; } + /** + * See comments in {@link PastSnapshotFinderByPreviousAnalysis#findByPreviousAnalysis(Snapshot)} + */ PastSnapshot findByVersion(Snapshot projectSnapshot, String version) { String hql = "from " + Snapshot.class.getSimpleName() + " where version=:version AND resourceId=:resourceId AND status=:status AND qualifier<>:lib order by createdAt desc"; List<Snapshot> snapshots = session.createQuery(hql) @@ -46,11 +50,16 @@ public class PastSnapshotFinderByVersion implements BatchExtension { .setMaxResults(1) .getResultList(); + Snapshot snapshot; + Date targetDate; if (snapshots.isEmpty()) { - return null; + snapshot = null; + targetDate = new Date(projectSnapshot.getCreatedAt().getTime() - 1000 * 60); + } else { + snapshot = snapshots.get(0); + targetDate = snapshot.getCreatedAt(); } - Snapshot snapshot = snapshots.get(0); - return new PastSnapshot(MODE, snapshot.getCreatedAt(), snapshot).setModeParameter(snapshot.getVersion()); + return new PastSnapshot(MODE, targetDate, snapshot).setModeParameter(version); } } |