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.

ProjectBadgesSupport.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 org.sonar.api.server.ws.Request;
  22. import org.sonar.api.server.ws.WebService;
  23. import org.sonar.db.DbSession;
  24. import org.sonar.db.component.BranchDto;
  25. import org.sonar.db.project.ProjectDto;
  26. import org.sonar.server.component.ComponentFinder;
  27. import org.sonar.server.exceptions.NotFoundException;
  28. import org.sonar.server.user.UserSession;
  29. import static org.sonar.api.web.UserRole.USER;
  30. import static org.sonar.db.component.BranchType.BRANCH;
  31. import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
  32. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  33. public class ProjectBadgesSupport {
  34. private static final String PARAM_PROJECT = "project";
  35. private static final String PARAM_BRANCH = "branch";
  36. private final UserSession userSession;
  37. private final ComponentFinder componentFinder;
  38. public ProjectBadgesSupport(UserSession userSession, ComponentFinder componentFinder) {
  39. this.userSession = userSession;
  40. this.componentFinder = componentFinder;
  41. }
  42. void addProjectAndBranchParams(WebService.NewAction action) {
  43. action.createParam(PARAM_PROJECT)
  44. .setDescription("Project or application key")
  45. .setRequired(true)
  46. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  47. action
  48. .createParam(PARAM_BRANCH)
  49. .setDescription("Branch key")
  50. .setExampleValue(KEY_BRANCH_EXAMPLE_001);
  51. }
  52. BranchDto getBranch(DbSession dbSession, Request request) {
  53. try {
  54. String projectKey = request.mandatoryParam(PARAM_PROJECT);
  55. String branchName = request.param(PARAM_BRANCH);
  56. ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
  57. userSession.checkProjectPermission(USER, project);
  58. if (project.isPrivate()) {
  59. throw generateInvalidProjectException();
  60. }
  61. BranchDto branch;
  62. if (branchName == null) {
  63. branch = componentFinder.getMainBranch(dbSession, project);
  64. } else {
  65. branch = componentFinder.getBranchOrPullRequest(dbSession, project, branchName, null);
  66. }
  67. if (!branch.getBranchType().equals(BRANCH)) {
  68. throw generateInvalidProjectException();
  69. }
  70. return branch;
  71. } catch (NotFoundException e) {
  72. throw new NotFoundException("Project has not been found");
  73. }
  74. }
  75. private static ProjectBadgesException generateInvalidProjectException() {
  76. return new ProjectBadgesException("Project is invalid");
  77. }
  78. }