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.

GetByProjectAction.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.qualitygate.ws;
  21. import org.sonar.api.server.ws.Change;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.project.ProjectDto;
  28. import org.sonar.server.component.ComponentFinder;
  29. import org.sonar.server.qualitygate.QualityGateFinder;
  30. import org.sonar.server.qualitygate.QualityGateFinder.QualityGateData;
  31. import org.sonar.server.user.UserSession;
  32. import org.sonarqube.ws.Qualitygates.GetByProjectResponse;
  33. import static org.sonar.api.web.UserRole.ADMIN;
  34. import static org.sonar.api.web.UserRole.USER;
  35. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_GET_BY_PROJECT;
  36. import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
  37. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  38. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  39. public class GetByProjectAction implements QualityGatesWsAction {
  40. private static final String PARAM_PROJECT = "project";
  41. private final UserSession userSession;
  42. private final DbClient dbClient;
  43. private final ComponentFinder componentFinder;
  44. private final QualityGateFinder qualityGateFinder;
  45. public GetByProjectAction(UserSession userSession, DbClient dbClient, ComponentFinder componentFinder, QualityGateFinder qualityGateFinder) {
  46. this.userSession = userSession;
  47. this.dbClient = dbClient;
  48. this.componentFinder = componentFinder;
  49. this.qualityGateFinder = qualityGateFinder;
  50. }
  51. @Override
  52. public void define(WebService.NewController context) {
  53. WebService.NewAction action = context.createAction(ACTION_GET_BY_PROJECT)
  54. .setInternal(false)
  55. .setSince("6.1")
  56. .setDescription("Get the quality gate of a project.<br />" +
  57. "Requires one of the following permissions:" +
  58. "<ul>" +
  59. "<li>'Administer System'</li>" +
  60. "<li>'Administer' rights on the specified project</li>" +
  61. "<li>'Browse' on the specified project</li>" +
  62. "</ul>")
  63. .setResponseExample(getClass().getResource("get_by_project-example.json"))
  64. .setHandler(this)
  65. .setChangelog(
  66. new Change("8.4", "Field 'id' in the response is deprecated. Format changes from integer to string."),
  67. new Change("6.6", "The parameter 'projectId' has been removed"),
  68. new Change("6.6", "The parameter 'projectKey' has been renamed to 'project'"),
  69. new Change("6.6", "This webservice is now part of the public API"));
  70. action.createParam(PARAM_PROJECT)
  71. .setDescription("Project key")
  72. .setExampleValue(KEY_PROJECT_EXAMPLE_001)
  73. .setRequired(true);
  74. }
  75. @Override
  76. public void handle(Request request, Response response) throws Exception {
  77. try (DbSession dbSession = dbClient.openSession(false)) {
  78. ProjectDto project = componentFinder.getProjectByKey(dbSession, request.mandatoryParam(PARAM_PROJECT));
  79. if (!userSession.hasProjectPermission(USER, project) &&
  80. !userSession.hasProjectPermission(ADMIN, project)) {
  81. throw insufficientPrivilegesException();
  82. }
  83. QualityGateData data = qualityGateFinder.getEffectiveQualityGate(dbSession, project);
  84. writeProtobuf(buildResponse(data), request, response);
  85. }
  86. }
  87. private static GetByProjectResponse buildResponse(QualityGateData qg) {
  88. GetByProjectResponse.Builder response = GetByProjectResponse.newBuilder();
  89. response.getQualityGateBuilder()
  90. .setId(qg.getUuid())
  91. .setName(qg.getName())
  92. .setDefault(qg.isDefault());
  93. return response.build();
  94. }
  95. }