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 org.sonar.db.issue.IssueDao.DEFAULT_PAGE_SIZE;
34 public class PullActionIssuesRetriever {
36 private final DbClient dbClient;
37 private final IssueQueryParams issueQueryParams;
39 public PullActionIssuesRetriever(DbClient dbClient, IssueQueryParams queryParams) {
40 this.dbClient = dbClient;
41 this.issueQueryParams = queryParams;
44 public void processIssuesByBatch(DbSession dbSession, Set<String> issueKeysSnapshot, Consumer<List<IssueDto>> listConsumer) {
45 boolean hasMoreIssues = !issueKeysSnapshot.isEmpty();
48 List<IssueDto> issueDtos = new ArrayList<>();
50 while (hasMoreIssues) {
51 Set<String> page = paginate(issueKeysSnapshot, offset);
52 issueDtos.addAll(filterIssues(nextOpenIssues(dbSession, page)));
53 offset += page.size();
54 hasMoreIssues = offset < issueKeysSnapshot.size();
57 listConsumer.accept(issueDtos);
60 private List<IssueDto> filterIssues(List<IssueDto> issues) {
63 .filter(i -> hasCorrectTypeAndStatus(i, issueQueryParams))
67 private static boolean hasCorrectTypeAndStatus(IssueDto issueDto, IssueQueryParams queryParams) {
68 return issueDto.getType() != 4 &&
69 (queryParams.isResolvedOnly() ? issueDto.getStatus().equals("RESOLVED") : true);
72 public List<String> retrieveClosedIssues(DbSession dbSession) {
73 return dbClient.issueDao().selectRecentlyClosedIssues(dbSession, issueQueryParams);
76 private List<IssueDto> nextOpenIssues(DbSession dbSession, Set<String> issueKeysSnapshot) {
77 return dbClient.issueDao().selectByBranch(dbSession, issueKeysSnapshot, issueQueryParams);
80 private static Set<String> paginate(Set<String> issueKeys, long offset) {
84 .limit(DEFAULT_PAGE_SIZE)
85 .collect(Collectors.toSet());