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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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,
  45. AnalysisMetadataHolder analysisMetadataHolder, PeriodHolder periodHolder) {
  46. this.system2 = system2;
  47. this.dbClient = dbClient;
  48. this.treeRootHolder = treeRootHolder;
  49. this.analysisMetadataHolder = analysisMetadataHolder;
  50. this.periodHolder = periodHolder;
  51. }
  52. @Override
  53. public void execute(ComputationStep.Context context) {
  54. try (DbSession dbSession = dbClient.openSession(false)) {
  55. new DepthTraversalTypeAwareCrawler(
  56. new PersistSnapshotsPathAwareVisitor(dbSession, analysisMetadataHolder.getAnalysisDate()))
  57. .visit(treeRootHolder.getRoot());
  58. dbSession.commit();
  59. }
  60. }
  61. private class PersistSnapshotsPathAwareVisitor extends TypeAwareVisitorAdapter {
  62. private final DbSession dbSession;
  63. private final long analysisDate;
  64. public PersistSnapshotsPathAwareVisitor(DbSession dbSession, long analysisDate) {
  65. super(CrawlerDepthLimit.ROOTS, Order.PRE_ORDER);
  66. this.dbSession = dbSession;
  67. this.analysisDate = analysisDate;
  68. }
  69. @Override
  70. public void visitProject(Component project) {
  71. SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), project);
  72. updateSnapshotPeriods(snapshot);
  73. persist(snapshot, dbSession);
  74. }
  75. @Override
  76. public void visitView(Component view) {
  77. SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), view);
  78. updateSnapshotPeriods(snapshot);
  79. persist(snapshot, dbSession);
  80. }
  81. private void updateSnapshotPeriods(SnapshotDto snapshotDto) {
  82. if (!periodHolder.hasPeriod()) {
  83. return;
  84. }
  85. Period period = periodHolder.getPeriod();
  86. snapshotDto.setPeriodMode(period.getMode());
  87. snapshotDto.setPeriodParam(period.getModeParameter());
  88. snapshotDto.setPeriodDate(period.getSnapshotDate());
  89. }
  90. private SnapshotDto createAnalysis(String snapshotUuid, Component component) {
  91. String componentUuid = component.getUuid();
  92. String projectVersion = component.getType() == PROJECT ? component.getProjectAttributes().getProjectVersion() : null;
  93. String buildString = component.getType() == PROJECT ? component.getProjectAttributes().getBuildString().orElse(null) : null;
  94. SnapshotDto dto = new SnapshotDto()
  95. .setUuid(snapshotUuid)
  96. .setProjectVersion(projectVersion)
  97. .setBuildString(buildString)
  98. .setComponentUuid(componentUuid)
  99. .setLast(false)
  100. .setStatus(SnapshotDto.STATUS_UNPROCESSED)
  101. .setCreatedAt(analysisDate)
  102. .setBuildDate(system2.now());
  103. if (component.getType() == PROJECT) {
  104. component.getProjectAttributes().getScmRevisionId().ifPresent(dto::setRevision);
  105. }
  106. return dto;
  107. }
  108. private void persist(SnapshotDto snapshotDto, DbSession dbSession) {
  109. dbClient.snapshotDao().insert(dbSession, snapshotDto);
  110. }
  111. }
  112. @Override
  113. public String getDescription() {
  114. return "Persist analysis";
  115. }
  116. }