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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Issue;
  22. import org.sonar.api.issue.IssueComment;
  23. import org.sonar.api.issue.internal.DefaultIssue;
  24. import org.sonar.api.issue.internal.DefaultIssueComment;
  25. import org.sonar.api.issue.internal.FieldDiffs;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.core.issue.db.IssueChangeDto;
  28. import org.sonar.core.issue.db.IssueChangeMapper;
  29. import org.sonar.core.issue.db.IssueDto;
  30. import org.sonar.core.issue.db.IssueMapper;
  31. import org.sonar.core.issue.db.UpdateConflictResolver;
  32. import org.sonar.core.persistence.DbSession;
  33. import org.sonar.core.persistence.MyBatis;
  34. import org.sonar.server.computation.ComputationContext;
  35. import org.sonar.server.computation.issue.IssueCache;
  36. import org.sonar.server.computation.issue.RuleCache;
  37. import org.sonar.server.db.DbClient;
  38. import org.sonar.server.util.CloseableIterator;
  39. public class PersistIssuesStep implements ComputationStep {
  40. private final DbClient dbClient;
  41. private final System2 system2;
  42. private final UpdateConflictResolver conflictResolver;
  43. private final RuleCache ruleCache;
  44. private final IssueCache issueCache;
  45. public PersistIssuesStep(DbClient dbClient, System2 system2, UpdateConflictResolver conflictResolver,
  46. RuleCache ruleCache, IssueCache issueCache) {
  47. this.dbClient = dbClient;
  48. this.system2 = system2;
  49. this.conflictResolver = conflictResolver;
  50. this.ruleCache = ruleCache;
  51. this.issueCache = issueCache;
  52. }
  53. @Override
  54. public void execute(ComputationContext context) {
  55. DbSession session = dbClient.openSession(true);
  56. IssueMapper mapper = session.getMapper(IssueMapper.class);
  57. IssueChangeMapper changeMapper = session.getMapper(IssueChangeMapper.class);
  58. CloseableIterator<DefaultIssue> issues = issueCache.traverse();
  59. try {
  60. while (issues.hasNext()) {
  61. DefaultIssue issue = issues.next();
  62. boolean saved = false;
  63. if (issue.isNew()) {
  64. Integer ruleId = ruleCache.get(issue.ruleKey()).getId();
  65. mapper.insert(IssueDto.toDtoForComputationInsert(issue, ruleId, system2.now()));
  66. saved = true;
  67. } else if (issue.isChanged()) {
  68. IssueDto dto = IssueDto.toDtoForUpdate(issue, system2.now());
  69. if (Issue.STATUS_CLOSED.equals(issue.status()) || issue.selectedAt() == null) {
  70. // Issue is closed by scan or changed by end-user
  71. mapper.update(dto);
  72. } else {
  73. int updateCount = mapper.updateIfBeforeSelectedDate(dto);
  74. if (updateCount == 0) {
  75. // End-user and scan changed the issue at the same time.
  76. // See https://jira.sonarsource.com/browse/SONAR-4309
  77. conflictResolver.resolve(issue, mapper);
  78. }
  79. }
  80. saved = true;
  81. }
  82. if (saved) {
  83. insertChanges(changeMapper, issue);
  84. }
  85. }
  86. session.flushStatements();
  87. session.commit();
  88. } finally {
  89. MyBatis.closeQuietly(session);
  90. issues.close();
  91. }
  92. }
  93. private void insertChanges(IssueChangeMapper mapper, DefaultIssue issue) {
  94. for (IssueComment comment : issue.comments()) {
  95. DefaultIssueComment c = (DefaultIssueComment) comment;
  96. if (c.isNew()) {
  97. IssueChangeDto changeDto = IssueChangeDto.of(c);
  98. mapper.insert(changeDto);
  99. }
  100. }
  101. FieldDiffs diffs = issue.currentChange();
  102. if (!issue.isNew() && diffs != null) {
  103. IssueChangeDto changeDto = IssueChangeDto.of(issue.key(), diffs);
  104. mapper.insert(changeDto);
  105. }
  106. }
  107. @Override
  108. public String getDescription() {
  109. return "Persist issues";
  110. }
  111. }