3 * Copyright (C) 2009-2017 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.computation.task.projectanalysis.issue;
22 import com.google.common.base.Strings;
23 import javax.annotation.CheckForNull;
24 import org.sonar.api.utils.log.Logger;
25 import org.sonar.api.utils.log.Loggers;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.user.UserDto;
29 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
30 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
31 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
33 import static org.sonar.api.CoreProperties.DEFAULT_ISSUE_ASSIGNEE;
36 * The user who is optionally declared as being the assignee
37 * of all the issues which SCM author is not associated with any SonarQube user.
39 public class DefaultAssignee {
41 private static final Logger LOG = Loggers.get(DefaultAssignee.class);
43 private final DbClient dbClient;
44 private final TreeRootHolder treeRootHolder;
45 private final SettingsRepository settingsRepository;
46 private final AnalysisMetadataHolder analysisMetadataHolder;
48 private boolean loaded = false;
49 private String login = null;
51 public DefaultAssignee(DbClient dbClient, TreeRootHolder treeRootHolder, SettingsRepository settingsRepository, AnalysisMetadataHolder analysisMetadataHolder) {
52 this.dbClient = dbClient;
53 this.treeRootHolder = treeRootHolder;
54 this.settingsRepository = settingsRepository;
55 this.analysisMetadataHolder = analysisMetadataHolder;
59 public String loadDefaultAssigneeLogin() {
63 String configuredLogin = settingsRepository.getSettings(treeRootHolder.getRoot()).getString(DEFAULT_ISSUE_ASSIGNEE);
64 if (!Strings.isNullOrEmpty(configuredLogin) && isValidLogin(configuredLogin)) {
65 this.login = configuredLogin;
71 private boolean isValidLogin(String login) {
72 try (DbSession dbSession = dbClient.openSession(false)) {
73 UserDto user = dbClient.userDao().selectActiveUserByLogin(dbSession, login);
75 LOG.info("Property {} is set with an unknown login: {}", DEFAULT_ISSUE_ASSIGNEE, login);
78 if (!isUserMemberOfOrganization(dbSession, user)) {
79 LOG.info("Property {} is set with a user which is not member of the organization of the project : {}", DEFAULT_ISSUE_ASSIGNEE, login);
86 private boolean isUserMemberOfOrganization(DbSession dbSession, UserDto user) {
87 return dbClient.organizationMemberDao().select(dbSession, analysisMetadataHolder.getOrganization().getUuid(), user.getId()).isPresent();