-
Notifications
You must be signed in to change notification settings - Fork 2k
/
QgateProjectFinder.java
108 lines (93 loc) · 4.69 KB
/
QgateProjectFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.qualitygate;
import java.util.Collection;
import java.util.List;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.Paging;
import org.sonar.api.web.UserRole;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.ProjectQgateAssociation;
import org.sonar.db.qualitygate.ProjectQgateAssociationDao;
import org.sonar.db.qualitygate.ProjectQgateAssociationDto;
import org.sonar.db.qualitygate.ProjectQgateAssociationQuery;
import org.sonar.db.qualitygate.QualityGateDao;
import org.sonar.server.user.UserSession;
import static org.sonar.api.utils.Paging.forPageIndex;
import static org.sonar.server.ws.WsUtils.checkFound;
@ServerSide
public class QgateProjectFinder {
private final DbClient dbClient;
private final QualityGateDao qualitygateDao;
private final ProjectQgateAssociationDao associationDao;
private final UserSession userSession;
public QgateProjectFinder(DbClient dbClient, UserSession userSession) {
this.dbClient = dbClient;
this.userSession = userSession;
this.qualitygateDao = dbClient.qualityGateDao();
this.associationDao = dbClient.projectQgateAssociationDao();
}
public Association find(DbSession dbSession, OrganizationDto organization, ProjectQgateAssociationQuery query) {
getQualityGateId(dbSession, organization, query.gateId());
List<ProjectQgateAssociationDto> projects = associationDao.selectProjects(dbSession, query);
List<ProjectQgateAssociationDto> authorizedProjects = keepAuthorizedProjects(dbSession, projects);
Paging paging = forPageIndex(query.pageIndex())
.withPageSize(query.pageSize())
.andTotal(authorizedProjects.size());
return new Association(toProjectAssociations(getPaginatedProjects(authorizedProjects, paging)), paging.hasNextPage());
}
private Long getQualityGateId(DbSession dbSession, OrganizationDto organization, String gateId) {
return checkFound(qualitygateDao.selectByOrganizationAndId(dbSession, organization, Long.valueOf(gateId)), "Quality gate '" + gateId + "' does not exists.").getId();
}
private static List<ProjectQgateAssociationDto> getPaginatedProjects(List<ProjectQgateAssociationDto> projects, Paging paging) {
return projects.stream().skip(paging.offset()).limit(paging.pageSize()).collect(MoreCollectors.toList());
}
private static List<ProjectQgateAssociation> toProjectAssociations(List<ProjectQgateAssociationDto> dtos) {
return dtos.stream().map(ProjectQgateAssociationDto::toQgateAssociation).collect(MoreCollectors.toList());
}
private List<ProjectQgateAssociationDto> keepAuthorizedProjects(DbSession dbSession, List<ProjectQgateAssociationDto> projects) {
if (userSession.isRoot()) {
// the method AuthorizationDao#keepAuthorizedProjectIds() should be replaced by
// a call to UserSession, which would transparently support roots.
// Meanwhile root is explicitly handled.
return projects;
}
List<Long> projectIds = projects.stream().map(ProjectQgateAssociationDto::getId).collect(MoreCollectors.toList());
Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, projectIds, userSession.getUserId(), UserRole.USER);
return projects.stream().filter(project -> authorizedProjectIds.contains(project.getId())).collect(MoreCollectors.toList());
}
public static class Association {
private List<ProjectQgateAssociation> projects;
private boolean hasMoreResults;
private Association(List<ProjectQgateAssociation> projects, boolean hasMoreResults) {
this.projects = projects;
this.hasMoreResults = hasMoreResults;
}
public List<ProjectQgateAssociation> projects() {
return projects;
}
public boolean hasMoreResults() {
return hasMoreResults;
}
}
}