|
| 1 | +/* |
| 2 | + * SonarLint Core - Implementation |
| 3 | + * Copyright (C) 2016-2025 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonarsource.sonarlint.core.sca; |
| 21 | + |
| 22 | +import java.util.UUID; |
| 23 | +import javax.annotation.CheckForNull; |
| 24 | +import org.sonarsource.sonarlint.core.SonarQubeClientManager; |
| 25 | +import org.sonarsource.sonarlint.core.branch.SonarProjectBranchTrackingService; |
| 26 | +import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger; |
| 27 | +import org.sonarsource.sonarlint.core.commons.progress.SonarLintCancelMonitor; |
| 28 | +import org.sonarsource.sonarlint.core.repository.config.ConfigurationRepository; |
| 29 | +import org.sonarsource.sonarlint.core.rpc.protocol.backend.sca.DependencyRiskTransition; |
| 30 | +import org.sonarsource.sonarlint.core.serverconnection.issues.ServerScaIssue; |
| 31 | +import org.sonarsource.sonarlint.core.storage.StorageService; |
| 32 | + |
| 33 | +public class ScaService { |
| 34 | + private static final SonarLintLogger LOG = SonarLintLogger.get(); |
| 35 | + |
| 36 | + private final ConfigurationRepository configurationRepository; |
| 37 | + private final StorageService storageService; |
| 38 | + private final SonarQubeClientManager sonarQubeClientManager; |
| 39 | + private final SonarProjectBranchTrackingService branchTrackingService; |
| 40 | + |
| 41 | + public ScaService(ConfigurationRepository configurationRepository, StorageService storageService, SonarQubeClientManager sonarQubeClientManager, |
| 42 | + SonarProjectBranchTrackingService branchTrackingService) { |
| 43 | + this.configurationRepository = configurationRepository; |
| 44 | + this.storageService = storageService; |
| 45 | + this.sonarQubeClientManager = sonarQubeClientManager; |
| 46 | + this.branchTrackingService = branchTrackingService; |
| 47 | + } |
| 48 | + |
| 49 | + public void changeStatus(String configurationScopeId, UUID issueReleaseKey, DependencyRiskTransition transition, @CheckForNull String comment, |
| 50 | + SonarLintCancelMonitor cancelMonitor) { |
| 51 | + var binding = configurationRepository.getEffectiveBindingOrThrow(configurationScopeId); |
| 52 | + var serverConnection = sonarQubeClientManager.getClientOrThrow(binding.connectionId()); |
| 53 | + var projectServerIssueStore = storageService.binding(binding).findings(); |
| 54 | + var branchName = branchTrackingService.awaitEffectiveSonarProjectBranch(configurationScopeId); |
| 55 | + |
| 56 | + if (branchName.isEmpty()) { |
| 57 | + throw new IllegalArgumentException("Could not determine matched branch for configuration scope " + configurationScopeId); |
| 58 | + } |
| 59 | + |
| 60 | + var scaIssues = projectServerIssueStore.loadScaIssues(branchName.get()); |
| 61 | + var dependencyRiskOpt = scaIssues.stream().filter(issue -> issue.key().equals(issueReleaseKey)).findFirst(); |
| 62 | + |
| 63 | + if (dependencyRiskOpt.isEmpty()) { |
| 64 | + throw new ScaIssueNotFoundException("Dependency Risk with key " + issueReleaseKey.toString() + " was not found", issueReleaseKey.toString()); |
| 65 | + } |
| 66 | + |
| 67 | + var dependencyRisk = dependencyRiskOpt.get(); |
| 68 | + |
| 69 | + if (!dependencyRisk.transitions().contains(adaptTransition(transition))) { |
| 70 | + throw new IllegalArgumentException("Transition " + transition + " is not allowed for this SCA issue"); |
| 71 | + } |
| 72 | + |
| 73 | + if ((transition == DependencyRiskTransition.ACCEPT || transition == DependencyRiskTransition.SAFE || transition == DependencyRiskTransition.FIXED) |
| 74 | + && (comment == null || comment.isBlank())) { |
| 75 | + throw new IllegalArgumentException("Comment is required for ACCEPT, FIXED, and SAFE transitions"); |
| 76 | + } |
| 77 | + |
| 78 | + LOG.info("Changing SCA issue status for issue {} to {} with comment: {}", issueReleaseKey, transition, comment); |
| 79 | + |
| 80 | + serverConnection.withClientApi(serverApi -> serverApi.sca().changeStatus(issueReleaseKey, transition.name(), comment, cancelMonitor)); |
| 81 | + } |
| 82 | + |
| 83 | + private static ServerScaIssue.Transition adaptTransition(DependencyRiskTransition transition) { |
| 84 | + return switch (transition) { |
| 85 | + case REOPEN -> ServerScaIssue.Transition.REOPEN; |
| 86 | + case CONFIRM -> ServerScaIssue.Transition.CONFIRM; |
| 87 | + case ACCEPT -> ServerScaIssue.Transition.ACCEPT; |
| 88 | + case SAFE -> ServerScaIssue.Transition.SAFE; |
| 89 | + case FIXED -> ServerScaIssue.Transition.FIXED; |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + public static class ScaIssueNotFoundException extends RuntimeException { |
| 94 | + private final String issueKey; |
| 95 | + |
| 96 | + public ScaIssueNotFoundException(String message, String issueKey) { |
| 97 | + super(message); |
| 98 | + this.issueKey = issueKey; |
| 99 | + } |
| 100 | + |
| 101 | + public String getIssueKey() { |
| 102 | + return issueKey; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments