]> source.dussan.org Git - sonarqube.git/blob
646934a4ede895ca7c479e03cc7a504f313b32c7
[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 org.sonar.db.issue.IssueDao.DEFAULT_PAGE_SIZE;
33
34 public class PullActionIssuesRetriever {
35
36   private final DbClient dbClient;
37   private final IssueQueryParams issueQueryParams;
38
39   public PullActionIssuesRetriever(DbClient dbClient, IssueQueryParams queryParams) {
40     this.dbClient = dbClient;
41     this.issueQueryParams = queryParams;
42   }
43
44   public void processIssuesByBatch(DbSession dbSession, Set<String> issueKeysSnapshot, Consumer<List<IssueDto>> listConsumer) {
45     boolean hasMoreIssues = !issueKeysSnapshot.isEmpty();
46     long offset = 0;
47
48     List<IssueDto> issueDtos = new ArrayList<>();
49
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();
55     }
56
57     listConsumer.accept(issueDtos);
58   }
59
60   private List<IssueDto> filterIssues(List<IssueDto> issues) {
61     return issues
62       .stream()
63       .filter(i -> hasCorrectTypeAndStatus(i, issueQueryParams))
64       .toList();
65   }
66
67   private static boolean hasCorrectTypeAndStatus(IssueDto issueDto, IssueQueryParams queryParams) {
68     return issueDto.getType() != 4 &&
69       (queryParams.isResolvedOnly() ? issueDto.getStatus().equals("RESOLVED") : true);
70   }
71
72   public List<String> retrieveClosedIssues(DbSession dbSession) {
73     return dbClient.issueDao().selectRecentlyClosedIssues(dbSession, issueQueryParams);
74   }
75
76   private List<IssueDto> nextOpenIssues(DbSession dbSession, Set<String> issueKeysSnapshot) {
77     return dbClient.issueDao().selectByBranch(dbSession, issueKeysSnapshot, issueQueryParams);
78   }
79
80   private static Set<String> paginate(Set<String> issueKeys, long offset) {
81     return issueKeys
82       .stream()
83       .skip(offset)
84       .limit(DEFAULT_PAGE_SIZE)
85       .collect(Collectors.toSet());
86   }
87 }