private QualityGate fetch(String qualityGateSetting, ServerClient client, MetricFinder metricFinder) { String jsonText = null; try { long qGateId = Long.parseLong(qualityGateSetting); jsonText = client.request(SHOW_URL + "?id=" + qGateId, false); } catch (NumberFormatException configIsNameInsteadOfId) { jsonText = client.request(SHOW_URL + "?name=" + qualityGateSetting, false); } JsonParser parser = new JsonParser(); JsonObject root = parser.parse(jsonText).getAsJsonObject(); QualityGate configuredGate = new QualityGate(root.get("name").getAsString()); if (root.has(ATTRIBUTE_CONDITIONS)) { for (JsonElement condition : root.get(ATTRIBUTE_CONDITIONS).getAsJsonArray()) { JsonObject conditionObject = condition.getAsJsonObject(); configuredGate.add(new ResolvedCondition(conditionObject, metricFinder.findByKey(conditionObject.get("metric").getAsString()))); } } return configuredGate; } }
@Override public DecoratorContext saveMeasure(Measure measure) { checkReadOnly(SAVE_MEASURE_METHOD); Metric metric = metricFinder.findByKey(measure.getMetricKey()); if (metric == null) { throw new SonarException("Unknown metric: " + measure.getMetricKey()); } measure.setMetric(metric); if (coverageFilter.accept(resource, measure)) { List<Measure> metricMeasures = measuresByMetric.get(measure.getMetricKey()); boolean add = true; if (metricMeasures != null) { int index = metricMeasures.indexOf(measure); if (index > -1) { if (metricMeasures.get(index) == measure) { add = false; } else { throw new SonarException("Can not add twice the same measure on " + resource + ": " + measure); } } } if (add) { measuresByMetric.put(measure.getMetricKey(), measure); } } return this; }
@Override public void persist() { try (DbSession session = mybatis.openSession(true)) { MeasureMapper mapper = session.getMapper(MeasureMapper.class); for (Entry<Measure> entry : measureCache.entries()) { String effectiveKey = entry.key()[0].toString(); Measure measure = entry.value(); BatchResource batchResource = resourceCache.get(effectiveKey); // Reload Metric to have all Hibernate fields populated measure.setMetric(metricFinder.findByKey(measure.getMetricKey())); if (shouldPersistMeasure(batchResource.resource(), measure)) { MeasureModel measureModel = model(measure, ruleFinder).setSnapshotId(batchResource.snapshotId()); mapper.insert(measureModel); } } session.commit(); } catch (Exception e) { throw new IllegalStateException("Unable to save some measures", e); } }
Metric metric = metricFinder.findByKey(measure.getMetric().getKey()); if (metric == null) { throw new IllegalStateException("Unknow metric with key: " + measure.getMetric().getKey());
@Override public void persist() { // Don't use batch insert for duplications since keeping all data in memory can produce OOM try (DbSession session = mybatis.openSession(false)) { MeasureMapper mapper = session.getMapper(MeasureMapper.class); Metric duplicationMetricWithId = metricFinder.findByKey(CoreMetrics.DUPLICATIONS_DATA_KEY); for (String effectiveKey : duplicationCache.componentKeys()) { Iterable<DefaultDuplication> dups = duplicationCache.byComponent(effectiveKey); Measure measure = new Measure(duplicationMetricWithId, DuplicationUtils.toXml(dups)).setPersistenceMode(PersistenceMode.DATABASE); BatchResource batchResource = resourceCache.get(effectiveKey); if (MeasurePersister.shouldPersistMeasure(batchResource.resource(), measure)) { MeasureModel measureModel = MeasurePersister.model(measure, ruleFinder).setSnapshotId(batchResource.snapshotId()); mapper.insert(measureModel); session.commit(); } } } catch (Exception e) { throw new IllegalStateException("Unable to save some measures", e); } }