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.

PersistAnalysisStep.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.ce.task.projectanalysis.step;
  21. import org.sonar.api.utils.System2;
  22. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  23. import org.sonar.ce.task.projectanalysis.component.Component;
  24. import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
  25. import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
  26. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  27. import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
  28. import org.sonar.ce.task.projectanalysis.period.Period;
  29. import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
  30. import org.sonar.ce.task.step.ComputationStep;
  31. import org.sonar.db.DbClient;
  32. import org.sonar.db.DbSession;
  33. import org.sonar.db.component.SnapshotDto;
  34. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  35. /**
  36. * Persist analysis
  37. */
  38. public class PersistAnalysisStep implements ComputationStep {
  39. private final System2 system2;
  40. private final DbClient dbClient;
  41. private final TreeRootHolder treeRootHolder;
  42. private final AnalysisMetadataHolder analysisMetadataHolder;
  43. private final PeriodHolder periodHolder;
  44. public PersistAnalysisStep(System2 system2, DbClient dbClient, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder, PeriodHolder periodHolder) {
  45. this.system2 = system2;
  46. this.dbClient = dbClient;
  47. this.treeRootHolder = treeRootHolder;
  48. this.analysisMetadataHolder = analysisMetadataHolder;
  49. this.periodHolder = periodHolder;
  50. }
  51. @Override
  52. public void execute(ComputationStep.Context context) {
  53. try (DbSession dbSession = dbClient.openSession(false)) {
  54. new DepthTraversalTypeAwareCrawler(
  55. new PersistSnapshotsPathAwareVisitor(dbSession, analysisMetadataHolder.getAnalysisDate()))
  56. .visit(treeRootHolder.getRoot());
  57. dbSession.commit();
  58. }
  59. }
  60. private class PersistSnapshotsPathAwareVisitor extends TypeAwareVisitorAdapter {
  61. private final DbSession dbSession;
  62. private final long analysisDate;
  63. public PersistSnapshotsPathAwareVisitor(DbSession dbSession, long analysisDate) {
  64. super(CrawlerDepthLimit.ROOTS, Order.PRE_ORDER);
  65. this.dbSession = dbSession;
  66. this.analysisDate = analysisDate;
  67. }
  68. @Override
  69. public void visitProject(Component project) {
  70. SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), project);
  71. updateSnapshotPeriods(snapshot);
  72. persist(snapshot, dbSession);
  73. }
  74. @Override
  75. public void visitView(Component view) {
  76. SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), view);
  77. updateSnapshotPeriods(snapshot);
  78. persist(snapshot, dbSession);
  79. }
  80. private void updateSnapshotPeriods(SnapshotDto snapshotDto) {
  81. if (!periodHolder.hasPeriod()) {
  82. return;
  83. }
  84. Period period = periodHolder.getPeriod();
  85. snapshotDto.setPeriodMode(period.getMode());
  86. snapshotDto.setPeriodParam(period.getModeParameter());
  87. snapshotDto.setPeriodDate(period.getDate());
  88. }
  89. private SnapshotDto createAnalysis(String snapshotUuid, Component component) {
  90. String componentUuid = component.getUuid();
  91. String projectVersion = component.getType() == PROJECT ? component.getProjectAttributes().getProjectVersion() : null;
  92. String buildString = component.getType() == PROJECT ? component.getProjectAttributes().getBuildString().orElse(null) : null;
  93. SnapshotDto dto = new SnapshotDto()
  94. .setUuid(snapshotUuid)
  95. .setProjectVersion(projectVersion)
  96. .setBuildString(buildString)
  97. .setComponentUuid(componentUuid)
  98. .setLast(false)
  99. .setStatus(SnapshotDto.STATUS_UNPROCESSED)
  100. .setCreatedAt(analysisDate)
  101. .setBuildDate(system2.now());
  102. if (component.getType() == PROJECT) {
  103. component.getProjectAttributes().getScmRevisionId().ifPresent(dto::setRevision);
  104. }
  105. return dto;
  106. }
  107. private void persist(SnapshotDto snapshotDto, DbSession dbSession) {
  108. dbClient.snapshotDao().insert(dbSession, snapshotDto);
  109. }
  110. }
  111. @Override
  112. public String getDescription() {
  113. return "Persist analysis";
  114. }
  115. }