]> source.dussan.org Git - sonarqube.git/blob
e2811f01c21fa415c5c79a1088590fb53b6a02d1
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.server.issue.ws.pull;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.function.Consumer;
26 import java.util.stream.Collectors;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.issue.IssueDto;
30 import org.sonar.db.issue.IssueQueryParams;
31
32 import static java.util.stream.Collectors.toList;
33 import static org.sonar.db.issue.IssueDao.DEFAULT_PAGE_SIZE;
34
35 public class PullActionIssuesRetriever {
36
37   private final DbClient dbClient;
38   private final IssueQueryParams issueQueryParams;
39
40   public PullActionIssuesRetriever(DbClient dbClient, IssueQueryParams queryParams) {
41     this.dbClient = dbClient;
42     this.issueQueryParams = queryParams;
43   }
44
45   public void processIssuesByBatch(DbSession dbSession, Set<String> issueKeysSnapshot, Consumer<List<IssueDto>> listConsumer) {
46     boolean hasMoreIssues = !issueKeysSnapshot.isEmpty();
47     long offset = 0;
48
49     List<IssueDto> issueDtos = new ArrayList<>();
50
51     while (hasMoreIssues) {
52       Set<String> page = paginate(issueKeysSnapshot, offset);
53       issueDtos.addAll(filterIssues(nextOpenIssues(dbSession, page)));
54       offset += page.size();
55       hasMoreIssues = offset < issueKeysSnapshot.size();
56     }
57
58     listConsumer.accept(issueDtos);
59   }
60
61   private List<IssueDto> filterIssues(List<IssueDto> issues) {
62     return issues
63       .stream()
64       .filter(i -> hasCorrectTypeAndStatus(i, issueQueryParams))
65       .collect(toList());
66   }
67
68   private static boolean hasCorrectTypeAndStatus(IssueDto issueDto, IssueQueryParams queryParams) {
69     return issueDto.getType() != 4 &&
70       (queryParams.isResolvedOnly() ? issueDto.getStatus().equals("RESOLVED") : true);
71   }
72
73   public List<String> retrieveClosedIssues(DbSession dbSession) {
74     return dbClient.issueDao().selectRecentlyClosedIssues(dbSession, issueQueryParams);
75   }
76
77   private List<IssueDto> nextOpenIssues(DbSession dbSession, Set<String> issueKeysSnapshot) {
78     return dbClient.issueDao().selectByBranch(dbSession, issueKeysSnapshot, issueQueryParams);
79   }
80
81   private static Set<String> paginate(Set<String> issueKeys, long offset) {
82     return issueKeys
83       .stream()
84       .skip(offset)
85       .limit(DEFAULT_PAGE_SIZE)
86       .collect(Collectors.toSet());
87   }
88 }