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.

QualityGateAction.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.badge.ws;
  21. import com.google.common.io.Resources;
  22. import org.sonar.api.measures.CoreMetrics;
  23. import org.sonar.api.measures.Metric.Level;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.api.server.ws.Response;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.api.server.ws.WebService.NewAction;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.db.measure.LiveMeasureDto;
  32. import org.sonar.server.component.ComponentFinder;
  33. import org.sonar.server.exceptions.ForbiddenException;
  34. import org.sonar.server.exceptions.NotFoundException;
  35. import org.sonar.server.user.UserSession;
  36. import static java.lang.String.format;
  37. import static java.nio.charset.StandardCharsets.UTF_8;
  38. import static org.apache.commons.io.IOUtils.write;
  39. import static org.sonar.api.web.UserRole.USER;
  40. import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
  41. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  42. import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001;
  43. import static org.sonarqube.ws.MediaTypes.SVG;
  44. public class QualityGateAction implements ProjectBadgesWsAction {
  45. private static final String PARAM_PROJECT = "project";
  46. private static final String PARAM_BRANCH = "branch";
  47. private static final String PARAM_PULL_REQUEST = "pullRequest";
  48. private final UserSession userSession;
  49. private final DbClient dbClient;
  50. private final ComponentFinder componentFinder;
  51. private final SvgGenerator svgGenerator;
  52. public QualityGateAction(UserSession userSession, DbClient dbClient, ComponentFinder componentFinder, SvgGenerator svgGenerator) {
  53. this.userSession = userSession;
  54. this.dbClient = dbClient;
  55. this.componentFinder = componentFinder;
  56. this.svgGenerator = svgGenerator;
  57. }
  58. @Override
  59. public void define(WebService.NewController controller) {
  60. NewAction action = controller.createAction("quality_gate")
  61. .setHandler(this)
  62. .setSince("7.1")
  63. .setDescription("Generate badge for project's quality gate as an SVG.<br/>" +
  64. "Requires 'Browse' permission on the specified project.")
  65. .setResponseExample(Resources.getResource(getClass(), "quality_gate-example.svg"));
  66. action.createParam(PARAM_PROJECT)
  67. .setDescription("Project key")
  68. .setRequired(true)
  69. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  70. action
  71. .createParam(PARAM_BRANCH)
  72. .setDescription("Branch key")
  73. .setExampleValue(KEY_BRANCH_EXAMPLE_001);
  74. action
  75. .createParam(PARAM_PULL_REQUEST)
  76. .setDescription("Pull request id")
  77. .setExampleValue(KEY_PULL_REQUEST_EXAMPLE_001);
  78. }
  79. @Override
  80. public void handle(Request request, Response response) throws Exception {
  81. response.stream().setMediaType(SVG);
  82. String projectKey = request.mandatoryParam(PARAM_PROJECT);
  83. String branch = request.param(PARAM_BRANCH);
  84. String pullRequest = request.param(PARAM_PULL_REQUEST);
  85. try (DbSession dbSession = dbClient.openSession(false)) {
  86. ComponentDto project = componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, projectKey, branch, pullRequest);
  87. userSession.checkComponentPermission(USER, project);
  88. Level qualityGateStatus = getQualityGate(dbSession, project);
  89. write(svgGenerator.generateQualityGate(qualityGateStatus), response.stream().output(), UTF_8);
  90. } catch (ProjectBadgesException | ForbiddenException | NotFoundException e) {
  91. write(svgGenerator.generateError(e.getMessage()), response.stream().output(), UTF_8);
  92. }
  93. }
  94. private Level getQualityGate(DbSession dbSession, ComponentDto project) {
  95. return Level.valueOf(dbClient.liveMeasureDao().selectMeasure(dbSession, project.uuid(), CoreMetrics.ALERT_STATUS_KEY)
  96. .map(LiveMeasureDto::getTextValue)
  97. .orElseThrow(() -> new ProjectBadgesException(format("Quality gate has not been found for project '%s' and branch '%s'", project.getKey(), project.getBranch()))));
  98. }
  99. }