1919 */
2020package org .sonarsource .sonarlint .core .tracking ;
2121
22+ import jakarta .annotation .PostConstruct ;
2223import java .net .URI ;
2324import java .nio .file .Path ;
2425import java .time .Instant ;
@@ -117,6 +118,24 @@ public TrackingService(SonarLintRpcClient client, ConfigurationRepository config
117118 this .databaseService = databaseService ;
118119 }
119120
121+ @ PostConstruct
122+ public void migrateData () {
123+ if (dogfoodEnvironmentDetectionService .isDogfoodEnvironment ()) {
124+ if (knownFindingsStorageService .exists ()) {
125+ try {
126+ var repository = new KnownFindingsRepository (databaseService .getDatabase ());
127+ var xodusKnownFindingsStore = knownFindingsStorageService .get ();
128+ var findingsPerConfigScope = xodusKnownFindingsStore .loadAll ();
129+ repository .storeFindings (findingsPerConfigScope );
130+ } catch (Exception e ) {
131+ LOG .error ("Unable to migrate known findings, will use fresh DB" , e );
132+ }
133+ }
134+ // always call to remove lingering temporary files
135+ knownFindingsStorageService .delete ();
136+ }
137+ }
138+
120139 @ EventListener
121140 public void onAnalysisStarted (AnalysisStartedEvent event ) {
122141 var configurationScopeId = event .getConfigurationScopeId ();
@@ -168,7 +187,6 @@ private MatchingResult matchWithServerFindings(String configurationScopeId, Matc
168187 var effectiveBindingOpt = configurationRepository .getEffectiveBinding (configurationScopeId );
169188 var activeBranchOpt = branchTrackingService .awaitEffectiveSonarProjectBranch (configurationScopeId );
170189 var translationOpt = pathTranslationService .getOrComputePathTranslation (configurationScopeId );
171- var knownFindingsStore = knownFindingsStorageService .get ();
172190 var issuesToReport = matchingSession .getIssuesPerFile ();
173191 var hotspotsToReport = matchingSession .getSecurityHotspotsPerFile ();
174192 if (effectiveBindingOpt .isPresent () && activeBranchOpt .isPresent () && translationOpt .isPresent ()) {
@@ -191,34 +209,34 @@ private MatchingResult matchWithServerFindings(String configurationScopeId, Matc
191209 return Map .entry (ideRelativePath , matches );
192210 }).collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
193211 }
194- issuesToReport .forEach ((clientRelativePath , trackedIssues ) -> storeTrackedIssues (knownFindingsStore , configurationScopeId , clientRelativePath , trackedIssues ));
195- hotspotsToReport .forEach ((clientRelativePath , trackedHotspots ) -> storeTrackedSecurityHotspots (knownFindingsStore , configurationScopeId , clientRelativePath , trackedHotspots ));
212+ issuesToReport .forEach ((clientRelativePath , trackedIssues ) -> storeTrackedIssues (configurationScopeId , clientRelativePath , trackedIssues ));
213+ hotspotsToReport .forEach ((clientRelativePath , trackedHotspots ) -> storeTrackedSecurityHotspots (configurationScopeId , clientRelativePath , trackedHotspots ));
196214 eventPublisher .publishEvent (new MatchingSessionEndedEvent (matchingSession .countNewIssues (), matchingSession .countRemainingUnmatchedIssues ()));
197215 return new MatchingResult (issuesToReport , hotspotsToReport );
198216 }
199217
200- private void storeTrackedIssues (XodusKnownFindingsStore knownIssuesStore , String configurationScopeId , Path clientRelativePath , Collection <TrackedIssue > newKnownIssues ) {
218+ private void storeTrackedIssues (String configurationScopeId , Path clientRelativePath , Collection <TrackedIssue > newKnownIssues ) {
201219 if (dogfoodEnvironmentDetectionService .isDogfoodEnvironment ()) {
202220 var knownFindingsRepository = new KnownFindingsRepository (databaseService .getDatabase ());
203221 knownFindingsRepository .storeKnownIssues (configurationScopeId , clientRelativePath ,
204222 newKnownIssues .stream ().map (i -> new KnownFinding (i .getId (), i .getServerKey (), i .getTextRangeWithHash (), i .getLineWithHash (), i .getRuleKey (), i .getMessage (),
205223 i .getIntroductionDate ())).toList ());
206224 } else {
207- knownIssuesStore .storeKnownIssues (configurationScopeId , clientRelativePath ,
225+ knownFindingsStorageService . get () .storeKnownIssues (configurationScopeId , clientRelativePath ,
208226 newKnownIssues .stream ().map (i -> new KnownFinding (i .getId (), i .getServerKey (), i .getTextRangeWithHash (), i .getLineWithHash (), i .getRuleKey (), i .getMessage (),
209227 i .getIntroductionDate ())).toList ());
210228 }
211229 }
212230
213- private void storeTrackedSecurityHotspots (XodusKnownFindingsStore knownIssuesStore , String configurationScopeId , Path clientRelativePath ,
231+ private void storeTrackedSecurityHotspots (String configurationScopeId , Path clientRelativePath ,
214232 Collection <TrackedIssue > newKnownSecurityHotspots ) {
215233 if (dogfoodEnvironmentDetectionService .isDogfoodEnvironment ()) {
216234 var knownFindingsRepository = new KnownFindingsRepository (databaseService .getDatabase ());
217235 knownFindingsRepository .storeKnownSecurityHotspots (configurationScopeId , clientRelativePath ,
218236 newKnownSecurityHotspots .stream ().map (i -> new KnownFinding (i .getId (), i .getServerKey (), i .getTextRangeWithHash (), i .getLineWithHash (), i .getRuleKey (), i .getMessage (),
219237 i .getIntroductionDate ())).toList ());
220238 } else {
221- knownIssuesStore .storeKnownSecurityHotspots (configurationScopeId , clientRelativePath ,
239+ knownFindingsStorageService . get () .storeKnownSecurityHotspots (configurationScopeId , clientRelativePath ,
222240 newKnownSecurityHotspots .stream ().map (i -> new KnownFinding (i .getId (), i .getServerKey (), i .getTextRangeWithHash (), i .getLineWithHash (), i .getRuleKey (), i .getMessage (),
223241 i .getIntroductionDate ())).toList ());
224242 }
@@ -283,7 +301,7 @@ private static TrackedIssue updateRawHotspotWithServerData(TrackedIssue trackedH
283301 serverHotspot .getStatus ().isResolved (), trackedHotspot .getSeverity (), RuleType .SECURITY_HOTSPOT , trackedHotspot .getRuleKey (),
284302 trackedHotspot .getTextRangeWithHash (), trackedHotspot .getLineWithHash (),
285303 serverHotspot .getKey (), trackedHotspot .getImpacts (), trackedHotspot .getFlows (), trackedHotspot .getQuickFixes (),
286- serverHotspot .getVulnerabilityProbability (), HotspotStatus .valueOf (serverHotspot .getStatus ().name ()), null , trackedHotspot .getRuleDescriptionContextKey (),
304+ serverHotspot .getVulnerabilityProbability (), HotspotStatus .valueOf (serverHotspot .getStatus ().name ()), null , trackedHotspot .getRuleDescriptionContextKey (),
287305 trackedHotspot .getCleanCodeAttribute (), trackedHotspot .getFileUri ());
288306 }
289307
@@ -301,7 +319,6 @@ private static TrackedIssue updateTrackedIssueWithLocalOnlyIssueData(TrackedIssu
301319 }
302320
303321 private MatchingSession startMatchingSession (String configurationScopeId , Set <Path > fileRelativePaths , Set <URI > fileUris , UnaryOperator <String > fileContentProvider ) {
304- var knownFindingsStore = knownFindingsStorageService .get ();
305322 var dogfoodEnvironment = dogfoodEnvironmentDetectionService .isDogfoodEnvironment ();
306323 Map <Path , List <KnownFinding >> issuesByRelativePath ;
307324 Map <Path , List <KnownFinding >> hotspotsByRelativePath ;
@@ -312,6 +329,7 @@ private MatchingSession startMatchingSession(String configurationScopeId, Set<Pa
312329 hotspotsByRelativePath = fileRelativePaths .stream ()
313330 .collect (toMap (Function .identity (), relativePath -> knownFindingsRepository .loadSecurityHotspotsForFile (configurationScopeId , relativePath )));
314331 } else {
332+ var knownFindingsStore = knownFindingsStorageService .get ();
315333 issuesByRelativePath = fileRelativePaths .stream ()
316334 .collect (toMap (Function .identity (), relativePath -> knownFindingsStore .loadIssuesForFile (configurationScopeId , relativePath )));
317335 hotspotsByRelativePath = fileRelativePaths .stream ()
0 commit comments