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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.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.db.qualitygate.QualityGateDto;
  29. import static org.sonar.server.qualitygate.ws.CreateAction.NAME_MAXIMUM_LENGTH;
  30. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_SELECT;
  31. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_GATE_NAME;
  32. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_KEY;
  33. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  34. public class SelectAction implements QualityGatesWsAction {
  35. private final DbClient dbClient;
  36. private final QualityGatesWsSupport wsSupport;
  37. public SelectAction(DbClient dbClient, QualityGatesWsSupport wsSupport) {
  38. this.dbClient = dbClient;
  39. this.wsSupport = wsSupport;
  40. }
  41. @Override
  42. public void define(WebService.NewController controller) {
  43. WebService.NewAction action = controller.createAction(ACTION_SELECT)
  44. .setDescription("Associate a project to a quality gate.<br>" +
  45. "Requires one of the following permissions:" +
  46. "<ul>" +
  47. " <li>'Administer Quality Gates'</li>" +
  48. " <li>'Administer' right on the specified project</li>" +
  49. "</ul>")
  50. .setPost(true)
  51. .setSince("4.3")
  52. .setHandler(this)
  53. .setChangelog(
  54. new Change("10.0", "Parameter 'gateId' is removed. Use 'gateName' instead."),
  55. new Change("8.4", "Parameter 'gateName' added"),
  56. new Change("8.4", "Parameter 'gateId' is deprecated. Format changes from integer to string. Use 'gateName' instead."),
  57. new Change("8.3", "The parameter 'projectId' was removed"));
  58. action.createParam(PARAM_GATE_NAME)
  59. .setRequired(true)
  60. .setDescription("Name of the quality gate")
  61. .setMaximumLength(NAME_MAXIMUM_LENGTH)
  62. .setSince("8.4")
  63. .setExampleValue("SonarSource way");
  64. action.createParam(PARAM_PROJECT_KEY)
  65. .setRequired(true)
  66. .setDescription("Project key")
  67. .setExampleValue(KEY_PROJECT_EXAMPLE_001)
  68. .setSince("6.1");
  69. }
  70. @Override
  71. public void handle(Request request, Response response) {
  72. String gateName = request.mandatoryParam(PARAM_GATE_NAME);
  73. String projectKey = request.mandatoryParam(PARAM_PROJECT_KEY);
  74. try (DbSession dbSession = dbClient.openSession(false)) {
  75. QualityGateDto qualityGate;
  76. qualityGate = wsSupport.getByName(dbSession, gateName);
  77. ProjectDto project = wsSupport.getProject(dbSession, projectKey);
  78. wsSupport.checkCanAdminProject(project);
  79. QualityGateDto currentQualityGate = dbClient.qualityGateDao().selectByProjectUuid(dbSession, project.getUuid());
  80. if (currentQualityGate == null) {
  81. // project uses the default profile
  82. dbClient.projectQgateAssociationDao()
  83. .insertProjectQGateAssociation(dbSession, project.getUuid(), qualityGate.getUuid());
  84. dbSession.commit();
  85. } else if (!qualityGate.getUuid().equals(currentQualityGate.getUuid())) {
  86. dbClient.projectQgateAssociationDao()
  87. .updateProjectQGateAssociation(dbSession, project.getUuid(), qualityGate.getUuid());
  88. dbSession.commit();
  89. }
  90. }
  91. response.noContent();
  92. }
  93. }