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.

ProjectDataLoader.java 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.batch;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import javax.annotation.Nullable;
  24. import org.sonar.api.server.ServerSide;
  25. import org.sonar.api.web.UserRole;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.component.ComponentDto;
  29. import org.sonar.db.component.FilePathWithHashDto;
  30. import org.sonar.db.permission.GlobalPermission;
  31. import org.sonar.scanner.protocol.input.FileData;
  32. import org.sonar.scanner.protocol.input.ProjectRepositories;
  33. import org.sonar.server.component.ComponentFinder;
  34. import org.sonar.server.exceptions.ForbiddenException;
  35. import org.sonar.server.user.UserSession;
  36. import static org.sonar.server.exceptions.BadRequestException.checkRequest;
  37. @ServerSide
  38. public class ProjectDataLoader {
  39. private final DbClient dbClient;
  40. private final UserSession userSession;
  41. private final ComponentFinder componentFinder;
  42. public ProjectDataLoader(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) {
  43. this.dbClient = dbClient;
  44. this.userSession = userSession;
  45. this.componentFinder = componentFinder;
  46. }
  47. public ProjectRepositories load(ProjectDataQuery query) {
  48. try (DbSession session = dbClient.openSession(false)) {
  49. String projectKey = query.getProjectKey();
  50. String branch = query.getBranch();
  51. String pullRequest = query.getPullRequest();
  52. ComponentDto project = componentFinder.getByKey(session, projectKey);
  53. checkRequest(project.isRootProject(), "Key '%s' belongs to a component which is not a Project", projectKey);
  54. boolean hasScanPerm = userSession.hasComponentPermission(UserRole.SCAN, project) ||
  55. userSession.hasPermission(GlobalPermission.SCAN);
  56. checkPermission(hasScanPerm);
  57. ComponentDto branchComponent = (branch == null && pullRequest == null) ? project
  58. : componentFinder.getByKeyAndOptionalBranchOrPullRequest(session, projectKey, branch, pullRequest);
  59. List<FilePathWithHashDto> files = searchFilesWithHashAndRevision(session, branchComponent);
  60. ProjectRepositories repository = new ProjectRepositories();
  61. addFileData(repository, files);
  62. return repository;
  63. }
  64. }
  65. private List<FilePathWithHashDto> searchFilesWithHashAndRevision(DbSession session, @Nullable ComponentDto branchComponent) {
  66. if (branchComponent == null) {
  67. return Collections.emptyList();
  68. }
  69. return dbClient.componentDao().selectEnabledFilesFromProject(session, branchComponent.uuid());
  70. }
  71. private static void addFileData(ProjectRepositories data, List<FilePathWithHashDto> files) {
  72. for (FilePathWithHashDto file : files) {
  73. FileData fileData = new FileData(file.getSrcHash(), file.getRevision());
  74. data.addFileData(file.getPath(), fileData);
  75. }
  76. }
  77. private static void checkPermission(boolean hasScanPerm) {
  78. if (!hasScanPerm) {
  79. throw new ForbiddenException("You're not authorized to push analysis results to the SonarQube server. " +
  80. "Please contact your SonarQube administrator.");
  81. }
  82. }
  83. }