@CheckForNull private static Metric.Level loadPreviousStatus(List<MetricDto> metrics, List<LiveMeasureDto> dbMeasures) { MetricDto alertStatusMetric = metrics.stream() .filter(m -> ALERT_STATUS_KEY.equals(m.getKey())) .findAny() .orElseThrow(() -> new IllegalStateException(String.format("Metric with key %s is not registered", ALERT_STATUS_KEY))); return dbMeasures.stream() .filter(m -> m.getMetricId() == alertStatusMetric.getId()) .map(LiveMeasureDto::getTextValue) .filter(Objects::nonNull) .map(m -> { try { return Metric.Level.valueOf(m); } catch (IllegalArgumentException e) { Loggers.get(LiveMeasureComputerImpl.class) .trace("Failed to parse value of metric '{}'", m, e); return null; } }) .filter(Objects::nonNull) .findAny() .orElse(null); }
@Override public void validate(String value, @Nullable List<String> options) { try { Metric.Level.valueOf(value); } catch (IllegalArgumentException e) { throw BadRequestException.create(format("Value '%s' must be one of \"OK\", \"ERROR\".", value)); } } }
private String generateQualityGate(MetricDto metric, LiveMeasureDto measure) { Level qualityGate = Level.valueOf(getNonNullValue(measure, LiveMeasureDto::getTextValue)); return generateBadge(metric, QUALITY_GATE_MESSAGE_BY_STATUS.get(qualityGate), COLOR_BY_QUALITY_GATE_STATUS.get(qualityGate)); }
private static org.sonar.server.webhook.ProjectAnalysis convert(ProjectAnalysis projectAnalysis) { CeTask ceTask = new CeTask(projectAnalysis.getCeTask().getId(), CeTask.Status.valueOf(projectAnalysis.getCeTask().getStatus().name())); Project project = new Project(projectAnalysis.getProject().getUuid(), projectAnalysis.getProject().getKey(), projectAnalysis.getProject().getName()); Analysis analysis = projectAnalysis.getAnalysis().map(a -> new Analysis(a.getAnalysisUuid(), a.getDate().getTime())).orElse(null); Branch branch = projectAnalysis.getBranch().map(b -> new Branch(b.isMain(), b.getName().orElse(null), Branch.Type.valueOf(b.getType().name()))).orElse(null); EvaluatedQualityGate qualityGate = Optional.ofNullable(projectAnalysis.getQualityGate()) .map(qg -> { EvaluatedQualityGate.Builder builder = EvaluatedQualityGate.newBuilder(); Set<Condition> conditions = qg.getConditions().stream() .map(q -> { Condition condition = new Condition(q.getMetricKey(), Condition.Operator.valueOf(q.getOperator().name()), q.getErrorThreshold(), q.getWarningThreshold(), q.isOnLeakPeriod()); builder.addCondition(condition, EvaluatedCondition.EvaluationStatus.valueOf(q.getStatus().name()), q.getStatus() == org.sonar.api.ce.posttask.QualityGate.EvaluationStatus.NO_VALUE ? null : q.getValue()); return condition; }) .collect(MoreCollectors.toSet()); return builder.setQualityGate(new org.sonar.server.qualitygate.QualityGate(qg.getId(), qg.getName(), conditions)) .setStatus(Metric.Level.valueOf(qg.getStatus().name())) .build(); }) .orElse(null); Long date = projectAnalysis.getAnalysis().map(a -> a.getDate().getTime()).orElse(null); Map<String, String> properties = projectAnalysis.getScannerContext().getProperties(); return new org.sonar.server.webhook.ProjectAnalysis(project, ceTask, analysis, branch, qualityGate, date, properties); } }
@Override public Object get(Value value, Class clazz, CoderContext context) { Measure<?> m = new Measure(); String metricKey = value.getString(); org.sonar.api.batch.measure.Metric metric = metricFinder.findByKey(metricKey); if (metric == null) { throw new IllegalStateException("Unknow metric with key " + metricKey); } m.setMetric((org.sonar.api.measures.Metric) metric); m.setRawValue(value.isNull(true) ? null : value.getDouble()); m.setData(value.getString()); m.setDescription(value.getString()); m.setAlertStatus(value.isNull(true) ? null : Metric.Level.valueOf(value.getString())); m.setAlertText(value.getString()); m.setDate(value.getDate()); m.setVariation1(value.isNull(true) ? null : value.getDouble()); m.setVariation2(value.isNull(true) ? null : value.getDouble()); m.setVariation3(value.isNull(true) ? null : value.getDouble()); m.setVariation4(value.isNull(true) ? null : value.getDouble()); m.setVariation5(value.isNull(true) ? null : value.getDouble()); m.setUrl(value.getString()); m.setPersonId(value.isNull(true) ? null : value.getInt()); m.setPersistenceMode(value.isNull(true) ? null : PersistenceMode.valueOf(value.getString())); return m; } }
@CheckForNull private static Metric.Level loadPreviousStatus(List<MetricDto> metrics, List<LiveMeasureDto> dbMeasures) { MetricDto alertStatusMetric = metrics.stream() .filter(m -> ALERT_STATUS_KEY.equals(m.getKey())) .findAny() .orElseThrow(() -> new IllegalStateException(String.format("Metric with key %s is not registered", ALERT_STATUS_KEY))); return dbMeasures.stream() .filter(m -> m.getMetricId() == alertStatusMetric.getId()) .map(LiveMeasureDto::getTextValue) .filter(Objects::nonNull) .map(m -> { try { return Metric.Level.valueOf(m); } catch (IllegalArgumentException e) { Loggers.get(LiveMeasureComputerImpl.class) .trace("Failed to parse value of metric '{}'", m, e); return null; } }) .filter(Objects::nonNull) .findAny() .orElse(null); }
@Override public Object get(Value value, Class clazz, CoderContext context) { Measure<?> m = new Measure(); String metricKey = value.getString(); org.sonar.api.batch.measure.Metric metric = metricFinder.findByKey(metricKey); if (metric == null) { throw new IllegalStateException("Unknow metric with key " + metricKey); } m.setMetric((org.sonar.api.measures.Metric) metric); m.setRawValue(value.isNull(true) ? null : value.getDouble()); m.setData(value.getString()); m.setDescription(value.getString()); m.setAlertStatus(value.isNull(true) ? null : Metric.Level.valueOf(value.getString())); m.setAlertText(value.getString()); m.setTendency(value.isNull(true) ? null : value.getInt()); m.setDate(value.getDate()); m.setVariation1(value.isNull(true) ? null : value.getDouble()); m.setVariation2(value.isNull(true) ? null : value.getDouble()); m.setVariation3(value.isNull(true) ? null : value.getDouble()); m.setVariation4(value.isNull(true) ? null : value.getDouble()); m.setVariation5(value.isNull(true) ? null : value.getDouble()); m.setUrl(value.getString()); m.setCharacteristic(value.isNull(true) ? null : techDebtModel.characteristicById(value.getInt())); m.setRequirement(value.isNull(true) ? null : techDebtModel.requirementsById(value.getInt())); m.setPersonId(value.isNull(true) ? null : value.getInt()); m.setPersistenceMode(value.isNull(true) ? null : PersistenceMode.valueOf(value.getString())); return m; } }
private String generateQualityGate(MetricDto metric, LiveMeasureDto measure) { Level qualityGate = Level.valueOf(getNonNullValue(measure, LiveMeasureDto::getTextValue)); return generateBadge(metric, QUALITY_GATE_MESSAGE_BY_STATUS.get(qualityGate), COLOR_BY_QUALITY_GATE_STATUS.get(qualityGate)); }
@Override public void validate(String value, @Nullable List<String> options) { try { Metric.Level.valueOf(value); } catch (IllegalArgumentException e) { throw BadRequestException.create(format("Value '%s' must be one of \"OK\", \"WARN\", \"ERROR\".", value)); } } }
private Level getQualityGate(DbSession dbSession, ComponentDto project) { return Level.valueOf(dbClient.liveMeasureDao().selectMeasure(dbSession, project.uuid(), ALERT_STATUS_KEY) .map(LiveMeasureDto::getTextValue) .orElseThrow(() -> new ProjectBadgesException("Quality gate has not been found"))); }
/** * @return the alert status if there is one, null otherwise */ public Metric.Level getAlertStatus() { if (alertStatus == null) { return null; } return Metric.Level.valueOf(alertStatus); }
/** * @return the measure alert level */ public Metric.Level getLevelValue() { if (textValue != null) { return Metric.Level.valueOf(textValue); } return null; }
/** * @return transforms and returns the data fields as a level of alert */ public Metric.Level getDataAsLevel() { if (data != null) { return Metric.Level.valueOf(data); } return null; }
webQualityGate = EvaluatedQualityGate.newBuilder() .setQualityGate(new org.sonar.server.qualitygate.QualityGate(qualityGate.getId(), qualityGate.getName(), Collections.singleton(qgCondition))) .setStatus(Metric.Level.valueOf(qualityGate.getStatus().name())) .addCondition(qgCondition, EvaluatedCondition.EvaluationStatus.valueOf(condition.getStatus().name()), condition.getValue()) .build();
private static org.sonar.server.webhook.ProjectAnalysis convert(ProjectAnalysis projectAnalysis) { CeTask ceTask = new CeTask(projectAnalysis.getCeTask().getId(), CeTask.Status.valueOf(projectAnalysis.getCeTask().getStatus().name())); Analysis analysis = projectAnalysis.getAnalysis().map(a -> new Analysis(a.getAnalysisUuid(), a.getDate().getTime())).orElse(null); Branch branch = projectAnalysis.getBranch().map(b -> new Branch(b.isMain(), b.getName().orElse(null), Branch.Type.valueOf(b.getType().name()))).orElse(null); EvaluatedQualityGate qualityGate = Optional.ofNullable(projectAnalysis.getQualityGate()) .map(qg -> { EvaluatedQualityGate.Builder builder = EvaluatedQualityGate.newBuilder(); Set<Condition> conditions = qg.getConditions().stream() .map(q -> { Condition condition = new Condition(q.getMetricKey(), Condition.Operator.valueOf(q.getOperator().name()), q.getErrorThreshold()); builder.addCondition(condition, EvaluatedCondition.EvaluationStatus.valueOf(q.getStatus().name()), q.getStatus() == org.sonar.api.ce.posttask.QualityGate.EvaluationStatus.NO_VALUE ? null : q.getValue()); return condition; }) .collect(MoreCollectors.toSet()); return builder.setQualityGate(new org.sonar.server.qualitygate.QualityGate(qg.getId(), qg.getName(), conditions)) .setStatus(Metric.Level.valueOf(qg.getStatus().name())) .build(); }) .orElse(null); Long date = projectAnalysis.getAnalysis().map(a -> a.getDate().getTime()).orElse(null); Map<String, String> properties = projectAnalysis.getScannerContext().getProperties(); Project project = new Project(projectAnalysis.getProject().getUuid(), projectAnalysis.getProject().getKey(), projectAnalysis.getProject().getName()); return new org.sonar.server.webhook.ProjectAnalysis(project, ceTask, analysis, branch, qualityGate, date, properties); } }
private Level getQualityGate(DbSession dbSession, ComponentDto project) { return Level.valueOf(dbClient.liveMeasureDao().selectMeasure(dbSession, project.uuid(), ALERT_STATUS_KEY) .map(LiveMeasureDto::getTextValue) .orElseThrow(() -> new ProjectBadgesException("Quality gate has not been found"))); }