// compare with past measure
Integer metricId = (measure.getMetric().getId() != null ? measure.getMetric().getId() : metricFinder.findByKey(measure.getMetric().getKey()).getId());
Integer characteristicId = (measure.getCharacteristic() != null ? measure.getCharacteristic().getId() : null);
+ String committer = measure.getCommitter();
Integer ruleId = (measure instanceof RuleMeasure ? ((RuleMeasure)measure).getRule().getId() : null);
- Object[] pastMeasure = pastMeasuresByKey.get(new MeasureKey(metricId, characteristicId, ruleId));
+ Object[] pastMeasure = pastMeasuresByKey.get(new MeasureKey(metricId, characteristicId, committer, ruleId));
if (updateVariation(measure, pastMeasure, index)) {
context.saveMeasure(measure);
}
static final class MeasureKey {
int metricId;
Integer characteristicId;
+ String committer;
Integer ruleId;
MeasureKey(Object[] pastFields) {
metricId = PastMeasuresLoader.getMetricId(pastFields);
characteristicId = PastMeasuresLoader.getCharacteristicId(pastFields);
+ committer = PastMeasuresLoader.getCommitter(pastFields);
ruleId = PastMeasuresLoader.getRuleId(pastFields);
}
- MeasureKey(int metricId, Integer characteristicId, Integer ruleId) {
+ MeasureKey(int metricId, Integer characteristicId, String committer, Integer ruleId) {
this.metricId = metricId;
this.characteristicId = characteristicId;
+ this.committer = committer;
this.ruleId = ruleId;
}
if (characteristicId != null ? !characteristicId.equals(that.characteristicId) : that.characteristicId != null) {
return false;
}
+ if (committer != null ? !committer.equals(that.committer) : that.committer != null) {
+ return false;
+ }
if (ruleId != null ? !ruleId.equals(that.ruleId) : that.ruleId != null) {
return false;
}
public int hashCode() {
int result = metricId;
result = 31 * result + (characteristicId != null ? characteristicId.hashCode() : 0);
+ result = 31 * result + (committer != null ? committer.hashCode() : 0);
result = 31 * result + (ruleId != null ? ruleId.hashCode() : 0);
return result;
}
}
-}
\ No newline at end of file
+}
import org.mockito.Matchers;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.DecoratorContext;
-import org.sonar.api.database.model.MeasureModel;
import org.sonar.api.measures.*;
import org.sonar.api.resources.*;
import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RulePriority;
import org.sonar.batch.components.PastMeasuresLoader;
import org.sonar.batch.components.PastSnapshot;
import org.sonar.batch.components.TimeMachineConfiguration;
// first past analysis
when(pastMeasuresLoader.getPastMeasures(javaPackage, pastSnapshot1)).thenReturn(Arrays.asList(
- new Object[]{NCLOC_ID, null, null, 180.0},
- new Object[]{COVERAGE_ID, null, null, 75.0}));
+ new Object[] {NCLOC_ID, null, null, null, 180.0},
+ new Object[] {COVERAGE_ID, null, null, null, 75.0}));
// second past analysis
when(pastMeasuresLoader.getPastMeasures(javaPackage, pastSnapshot3)).thenReturn(Arrays.<Object[]>asList(
- new Object[]{NCLOC_ID, null, null, 240.0}));
+ new Object[] {NCLOC_ID, null, null, null, 240.0}));
// current analysis
DecoratorContext context = mock(DecoratorContext.class);
// first past analysis
when(pastMeasuresLoader.getPastMeasures(javaPackage, pastSnapshot1)).thenReturn(Arrays.asList(
- new Object[]{VIOLATIONS_ID, null, null, 180.0},//total
- new Object[]{VIOLATIONS_ID, null, rule1.getId(), 100.0},// rule 1
- new Object[]{VIOLATIONS_ID, null, rule2.getId(), 80.0})); // rule 2
+ new Object[] {VIOLATIONS_ID, null, null, null, 180.0},// total
+ new Object[] {VIOLATIONS_ID, null, null, rule1.getId(), 100.0},// rule 1
+ new Object[] {VIOLATIONS_ID, null, null, rule2.getId(), 80.0})); // rule 2
// current analysis
DecoratorContext context = mock(DecoratorContext.class);
import org.sonar.batch.index.DefaultIndex;
import javax.persistence.Query;
+
import java.util.*;
public class DefaultTimeMachine implements TimeMachine {
.append(MeasureModel.class.getSimpleName())
.append(" m, ")
.append(Snapshot.class.getSimpleName())
- .append(" s WHERE m.snapshotId=s.id AND s.resourceId=:resourceId AND s.status=:status AND m.characteristic IS NULL AND s.qualifier<>:lib");
+ .append(" s WHERE m.snapshotId=s.id AND s.resourceId=:resourceId AND s.status=:status AND s.qualifier<>:lib");
params.put("resourceId", resource.getId());
params.put("status", Snapshot.STATUS_PROCESSED);
params.put("lib", Qualifiers.LIBRARY);
- sb.append(" AND m.ruleId IS NULL AND m.rulePriority IS NULL ");
+ sb.append(" AND m.characteristic IS NULL");
+ sb.append(" AND m.committer IS NULL");
+ sb.append(" AND m.ruleId IS NULL AND m.rulePriority IS NULL");
if (!metricIds.isEmpty()) {
sb.append(" AND m.metricId IN (:metricIds) ");
params.put("metricIds", metricIds);
measure.setVariation5(model.getVariationValue5());
measure.setUrl(model.getUrl());
measure.setCharacteristic(model.getCharacteristic());
+ measure.setCommitter(model.getCommitter());
return measure;
}
-}
\ No newline at end of file
+}
}
public List<Object[]> getPastMeasures(String resourceKey, Snapshot projectPastSnapshot) {
- String sql = "select m.metric_id, m.characteristic_id, m.rule_id, m.value from project_measures m, snapshots s" +
+ String sql = "select m.metric_id, m.characteristic_id, m.committer, m.rule_id, m.value from project_measures m, snapshots s" +
" where m.snapshot_id=s.id and m.metric_id in (:metricIds) " +
" and (s.root_snapshot_id=:rootSnapshotId or s.id=:rootSnapshotId) " +
" and s.status=:status and s.project_id=(select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib)";
return number != null ? number.intValue() : null;
}
+ public static String getCommitter(Object[] row) {
+ return (String) row[2];
+ }
+
public static Integer getRuleId(Object[] row) {
// can be BigDecimal on Oracle
- Number number = (Number) row[2];
+ Number number = (Number) row[3];
return number != null ? number.intValue() : null;
}
public static boolean hasValue(Object[] row) {
- return row[3] != null;
+ return row[4] != null;
}
public static double getValue(Object[] row) {
- return ((Number) row[3]).doubleValue();
+ return ((Number) row[4]).doubleValue();
}
+
}
merge.setVariationValue5(measure.getVariation5());
merge.setUrl(measure.getUrl());
merge.setCharacteristic(measure.getCharacteristic());
+ merge.setCommitter(measure.getCommitter());
if (measure.getValue() != null) {
merge.setValue(measure.getValue().doubleValue());
} else {
import java.util.Arrays;
import java.util.List;
-import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.nullValue;
Object[] pastMeasure = measures.get(0);
assertThat(PastMeasuresLoader.getMetricId(pastMeasure), is(1));
assertThat(PastMeasuresLoader.getCharacteristicId(pastMeasure), nullValue());
+ assertThat(PastMeasuresLoader.getCommitter(pastMeasure), nullValue());
assertThat(PastMeasuresLoader.getValue(pastMeasure), is(5.0));
pastMeasure = measures.get(1);
assertThat(PastMeasuresLoader.getMetricId(pastMeasure), is(2));
assertThat(PastMeasuresLoader.getCharacteristicId(pastMeasure), nullValue());
+ assertThat(PastMeasuresLoader.getCommitter(pastMeasure), nullValue());
assertThat(PastMeasuresLoader.getValue(pastMeasure), is(60.0));
}
Object[] pastMeasure = measures.get(0);
assertThat(PastMeasuresLoader.getMetricId(pastMeasure), is(1));
assertThat(PastMeasuresLoader.getCharacteristicId(pastMeasure), nullValue());
+ assertThat(PastMeasuresLoader.getCommitter(pastMeasure), nullValue());
assertThat(PastMeasuresLoader.getValue(pastMeasure), is(60.0));
pastMeasure = measures.get(1);
assertThat(PastMeasuresLoader.getMetricId(pastMeasure), is(2));
assertThat(PastMeasuresLoader.getCharacteristicId(pastMeasure), nullValue());
+ assertThat(PastMeasuresLoader.getCommitter(pastMeasure), nullValue());
assertThat(PastMeasuresLoader.getValue(pastMeasure), is(80.0));
}
List<Metric> metrics = Arrays.asList(ncloc, complexity, data);
PastMeasuresLoader loader = new PastMeasuresLoader(getSession(), metrics);
-
+
assertThat(loader.getMetrics().size(), is(2));
assertThat(loader.getMetrics(), hasItems(ncloc, complexity));
}
import org.sonar.api.rules.RulePriority;
import javax.persistence.*;
+
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@JoinColumn(name = "characteristic_id")
private Characteristic characteristic;
+ @Column(name = "committer", updatable = true, nullable = true, length = 100)
+ private String committer;
+
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
-
+
/**
* Creates a measure based on a metric and a double value
*/
return this;
}
+ public String getCommitter() {
+ return committer;
+ }
+
+ public MeasureModel setCommitter(String committer) {
+ this.committer = committer;
+ return this;
+ }
+
@Override
public Object clone() {
MeasureModel clone = new MeasureModel();
clone.setMeasureDate(getMeasureDate());
clone.setUrl(getUrl());
clone.setCharacteristic(getCharacteristic());
+ clone.setCommitter(getCommitter());
return clone;
}
+
}
*/
package org.sonar.api.measures;
+import com.google.common.annotations.Beta;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.sonar.api.qualitymodel.Characteristic;
protected Double variation1, variation2, variation3, variation4, variation5;
protected String url;
protected Characteristic characteristic;
+ protected String committer;
protected PersistenceMode persistenceMode = PersistenceMode.FULL;
public Measure(String metricKey) {
this.data=null;
return this;
}
-
+
/**
* @return the description of the measure
*/
return this;
}
+ /**
+ * @since 2.14
+ */
+ @Beta
+ public String getCommitter() {
+ return committer;
+ }
+
+ /**
+ * @since 2.14
+ */
+ @Beta
+ public Measure setCommitter(String committer) {
+ this.committer = committer;
+ return this;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
if (characteristic != null ? !characteristic.equals(measure.characteristic) : measure.characteristic != null) {
return false;
}
+ if (committer != null ? !committer.equals(measure.committer) : measure.committer != null) {
+ return false;
+ }
return true;
}
public int hashCode() {
int result = metricKey != null ? metricKey.hashCode() : 0;
result = 31 * result + (characteristic != null ? characteristic.hashCode() : 0);
+ result = 31 * result + (committer != null ? committer.hashCode() : 0);
return result;
}
for (Measure measure : measures) {
if (measure.getClass().equals(Measure.class) &&
measure.getMetricKey().equals(metricKey) &&
- measure.getCharacteristic() == null) {
+ measure.getCharacteristic() == null &&
+ measure.getCommitter() == null) {
return measure;
}
}
sql.append(" ) AND ");
}
- sql.append(" pm.rule_id is null AND pm.rule_priority is null AND pm.characteristic_id IS NULL AND ");
+ sql.append(" pm.rule_id IS NULL AND pm.rule_priority IS NULL");
+ sql.append(" AND pm.characteristic_id IS NULL");
+ sql.append(" AND pm.committer IS NULL");
+ sql.append(" AND ");
}
sql.append(" s.status=:status AND s.islast=:islast ");
if (filter.getScopes() != null) {