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.

ProjectPersister.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.ce.task.projectanalysis.component;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.sonar.api.resources.Qualifiers;
  23. import org.sonar.api.utils.System2;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.project.ProjectDto;
  27. /**
  28. * Creates or updates the data in table {@code PROJECTS} for the current root.
  29. */
  30. public class ProjectPersister {
  31. private final DbClient dbClient;
  32. private final TreeRootHolder treeRootHolder;
  33. private final System2 system2;
  34. public ProjectPersister(DbClient dbClient, TreeRootHolder treeRootHolder, System2 system2) {
  35. this.dbClient = dbClient;
  36. this.treeRootHolder = treeRootHolder;
  37. this.system2 = system2;
  38. }
  39. public void persist(DbSession dbSession) {
  40. if (shouldSkip(treeRootHolder.getRoot())) {
  41. return;
  42. }
  43. ProjectDto dbProjectDto = dbClient.projectDao().selectProjectByKey(dbSession, treeRootHolder.getRoot().getKey())
  44. .orElseThrow(() -> new IllegalStateException("Project has been deleted by end-user during analysis"));
  45. ProjectDto projectDto = toProjectDto(treeRootHolder.getRoot(), dbProjectDto);
  46. if (hasChanged(dbProjectDto, projectDto)) {
  47. // insert or update in projects table
  48. dbClient.projectDao().update(dbSession, projectDto);
  49. }
  50. }
  51. private static boolean shouldSkip(Component rootComponent) {
  52. return !rootComponent.getType().equals(Component.Type.PROJECT) && !rootComponent.getType().equals(Component.Type.PROJECT_VIEW);
  53. }
  54. private static boolean hasChanged(ProjectDto dbProject, ProjectDto newProject) {
  55. return !StringUtils.equals(dbProject.getName(), newProject.getName()) ||
  56. !StringUtils.equals(dbProject.getDescription(), newProject.getDescription());
  57. }
  58. private ProjectDto toProjectDto(Component root, ProjectDto projectDtoFromDatabase) {
  59. ProjectDto projectDto = new ProjectDto();
  60. // Component has different uuid from project
  61. projectDto.setUuid(projectDtoFromDatabase.getUuid());
  62. projectDto.setName(root.getName());
  63. projectDto.setDescription(root.getDescription());
  64. projectDto.setUpdatedAt(system2.now());
  65. projectDto.setKey(root.getKey());
  66. projectDto.setQualifier(root.getType().equals(Component.Type.PROJECT) ? Qualifiers.PROJECT : Qualifiers.APP);
  67. return projectDto;
  68. }
  69. }