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.

SetTagsAction.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.ws;
  21. import com.google.common.base.MoreObjects;
  22. import com.google.common.io.Resources;
  23. import java.util.Collections;
  24. import java.util.Date;
  25. import java.util.List;
  26. import org.sonar.api.server.ws.Change;
  27. import org.sonar.api.server.ws.Request;
  28. import org.sonar.api.server.ws.Response;
  29. import org.sonar.api.server.ws.WebService;
  30. import org.sonar.api.server.ws.WebService.NewAction;
  31. import org.sonar.core.issue.DefaultIssue;
  32. import org.sonar.core.issue.IssueChangeContext;
  33. import org.sonar.core.util.Uuids;
  34. import org.sonar.db.DbClient;
  35. import org.sonar.db.DbSession;
  36. import org.sonar.db.issue.IssueDto;
  37. import org.sonar.server.issue.IssueFieldsSetter;
  38. import org.sonar.server.issue.IssueFinder;
  39. import org.sonar.server.user.UserSession;
  40. import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder;
  41. import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_SET_TAGS;
  42. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUE;
  43. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_TAGS;
  44. /**
  45. * Set tags on an issue
  46. */
  47. public class SetTagsAction implements IssuesWsAction {
  48. private final UserSession userSession;
  49. private final DbClient dbClient;
  50. private final IssueFinder issueFinder;
  51. private final IssueFieldsSetter issueFieldsSetter;
  52. private final IssueUpdater issueUpdater;
  53. private final OperationResponseWriter responseWriter;
  54. public SetTagsAction(UserSession userSession, DbClient dbClient, IssueFinder issueFinder, IssueFieldsSetter issueFieldsSetter, IssueUpdater issueUpdater,
  55. OperationResponseWriter responseWriter) {
  56. this.userSession = userSession;
  57. this.dbClient = dbClient;
  58. this.issueFinder = issueFinder;
  59. this.issueFieldsSetter = issueFieldsSetter;
  60. this.issueUpdater = issueUpdater;
  61. this.responseWriter = responseWriter;
  62. }
  63. @Override
  64. public void define(WebService.NewController controller) {
  65. NewAction action = controller.createAction(ACTION_SET_TAGS)
  66. .setPost(true)
  67. .setSince("5.1")
  68. .setDescription("Set tags on an issue. <br/>" +
  69. "Requires authentication and Browse permission on project")
  70. .setChangelog(
  71. new Change("10.4", "The response fields 'status' and 'resolution' are deprecated. Please use 'issueStatus' instead."),
  72. new Change("10.4", "Add 'issueStatus' field to the response."),
  73. new Change("10.2", "Add 'impacts', 'cleanCodeAttribute', 'cleanCodeAttributeCategory' fields to the response"),
  74. new Change("9.6", "Response field 'ruleDescriptionContextKey' added"),
  75. new Change("8.8", "The response field components.uuid is removed"),
  76. new Change("6.5", "the database ids of the components are removed from the response"),
  77. new Change("6.5", "the response field components.uuid is deprecated. Use components.key instead."),
  78. new Change("6.4", "response contains issue information instead of list of tags"))
  79. .setResponseExample(Resources.getResource(this.getClass(), "set_tags-example.json"))
  80. .setHandler(this);
  81. action.createParam(PARAM_ISSUE)
  82. .setDescription("Issue key")
  83. .setSince("6.3")
  84. .setExampleValue(Uuids.UUID_EXAMPLE_01)
  85. .setRequired(true);
  86. action.createParam(PARAM_TAGS)
  87. .setDescription("Comma-separated list of tags. All tags are removed if parameter is empty or not set.")
  88. .setExampleValue("security,cwe,misra-c");
  89. }
  90. @Override
  91. public void handle(Request request, Response response) throws Exception {
  92. String key = request.mandatoryParam(PARAM_ISSUE);
  93. List<String> tags = MoreObjects.firstNonNull(request.paramAsStrings(PARAM_TAGS), Collections.emptyList());
  94. SearchResponseData preloadedSearchResponseData = setTags(key, tags);
  95. responseWriter.write(key, preloadedSearchResponseData, request, response);
  96. }
  97. private SearchResponseData setTags(String issueKey, List<String> tags) {
  98. userSession.checkLoggedIn();
  99. try (DbSession session = dbClient.openSession(false)) {
  100. IssueDto issueDto = issueFinder.getByKey(session, issueKey);
  101. DefaultIssue issue = issueDto.toDefaultIssue();
  102. IssueChangeContext context = issueChangeContextByUserBuilder(new Date(), userSession.getUuid()).build();
  103. if (issueFieldsSetter.setTags(issue, tags, context)) {
  104. return issueUpdater.saveIssueAndPreloadSearchResponseData(session, issue, context);
  105. }
  106. return new SearchResponseData(issueDto);
  107. }
  108. }
  109. }