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.

SelectAction.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 java.util.Optional;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.server.ws.Request;
  24. import org.sonar.api.server.ws.Response;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.core.util.Uuids;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.db.organization.OrganizationDto;
  31. import org.sonar.db.property.PropertyDto;
  32. import org.sonar.db.qualitygate.QGateWithOrgDto;
  33. import org.sonar.server.component.ComponentFinder;
  34. import org.sonar.server.component.ComponentFinder.ParamNames;
  35. import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY;
  36. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_SELECT;
  37. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_GATE_ID;
  38. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_ID;
  39. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_KEY;
  40. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  41. public class SelectAction implements QualityGatesWsAction {
  42. private final DbClient dbClient;
  43. private final ComponentFinder componentFinder;
  44. private final QualityGatesWsSupport wsSupport;
  45. public SelectAction(DbClient dbClient, ComponentFinder componentFinder, QualityGatesWsSupport wsSupport) {
  46. this.dbClient = dbClient;
  47. this.componentFinder = componentFinder;
  48. this.wsSupport = wsSupport;
  49. }
  50. @Override
  51. public void define(WebService.NewController controller) {
  52. WebService.NewAction action = controller.createAction(ACTION_SELECT)
  53. .setDescription("Associate a project to a quality gate.<br>" +
  54. "The '%s' or '%s' must be provided.<br>" +
  55. "Project id as a numeric value is deprecated since 6.1. Please use the id similar to '%s'.<br>" +
  56. "Requires the 'Administer Quality Gates' permission.",
  57. PARAM_PROJECT_ID, PARAM_PROJECT_KEY,
  58. Uuids.UUID_EXAMPLE_02)
  59. .setPost(true)
  60. .setSince("4.3")
  61. .setHandler(this);
  62. action.createParam(PARAM_GATE_ID)
  63. .setDescription("Quality gate id")
  64. .setRequired(true)
  65. .setExampleValue("1");
  66. action.createParam(PARAM_PROJECT_ID)
  67. .setDescription("Project id. Project id as an numeric value is deprecated since 6.1")
  68. .setExampleValue(Uuids.UUID_EXAMPLE_01);
  69. action.createParam(PARAM_PROJECT_KEY)
  70. .setDescription("Project key")
  71. .setExampleValue(KEY_PROJECT_EXAMPLE_001)
  72. .setSince("6.1");
  73. wsSupport.createOrganizationParam(action);
  74. }
  75. @Override
  76. public void handle(Request request, Response response) {
  77. long gateId = request.mandatoryParamAsLong(PARAM_GATE_ID);
  78. String projectId = request.param(PARAM_PROJECT_ID);
  79. String projectKey = request.param(PARAM_PROJECT_KEY);
  80. try (DbSession dbSession = dbClient.openSession(false)) {
  81. OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
  82. QGateWithOrgDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, gateId);
  83. ComponentDto project = getProject(dbSession, organization, projectId, projectKey);
  84. wsSupport.checkCanAdminProject(organization, project);
  85. dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
  86. .setKey(SONAR_QUALITYGATE_PROPERTY)
  87. .setResourceId(project.getId())
  88. .setValue(String.valueOf(qualityGate.getId())));
  89. dbSession.commit();
  90. }
  91. response.noContent();
  92. }
  93. private ComponentDto getProject(DbSession dbSession, OrganizationDto organization, @Nullable String projectId, @Nullable String projectKey) {
  94. ComponentDto project = selectProjectById(dbSession, projectId)
  95. .orElseGet(() -> componentFinder.getByUuidOrKey(dbSession, projectId, projectKey, ParamNames.PROJECT_ID_AND_KEY));
  96. wsSupport.checkProjectBelongsToOrganization(organization, project);
  97. return project;
  98. }
  99. private Optional<ComponentDto> selectProjectById(DbSession dbSession, @Nullable String projectId) {
  100. if (projectId == null) {
  101. return Optional.empty();
  102. }
  103. try {
  104. long dbId = Long.parseLong(projectId);
  105. return Optional.ofNullable(dbClient.componentDao().selectById(dbSession, dbId).orElse(null));
  106. } catch (NumberFormatException e) {
  107. return Optional.empty();
  108. }
  109. }
  110. }