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-2016 SonarSource SA
 * mailto:contact 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 com.google.common.base.Strings;
import java.util.Collection;
import java.util.Map;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.condition.IsUnResolved;
import org.sonar.api.server.ServerSide;
import org.sonar.api.user.User;
import org.sonar.api.user.UserFinder;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.server.user.UserSession;

@ServerSide
public class AssignAction extends Action {

  private static final String ASSIGN_KEY = "assign";
  public static final String VERIFIED_ASSIGNEE = "verifiedAssignee";

  private final UserFinder userFinder;
  private final IssueUpdater issueUpdater;

  public AssignAction(UserFinder userFinder, IssueUpdater issueUpdater) {
    super(ASSIGN_KEY);
    this.userFinder = userFinder;
    this.issueUpdater = issueUpdater;
    super.setConditions(new IsUnResolved());
  }

  @Override
  public boolean verify(Map properties, Collection issues, UserSession userSession) {
    String assignee = assigneeValue(properties);
    if (!Strings.isNullOrEmpty(assignee)) {
      User user = selectUser(assignee);
      if (user == null) {
        throw new IllegalArgumentException("Unknown user: " + assignee);
      }
      properties.put(VERIFIED_ASSIGNEE, user);
    } else {
      properties.put(VERIFIED_ASSIGNEE, null);
    }
    return true;
  }

  @Override
  public boolean execute(Map properties, Context context) {
    if (!properties.containsKey(VERIFIED_ASSIGNEE)) {
      throw new IllegalArgumentException("Assignee is missing from the execution parameters");
    }
    User assignee = (User) properties.get(VERIFIED_ASSIGNEE);
    return issueUpdater.assign((DefaultIssue) context.issue(), assignee, context.issueChangeContext());
  }

  private String assigneeValue(Map properties) {
    return (String) properties.get("assignee");
  }

  private User selectUser(String assigneeKey) {
    return userFinder.findByLogin(assigneeKey);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy