You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SetTypeAction.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.sonar.server.issue;
  21. import java.util.Collection;
  22. import java.util.Map;
  23. import org.sonar.api.issue.Issue;
  24. import org.sonar.api.rules.RuleType;
  25. import org.sonar.api.web.UserRole;
  26. import org.sonar.core.issue.DefaultIssue;
  27. import org.sonar.server.issue.workflow.IsUnResolved;
  28. import org.sonar.server.user.UserSession;
  29. import static com.google.common.base.Preconditions.checkArgument;
  30. import static com.google.common.base.Strings.isNullOrEmpty;
  31. public class SetTypeAction extends Action {
  32. public static final String SET_TYPE_KEY = "set_type";
  33. public static final String TYPE_PARAMETER = "type";
  34. private final IssueFieldsSetter issueUpdater;
  35. private final UserSession userSession;
  36. public SetTypeAction(IssueFieldsSetter issueUpdater, UserSession userSession) {
  37. super(SET_TYPE_KEY);
  38. this.issueUpdater = issueUpdater;
  39. this.userSession = userSession;
  40. super.setConditions(new IsUnResolved(), this::isCurrentUserIssueAdmin);
  41. }
  42. private boolean isCurrentUserIssueAdmin(Issue issue) {
  43. return userSession.hasComponentUuidPermission(UserRole.ISSUE_ADMIN, issue.projectUuid());
  44. }
  45. @Override
  46. public boolean verify(Map<String, Object> properties, Collection<DefaultIssue> issues, UserSession userSession) {
  47. verifyTypeParameter(properties);
  48. return true;
  49. }
  50. @Override
  51. public boolean execute(Map<String, Object> properties, Context context) {
  52. String type = verifyTypeParameter(properties);
  53. return issueUpdater.setType(context.issue(), RuleType.valueOf(type), context.issueChangeContext());
  54. }
  55. @Override
  56. public boolean shouldRefreshMeasures() {
  57. return true;
  58. }
  59. private static String verifyTypeParameter(Map<String, Object> properties) {
  60. String type = (String) properties.get(TYPE_PARAMETER);
  61. checkArgument(!isNullOrEmpty(type), "Missing parameter : '%s'", TYPE_PARAMETER);
  62. checkArgument(RuleType.names().contains(type), "Unknown type : %s", type);
  63. return type;
  64. }
  65. }