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.

PersistIssuesStep.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.computation.step;
  21. import org.sonar.api.issue.IssueComment;
  22. import org.sonar.api.utils.System2;
  23. import org.sonar.core.issue.DefaultIssue;
  24. import org.sonar.core.issue.DefaultIssueComment;
  25. import org.sonar.core.issue.FieldDiffs;
  26. import org.sonar.db.issue.IssueChangeDto;
  27. import org.sonar.db.issue.IssueChangeMapper;
  28. import org.sonar.db.issue.IssueDto;
  29. import org.sonar.db.issue.IssueMapper;
  30. import org.sonar.server.computation.issue.UpdateConflictResolver;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.MyBatis;
  33. import org.sonar.server.computation.issue.IssueCache;
  34. import org.sonar.server.computation.issue.RuleRepository;
  35. import org.sonar.server.db.DbClient;
  36. import org.sonar.server.util.CloseableIterator;
  37. public class PersistIssuesStep implements ComputationStep {
  38. private final DbClient dbClient;
  39. private final System2 system2;
  40. private final UpdateConflictResolver conflictResolver;
  41. private final RuleRepository ruleRepository;
  42. private final IssueCache issueCache;
  43. public PersistIssuesStep(DbClient dbClient, System2 system2, UpdateConflictResolver conflictResolver,
  44. RuleRepository ruleRepository, IssueCache issueCache) {
  45. this.dbClient = dbClient;
  46. this.system2 = system2;
  47. this.conflictResolver = conflictResolver;
  48. this.ruleRepository = ruleRepository;
  49. this.issueCache = issueCache;
  50. }
  51. @Override
  52. public void execute() {
  53. DbSession session = dbClient.openSession(true);
  54. IssueMapper mapper = session.getMapper(IssueMapper.class);
  55. IssueChangeMapper changeMapper = session.getMapper(IssueChangeMapper.class);
  56. CloseableIterator<DefaultIssue> issues = issueCache.traverse();
  57. try {
  58. while (issues.hasNext()) {
  59. DefaultIssue issue = issues.next();
  60. boolean saved = false;
  61. if (issue.isNew()) {
  62. Integer ruleId = ruleRepository.getByKey(issue.ruleKey()).getId();
  63. IssueDto dto = IssueDto.toDtoForComputationInsert(issue, ruleId, system2.now());
  64. mapper.insert(dto);
  65. saved = true;
  66. } else if (issue.isChanged()) {
  67. IssueDto dto = IssueDto.toDtoForUpdate(issue, system2.now());
  68. int updateCount = mapper.updateIfBeforeSelectedDate(dto);
  69. if (updateCount == 0) {
  70. // End-user and scan changed the issue at the same time.
  71. // See https://jira.sonarsource.com/browse/SONAR-4309
  72. conflictResolver.resolve(issue, mapper);
  73. }
  74. saved = true;
  75. }
  76. if (saved) {
  77. insertChanges(changeMapper, issue);
  78. }
  79. }
  80. session.flushStatements();
  81. session.commit();
  82. } finally {
  83. MyBatis.closeQuietly(session);
  84. issues.close();
  85. }
  86. }
  87. private static void insertChanges(IssueChangeMapper mapper, DefaultIssue issue) {
  88. for (IssueComment comment : issue.comments()) {
  89. DefaultIssueComment c = (DefaultIssueComment) comment;
  90. if (c.isNew()) {
  91. IssueChangeDto changeDto = IssueChangeDto.of(c);
  92. mapper.insert(changeDto);
  93. }
  94. }
  95. FieldDiffs diffs = issue.currentChange();
  96. if (!issue.isNew() && diffs != null) {
  97. IssueChangeDto changeDto = IssueChangeDto.of(issue.key(), diffs);
  98. mapper.insert(changeDto);
  99. }
  100. }
  101. @Override
  102. public String getDescription() {
  103. return "Persist issues";
  104. }
  105. }