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.

SetSeverityAction.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.io.Resources;
  22. import java.util.Date;
  23. import org.sonar.api.rule.Severity;
  24. import org.sonar.api.server.ws.Change;
  25. import org.sonar.api.server.ws.Request;
  26. import org.sonar.api.server.ws.Response;
  27. import org.sonar.api.server.ws.WebService;
  28. import org.sonar.core.issue.DefaultIssue;
  29. import org.sonar.core.issue.IssueChangeContext;
  30. import org.sonar.core.util.Uuids;
  31. import org.sonar.db.DbClient;
  32. import org.sonar.db.DbSession;
  33. import org.sonar.db.component.BranchDto;
  34. import org.sonar.db.issue.IssueDto;
  35. import org.sonar.server.issue.IssueFieldsSetter;
  36. import org.sonar.server.issue.IssueFinder;
  37. import org.sonar.server.pushapi.issues.IssueChangeEventService;
  38. import org.sonar.server.user.UserSession;
  39. import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
  40. import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder;
  41. import static org.sonar.db.component.BranchType.BRANCH;
  42. import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_SET_SEVERITY;
  43. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUE;
  44. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SEVERITY;
  45. public class SetSeverityAction implements IssuesWsAction {
  46. private final UserSession userSession;
  47. private final DbClient dbClient;
  48. private final IssueChangeEventService issueChangeEventService;
  49. private final IssueFinder issueFinder;
  50. private final IssueFieldsSetter issueFieldsSetter;
  51. private final IssueUpdater issueUpdater;
  52. private final OperationResponseWriter responseWriter;
  53. public SetSeverityAction(UserSession userSession, DbClient dbClient, IssueChangeEventService issueChangeEventService,
  54. IssueFinder issueFinder, IssueFieldsSetter issueFieldsSetter, IssueUpdater issueUpdater,
  55. OperationResponseWriter responseWriter) {
  56. this.userSession = userSession;
  57. this.dbClient = dbClient;
  58. this.issueChangeEventService = issueChangeEventService;
  59. this.issueFinder = issueFinder;
  60. this.issueFieldsSetter = issueFieldsSetter;
  61. this.issueUpdater = issueUpdater;
  62. this.responseWriter = responseWriter;
  63. }
  64. @Override
  65. public void define(WebService.NewController controller) {
  66. WebService.NewAction action = controller.createAction(ACTION_SET_SEVERITY)
  67. .setDescription("Change severity.<br/>" +
  68. "Requires the following permissions:" +
  69. "<ul>" +
  70. " <li>'Authentication'</li>" +
  71. " <li>'Browse' rights on project of the specified issue</li>" +
  72. " <li>'Administer Issues' rights on project of the specified issue</li>" +
  73. "</ul>")
  74. .setSince("3.6")
  75. .setChangelog(
  76. new Change("10.2", "This endpoint is now deprecated."),
  77. new Change("10.2", "Add 'impacts', 'cleanCodeAttribute', 'cleanCodeAttributeCategory' fields to the response"),
  78. new Change("9.6", "Response field 'ruleDescriptionContextKey' added"),
  79. new Change("8.8", "The response field components.uuid is removed"),
  80. new Change("6.5", "the database ids of the components are removed from the response"),
  81. new Change("6.5", "the response field components.uuid is deprecated. Use components.key instead."))
  82. .setHandler(this)
  83. .setDeprecatedSince("10.2")
  84. .setResponseExample(Resources.getResource(this.getClass(), "set_severity-example.json"))
  85. .setPost(true);
  86. action.createParam(PARAM_ISSUE)
  87. .setDescription("Issue key")
  88. .setRequired(true)
  89. .setExampleValue(Uuids.UUID_EXAMPLE_01);
  90. action.createParam(PARAM_SEVERITY)
  91. .setDescription("New severity")
  92. .setRequired(true)
  93. .setPossibleValues(Severity.ALL);
  94. }
  95. @Override
  96. public void handle(Request request, Response response) throws Exception {
  97. userSession.checkLoggedIn();
  98. String issueKey = request.mandatoryParam(PARAM_ISSUE);
  99. String severity = request.mandatoryParam(PARAM_SEVERITY);
  100. try (DbSession session = dbClient.openSession(false)) {
  101. SearchResponseData preloadedSearchResponseData = setType(session, issueKey, severity);
  102. responseWriter.write(issueKey, preloadedSearchResponseData, request, response);
  103. }
  104. }
  105. private SearchResponseData setType(DbSession session, String issueKey, String severity) {
  106. IssueDto issueDto = issueFinder.getByKey(session, issueKey);
  107. DefaultIssue issue = issueDto.toDefaultIssue();
  108. userSession.checkComponentUuidPermission(ISSUE_ADMIN, issue.projectUuid());
  109. IssueChangeContext context = issueChangeContextByUserBuilder(new Date(), userSession.getUuid()).withRefreshMeasures().build();
  110. if (issueFieldsSetter.setManualSeverity(issue, severity, context)) {
  111. BranchDto branch = issueUpdater.getBranch(session, issue);
  112. SearchResponseData response = issueUpdater.saveIssueAndPreloadSearchResponseData(session, issue, context, branch);
  113. if (branch.getBranchType().equals(BRANCH) && response.getComponentByUuid(issue.projectUuid()) != null) {
  114. issueChangeEventService.distributeIssueChangeEvent(issue, severity, null, null,
  115. branch, response.getComponentByUuid(issue.projectUuid()).getKey());
  116. }
  117. return response;
  118. }
  119. return new SearchResponseData(issueDto);
  120. }
  121. }