All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.server.issue.AssignAction Maven / Gradle / Ivy

There is a newer version: 7.2.1
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.issue;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.issue.condition.IsUnResolved;
import org.sonar.api.server.ServerSide;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.user.UserDto;
import org.sonar.server.user.UserSession;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static org.sonar.server.ws.WsUtils.checkFound;

@ServerSide
public class AssignAction extends Action {

  public static final String ASSIGN_KEY = "assign";
  public static final String ASSIGNEE_PARAMETER = "assignee";
  private static final String VERIFIED_ASSIGNEE = "verifiedAssignee";
  private static final String ASSIGNEE_ORGANIZATIONS = "assigneeOrganizationUuids";

  private final DbClient dbClient;
  private final IssueFieldsSetter issueFieldsSetter;

  public AssignAction(DbClient dbClient, IssueFieldsSetter issueFieldsSetter) {
    super(ASSIGN_KEY);
    this.dbClient = dbClient;
    this.issueFieldsSetter = issueFieldsSetter;
    super.setConditions(new IsUnResolved());
  }

  @Override
  public boolean verify(Map properties, Collection issues, UserSession userSession) {
    String assigneeLogin = getAssigneeValue(properties);
    UserDto assignee = isNullOrEmpty(assigneeLogin) ? null : getUser(assigneeLogin);
    properties.put(VERIFIED_ASSIGNEE, assignee);
    properties.put(ASSIGNEE_ORGANIZATIONS, loadUserOrganizations(assignee));
    return true;
  }

  private static String getAssigneeValue(Map properties) {
    return (String) properties.get(ASSIGNEE_PARAMETER);
  }

  private UserDto getUser(String assigneeKey) {
    try (DbSession dbSession = dbClient.openSession(false)) {
      return checkFound(dbClient.userDao().selectActiveUserByLogin(dbSession, assigneeKey), "Unknown user: %s", assigneeKey);
    }
  }

  private Set loadUserOrganizations(@Nullable UserDto assignee) {
    if (assignee == null) {
      return Collections.emptySet();
    }
    try (DbSession dbSession = dbClient.openSession(false)) {
      return dbClient.organizationMemberDao().selectOrganizationUuidsByUser(dbSession, assignee.getId());
    }
  }

  @Override
  public boolean execute(Map properties, Context context) {
    checkArgument(properties.containsKey(VERIFIED_ASSIGNEE), "Assignee is missing from the execution parameters");
    UserDto assignee = (UserDto) properties.get(VERIFIED_ASSIGNEE);
    return isAssigneeMemberOfIssueOrganization(assignee, properties, context) && issueFieldsSetter.assign(context.issue(), assignee, context.issueChangeContext());
  }

  private static boolean isAssigneeMemberOfIssueOrganization(@Nullable UserDto assignee, Map properties, Context context) {
    return assignee == null || ((Set) properties.get(ASSIGNEE_ORGANIZATIONS)).contains(context.project().getOrganizationUuid());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy