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.

AbstractChangeTagsAction.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 com.google.common.base.Splitter;
  22. import com.google.common.base.Strings;
  23. import java.util.Collection;
  24. import java.util.HashSet;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import org.sonar.server.issue.workflow.IsUnResolved;
  28. import org.sonar.api.server.ServerSide;
  29. import org.sonar.api.server.rule.RuleTagFormat;
  30. import org.sonar.core.issue.DefaultIssue;
  31. import org.sonar.server.user.UserSession;
  32. @ServerSide
  33. public abstract class AbstractChangeTagsAction extends Action {
  34. public static final String TAGS_PARAMETER = "tags";
  35. private static final Splitter TAGS_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
  36. private final IssueFieldsSetter issueUpdater;
  37. protected AbstractChangeTagsAction(String key, IssueFieldsSetter issueUpdater) {
  38. super(key);
  39. this.issueUpdater = issueUpdater;
  40. super.setConditions(new IsUnResolved());
  41. }
  42. @Override
  43. public boolean verify(Map<String, Object> properties, Collection<DefaultIssue> issues, UserSession userSession) {
  44. parseTags(properties);
  45. return true;
  46. }
  47. @Override
  48. public boolean execute(Map<String, Object> properties, Context context) {
  49. Collection<String> tags = getTagsToSet(context, parseTags(properties));
  50. return issueUpdater.setTags(context.issue(), tags, context.issueChangeContext());
  51. }
  52. protected abstract Collection<String> getTagsToSet(Context context, Collection<String> tagsFromParams);
  53. @Override
  54. public boolean shouldRefreshMeasures() {
  55. return false;
  56. }
  57. private static Set<String> parseTags(Map<String, Object> properties) {
  58. Set<String> result = new HashSet<>();
  59. String tagsString = (String) properties.get(TAGS_PARAMETER);
  60. if (!Strings.isNullOrEmpty(tagsString)) {
  61. for (String tag : TAGS_SPLITTER.split(tagsString)) {
  62. RuleTagFormat.validate(tag);
  63. result.add(tag);
  64. }
  65. }
  66. return result;
  67. }
  68. }