|
26 | 26 | import java.util.Collection; |
27 | 27 | import java.util.List; |
28 | 28 | import java.util.Map; |
29 | | -import java.util.Objects; |
30 | 29 | import java.util.Optional; |
31 | 30 | import java.util.Set; |
32 | 31 | import java.util.UUID; |
33 | 32 | import java.util.function.Consumer; |
34 | | -import com.fasterxml.jackson.core.type.TypeReference; |
35 | 33 | import com.fasterxml.jackson.databind.ObjectMapper; |
36 | | -import java.util.stream.Collectors; |
37 | | -import javax.annotation.Nullable; |
38 | 34 | import org.jooq.Configuration; |
39 | 35 | import org.jooq.JSON; |
40 | 36 | import org.jooq.TableField; |
41 | 37 | import org.sonarsource.sonarlint.core.commons.CleanCodeAttribute; |
42 | 38 | import org.sonarsource.sonarlint.core.commons.HotspotReviewStatus; |
43 | | -import org.sonarsource.sonarlint.core.commons.ImpactSeverity; |
44 | 39 | import org.sonarsource.sonarlint.core.commons.IssueSeverity; |
45 | 40 | import org.sonarsource.sonarlint.core.commons.IssueStatus; |
46 | 41 | import org.sonarsource.sonarlint.core.commons.RuleType; |
47 | | -import org.sonarsource.sonarlint.core.commons.SoftwareQuality; |
48 | 42 | import org.sonarsource.sonarlint.core.commons.VulnerabilityProbability; |
49 | 43 | import org.sonarsource.sonarlint.core.commons.api.SonarLanguage; |
50 | 44 | import org.sonarsource.sonarlint.core.commons.api.TextRange; |
51 | 45 | import org.sonarsource.sonarlint.core.commons.api.TextRangeWithHash; |
52 | | -import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger; |
53 | 46 | import org.sonarsource.sonarlint.core.commons.storage.SonarLintDatabase; |
54 | 47 | import org.sonarsource.sonarlint.core.commons.storage.model.Tables; |
55 | 48 | import org.sonarsource.sonarlint.core.commons.storage.model.tables.records.ServerBranchesRecord; |
|
66 | 59 | import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.SERVER_FINDINGS; |
67 | 60 | import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.SERVER_BRANCHES; |
68 | 61 | import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.SERVER_DEPENDENCY_RISKS; |
| 62 | +import static org.sonarsource.sonarlint.core.serverconnection.storage.JsonMapper.deserializeImpacts; |
| 63 | +import static org.sonarsource.sonarlint.core.serverconnection.storage.JsonMapper.deserializeLanguages; |
| 64 | +import static org.sonarsource.sonarlint.core.serverconnection.storage.JsonMapper.deserializeTransitions; |
| 65 | +import static org.sonarsource.sonarlint.core.serverconnection.storage.JsonMapper.serializeLanguages; |
| 66 | +import static org.sonarsource.sonarlint.core.serverconnection.storage.JsonMapper.serializeTransitions; |
69 | 67 |
|
70 | 68 | public class ServerFindingRepository implements ProjectServerIssueStore { |
71 | | - private static final SonarLintLogger LOG = SonarLintLogger.get(); |
72 | 69 |
|
73 | 70 | private final SonarLintDatabase database; |
74 | 71 | private final String connectionId; |
@@ -352,14 +349,7 @@ private void upsertBranchMetadata(String branchName, |
352 | 349 | Instant syncTimestamp, Set<SonarLanguage> enabledLanguages) { |
353 | 350 |
|
354 | 351 | var ldt = LocalDateTime.ofInstant(syncTimestamp, ZoneId.systemDefault()); |
355 | | - JSON langsJson = null; |
356 | | - try { |
357 | | - var objectMapper = new ObjectMapper(); |
358 | | - var languageNames = enabledLanguages.stream().map(Enum::name).toList(); |
359 | | - langsJson = JSON.valueOf(objectMapper.writeValueAsString(languageNames)); |
360 | | - } catch (Exception e) { |
361 | | - LOG.error("Failed to serialize enabled languages {}", enabledLanguages, e); |
362 | | - } |
| 352 | + var langsJson = serializeLanguages(enabledLanguages); |
363 | 353 |
|
364 | 354 | int updated = database.dsl().update(SERVER_BRANCHES) |
365 | 355 | .set(tsField, ldt) |
@@ -388,81 +378,7 @@ private Set<SonarLanguage> readLanguages(String branchName, |
388 | 378 | if (rec == null) { |
389 | 379 | return Set.of(); |
390 | 380 | } |
391 | | - var json = rec.value1(); |
392 | | - if (json == null) { |
393 | | - return Set.of(); |
394 | | - } |
395 | | - try { |
396 | | - var objectMapper = new ObjectMapper(); |
397 | | - var languages = objectMapper.readValue(json.data(), new TypeReference<List<String>>() { |
398 | | - }); |
399 | | - return languages.stream() |
400 | | - .map(language -> { |
401 | | - try { |
402 | | - return SonarLanguage.valueOf(language); |
403 | | - } catch (Exception e) { |
404 | | - return null; |
405 | | - } |
406 | | - }) |
407 | | - .filter(Objects::nonNull) |
408 | | - .collect(Collectors.toUnmodifiableSet()); |
409 | | - } catch (Exception e) { |
410 | | - return Set.of(); |
411 | | - } |
412 | | - } |
413 | | - |
414 | | - private static JSON serializeTransitions(@Nullable List<ServerDependencyRisk.Transition> transitions) { |
415 | | - if (transitions == null) { |
416 | | - return null; |
417 | | - } |
418 | | - try { |
419 | | - var objectMapper = new ObjectMapper(); |
420 | | - var stringList = transitions.stream().map(Enum::name).toList(); |
421 | | - return JSON.valueOf(objectMapper.writeValueAsString(stringList)); |
422 | | - } catch (Exception e) { |
423 | | - LOG.error("Failed to serialize transitions {}", transitions, e); |
424 | | - return JSON.valueOf("{}"); |
425 | | - } |
426 | | - } |
427 | | - |
428 | | - private static Map<SoftwareQuality, ImpactSeverity> deserializeImpacts(@Nullable JSON impactsJson) { |
429 | | - if (impactsJson == null) { |
430 | | - return Map.of(); |
431 | | - } |
432 | | - try { |
433 | | - var objectMapper = new ObjectMapper(); |
434 | | - var map = objectMapper.readValue(impactsJson.data(), new TypeReference<Map<String, String>>() { |
435 | | - }); |
436 | | - return map.entrySet().stream() |
437 | | - .collect(Collectors.toMap(entry -> SoftwareQuality.valueOf(entry.getKey()), entry -> ImpactSeverity.valueOf(entry.getValue()))); |
438 | | - } catch (Exception e) { |
439 | | - LOG.error("Failed to deserialize impacts {}", impactsJson.data(), e); |
440 | | - return Map.of(); |
441 | | - } |
442 | | - } |
443 | | - |
444 | | - private static List<ServerDependencyRisk.Transition> deserializeTransitions(@Nullable JSON json) { |
445 | | - if (json == null) { |
446 | | - return List.of(); |
447 | | - } |
448 | | - try { |
449 | | - var objectMapper = new ObjectMapper(); |
450 | | - var transitions = objectMapper.readValue(json.data(), new TypeReference<List<String>>() { |
451 | | - }); |
452 | | - return transitions.stream() |
453 | | - .map(transition -> { |
454 | | - try { |
455 | | - return ServerDependencyRisk.Transition.valueOf(transition); |
456 | | - } catch (Exception e) { |
457 | | - return null; |
458 | | - } |
459 | | - }) |
460 | | - .filter(Objects::nonNull) |
461 | | - .toList(); |
462 | | - } catch (Exception e) { |
463 | | - LOG.error("Failed to deserialize transitions {}", json.data(), e); |
464 | | - return List.of(); |
465 | | - } |
| 381 | + return deserializeLanguages(rec.value1()); |
466 | 382 | } |
467 | 383 |
|
468 | 384 | private static ServerIssue<?> adaptIssue(ServerFindingsRecord rec) { |
|
0 commit comments