3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.issue.ws.pull;
22 import java.util.ArrayList;
23 import java.util.List;
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;
32 import static java.util.stream.Collectors.toList;
33 import static org.sonar.db.issue.IssueDao.DEFAULT_PAGE_SIZE;
35 public class PullActionIssuesRetriever {
37 private final DbClient dbClient;
38 private final IssueQueryParams issueQueryParams;
40 public PullActionIssuesRetriever(DbClient dbClient, IssueQueryParams queryParams) {
41 this.dbClient = dbClient;
42 this.issueQueryParams = queryParams;
45 public void processIssuesByBatch(DbSession dbSession, Set<String> issueKeysSnapshot, Consumer<List<IssueDto>> listConsumer) {
46 boolean hasMoreIssues = !issueKeysSnapshot.isEmpty();
49 List<IssueDto> issueDtos = new ArrayList<>();
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();
58 listConsumer.accept(issueDtos);
61 private List<IssueDto> filterIssues(List<IssueDto> issues) {
64 .filter(i -> hasCorrectTypeAndStatus(i, issueQueryParams))
68 private static boolean hasCorrectTypeAndStatus(IssueDto issueDto, IssueQueryParams queryParams) {
69 return issueDto.getType() != 4 &&
70 (queryParams.isResolvedOnly() ? issueDto.getStatus().equals("RESOLVED") : true);
73 public List<String> retrieveClosedIssues(DbSession dbSession) {
74 return dbClient.issueDao().selectRecentlyClosedIssues(dbSession, issueQueryParams);
77 private List<IssueDto> nextOpenIssues(DbSession dbSession, Set<String> issueKeysSnapshot) {
78 return dbClient.issueDao().selectByBranch(dbSession, issueKeysSnapshot, issueQueryParams);
81 private static Set<String> paginate(Set<String> issueKeys, long offset) {
85 .limit(DEFAULT_PAGE_SIZE)
86 .collect(Collectors.toSet());