3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.ce.ws;
22 import java.util.Optional;
23 import org.sonar.api.server.ws.Request;
24 import org.sonar.api.server.ws.Response;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.web.UserRole;
27 import org.sonar.core.util.Uuids;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.ce.CeTaskMessageDto;
31 import org.sonar.db.project.ProjectDto;
32 import org.sonar.db.user.UserDismissedMessageDto;
33 import org.sonar.db.user.UserDto;
34 import org.sonar.server.component.ComponentFinder;
35 import org.sonar.server.exceptions.NotFoundException;
36 import org.sonar.server.user.UserSession;
38 import static java.lang.String.format;
39 import static java.util.Objects.requireNonNull;
40 import static org.sonar.server.exceptions.NotFoundException.checkFound;
42 public class DismissAnalysisWarningAction implements CeWsAction {
44 private static final String MESSAGE_NOT_FOUND = "Message '%s' not found.";
45 private static final String MESSAGE_CANNOT_BE_DISMISSED = "Message '%s' cannot be dismissed.";
46 private static final String PARAM_COMPONENT_KEY = "component";
47 private static final String PARAM_MESSAGE_KEY = "warning";
49 private final UserSession userSession;
50 private final DbClient dbClient;
51 private final ComponentFinder componentFinder;
53 public DismissAnalysisWarningAction(UserSession userSession, DbClient dbClient, ComponentFinder componentFinder) {
54 this.userSession = userSession;
55 this.dbClient = dbClient;
56 this.componentFinder = componentFinder;
60 public void define(WebService.NewController controller) {
61 WebService.NewAction action = controller
62 .createAction("dismiss_analysis_warning")
64 .setDescription("Permanently dismiss a specific analysis warning. Requires authentication and 'Browse' permission on the specified project.")
69 action.createParam(PARAM_COMPONENT_KEY)
70 .setDescription("Key of the project")
72 .setExampleValue(Uuids.UUID_EXAMPLE_01);
73 action.createParam(PARAM_MESSAGE_KEY)
74 .setDescription("Key of the warning")
76 .setExampleValue(Uuids.UUID_EXAMPLE_02);
80 public void handle(Request request, Response response) throws Exception {
81 userSession.checkLoggedIn();
82 String userLogin = requireNonNull(userSession.getLogin());
83 String projectKey = request.mandatoryParam(PARAM_COMPONENT_KEY);
84 String messageKey = request.mandatoryParam(PARAM_MESSAGE_KEY);
86 try (DbSession dbSession = dbClient.openSession(false)) {
87 UserDto user = getUser(dbSession, userLogin);
88 ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
89 userSession.checkEntityPermission(UserRole.USER, project);
91 CeTaskMessageDto messageDto = dbClient.ceTaskMessageDao()
92 .selectByUuid(dbSession, messageKey)
93 .orElseThrow(() -> new NotFoundException(format(MESSAGE_NOT_FOUND, messageKey)));
94 if (!messageDto.getType().isDismissible()) {
95 throw new IllegalArgumentException(format(MESSAGE_CANNOT_BE_DISMISSED, messageKey));
98 Optional<UserDismissedMessageDto> result = dbClient.userDismissedMessagesDao().selectByUserAndProjectAndMessageType(dbSession, user, project, messageDto.getType());
99 if (!result.isPresent()) {
100 dbClient.userDismissedMessagesDao().insert(dbSession, new UserDismissedMessageDto()
101 .setUuid(Uuids.create())
102 .setUserUuid(user.getUuid())
103 .setProjectUuid(project.getUuid())
104 .setCeMessageType(messageDto.getType()));
108 response.noContent();
112 private UserDto getUser(DbSession dbSession, String userLogin) {
113 return checkFound(dbClient.userDao().selectByLogin(dbSession, userLogin), "User '%s' not found", userLogin);