Skip to content

Commit 87b8154

Browse files
committed
Added ability to select a defined group of users that will participate in Quboo.
1 parent ea1ccac commit 87b8154

File tree

6 files changed

+88
-37
lines changed

6 files changed

+88
-37
lines changed

src/main/java/io/tpd/quboo/sonarplugin/dtos/IssuesWrapper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ public IssuesWrapper() {
2727
public void filterAndAddIssues(final Issues issues,
2828
final List<String> selectedProjects,
2929
final List<String> excludedProjects,
30+
final List<String> selectedUsers,
3031
final String sonarVersion) {
3132
log.info("Quboo project filters - Included: {} | Excluded: {}",
3233
selectedProjects, excludedProjects);
34+
log.info("Quboo user filter - Included: {}",
35+
selectedUsers);
3336
this.issues.addAll(
3437
issues.getIssues().stream()
3538
.filter(issue -> issue.getProject() == null ||
@@ -41,6 +44,9 @@ public void filterAndAddIssues(final Issues issues,
4144
excludedProjects == null || excludedProjects.isEmpty() ||
4245
!excludedProjects.contains(issue.getProject())
4346
)
47+
.filter(issue -> issue.getAssignee() == null ||
48+
selectedUsers == null || selectedUsers.isEmpty() ||
49+
selectedUsers.contains(issue.getAssignee()))
4450
.filter(issue -> !QubooCache.INSTANCE.inCache(issue))
4551
.collect(Collectors.toList())
4652
);

src/main/java/io/tpd/quboo/sonarplugin/hooks/QubooConnector.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class QubooConnector implements PostProjectAnalysisTask {
3535
private OkHttpClient http;
3636
private List<String> selectedProjects;
3737
private List<String> rejectedProjects;
38+
private List<String> selectedUsers;
3839

3940
public QubooConnector(final Server server) {
4041
this.http = new HttpClients().getQubooTrustedOkHttpClient();
@@ -55,8 +56,9 @@ public void finished(ProjectAnalysis analysis) {
5556
final String qubooKey = analysis.getScannerContext().getProperties().get(QubooProperties.ACCESS_KEY);
5657
final String qubooSecret = analysis.getScannerContext().getProperties().get(QubooProperties.SECRET_KEY);
5758
final String token = analysis.getScannerContext().getProperties().get(QubooProperties.TOKEN_KEY);
58-
this.selectedProjects = extractProjectList(analysis.getScannerContext().getProperties().get(QubooProperties.SELECTED_PROJECTS_KEY));
59-
this.rejectedProjects = extractProjectList(analysis.getScannerContext().getProperties().get(QubooProperties.REJECTED_PROJECTS_KEY));
59+
this.selectedProjects = extractCommaSeparatedList(analysis.getScannerContext().getProperties().get(QubooProperties.SELECTED_PROJECTS_KEY));
60+
this.rejectedProjects = extractCommaSeparatedList(analysis.getScannerContext().getProperties().get(QubooProperties.REJECTED_PROJECTS_KEY));
61+
this.selectedUsers = extractCommaSeparatedList(analysis.getScannerContext().getProperties().get(QubooProperties.SELECTED_USERS_KEY));
6062
if (!isEmpty(token)) {
6163
log.info("A token will be used to connect to SonarQube server");
6264
}
@@ -75,16 +77,16 @@ public void finished(ProjectAnalysis analysis) {
7577
}
7678
}
7779

78-
private List<String> extractProjectList(final String commaSeparatedProjects) {
79-
if (StringUtils.isBlank(commaSeparatedProjects)) {
80+
private List<String> extractCommaSeparatedList(final String commaSeparatedList) {
81+
if (StringUtils.isBlank(commaSeparatedList)) {
8082
return new ArrayList<>();
8183
} else {
82-
List<String> projects = new ArrayList<>();
83-
String[] projectArray = commaSeparatedProjects.trim().split(",");
84-
for (String p : projectArray) {
85-
projects.add(p.trim()); // in case of spaces between commas
84+
List<String> list = new ArrayList<>();
85+
String[] array = commaSeparatedList.trim().split(",");
86+
for (String p : array) {
87+
list.add(p.trim()); // in case of spaces between commas
8688
}
87-
return projects;
89+
return list;
8890
}
8991
}
9092

@@ -124,7 +126,7 @@ private IssuesWrapper getIssues(final String token) {
124126
final String body = response.body().string();
125127
final Issues issues = mapper.readValue(body, Issues.class);
126128
log.info("Quboo plugin got {} issues", issues.getIssues().size());
127-
wrapper.filterAndAddIssues(issues, selectedProjects, rejectedProjects, server.getVersion());
129+
wrapper.filterAndAddIssues(issues, selectedProjects, rejectedProjects, selectedUsers, server.getVersion());
128130
moreData = moreData(issues.getPaging(), issues.getIssues().size());
129131
pageNumber++;
130132
if (pageNumber > 50) { // there is a hard limit in Sonar API that doesn't allow querying more than 10K results

src/main/java/io/tpd/quboo/sonarplugin/hooks/QubooSensor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void execute(final SensorContext context) {
2525
toContextPropertyIfPresent(context, QubooProperties.TOKEN_KEY);
2626
toContextPropertyIfPresent(context, QubooProperties.SELECTED_PROJECTS_KEY);
2727
toContextPropertyIfPresent(context, QubooProperties.REJECTED_PROJECTS_KEY);
28+
toContextPropertyIfPresent(context, QubooProperties.SELECTED_USERS_KEY);
2829
Optional<String> accessKeyOptional = context.config().get(QubooProperties.ACCESS_KEY);
2930
if (accessKeyOptional.isPresent() && accessKeyOptional.get().equals(QubooProperties.DEFAULT_ACCESS_KEY)) {
3031
log.warn("WARNING: Quboo will ignore this analysis because you haven't set the Quboo Access (and Secret) Keys. Go to your Sonarqube server (as admin), Administration -> Configuration -> Quboo and enter the values you find in your Quboo account settings.");

src/main/java/io/tpd/quboo/sonarplugin/settings/QubooProperties.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class QubooProperties {
3636
public static final String CATEGORY = "Quboo";
3737
public static final String SELECTED_PROJECTS_KEY = "sonar.quboo.selected-projects";
3838
public static final String REJECTED_PROJECTS_KEY = "sonar.quboo.rejected-projects";
39+
public static final String SELECTED_USERS_KEY = "sonar.quboo.selected-users";
3940

4041
private QubooProperties() {
4142
// only statics
@@ -88,6 +89,17 @@ public static List<PropertyDefinition> getProperties() {
8889
.subCategory("Filters")
8990
.type(PropertyType.STRING)
9091
.index(5)
92+
.build(),
93+
PropertyDefinition.builder(SELECTED_USERS_KEY)
94+
.name("Selected users")
95+
.description("If you want to select a limited set of users that will participate in Quboo, " +
96+
"enter here their login names (usernames in Sonarqube) separated by commas, for example: janedoe,johndoe. Leave it empty " +
97+
"to include all users.")
98+
.defaultValue("")
99+
.category(CATEGORY)
100+
.subCategory("Filters")
101+
.type(PropertyType.STRING)
102+
.index(6)
91103
.build()
92104
);
93105
}

src/test/java/io/tpd/quboo/sonarplugin/dtos/IssuesWrapperTest.java

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,84 +23,102 @@ public void setup() {
2323
@Test
2424
public void issueWithoutProjectIncluded() {
2525
Issues issues = buildIssuesWithProjects(null, null);
26-
List<String> selected = projects("project1");
27-
List<String> rejected = projects("project2");
28-
w.filterAndAddIssues(issues, selected, rejected, null);
26+
List<String> selected = toList("project1");
27+
List<String> rejected = toList("project2");
28+
w.filterAndAddIssues(issues, selected, rejected, null, null);
2929

3030
assertThat(w.getIssues()).hasSize(2);
3131
}
3232

3333
@Test
3434
public void issueWithNoMatchIncluded() {
3535
Issues issues = buildIssuesWithProjects("project3");
36-
List<String> selected = projects();
37-
List<String> rejected = projects("project2");
38-
w.filterAndAddIssues(issues, selected, rejected, null);
36+
List<String> selected = toList();
37+
List<String> rejected = toList("project2");
38+
w.filterAndAddIssues(issues, selected, rejected, null, null);
3939

4040
assertThat(w.getIssues()).hasSize(1);
4141
}
4242

4343
@Test
4444
public void issueWithSelectedMatchIncluded() {
4545
Issues issues = buildIssuesWithProjects("project1");
46-
List<String> selected = projects("project1");
47-
List<String> rejected = projects("project3");
48-
w.filterAndAddIssues(issues, selected, rejected, null);
46+
List<String> selected = toList("project1");
47+
List<String> rejected = toList("project3");
48+
w.filterAndAddIssues(issues, selected, rejected, null, null);
4949

5050
assertThat(w.getIssues()).hasSize(1);
5151
}
5252

5353
@Test
5454
public void issueWithNoSelectedMatchExcluded() {
5555
Issues issues = buildIssuesWithProjects("project2");
56-
List<String> selected = projects("project1");
57-
List<String> rejected = projects();
58-
w.filterAndAddIssues(issues, selected, rejected, null);
56+
List<String> selected = toList("project1");
57+
List<String> rejected = toList();
58+
w.filterAndAddIssues(issues, selected, rejected, null, null);
5959

6060
assertThat(w.getIssues()).isEmpty();
6161
}
6262

6363
@Test
6464
public void issueWithDoubleMatchNotExcluded() {
6565
Issues issues = buildIssuesWithProjects("project1");
66-
List<String> selected = projects("project1");
67-
List<String> rejected = projects("project1");
68-
w.filterAndAddIssues(issues, selected, rejected, null);
66+
List<String> selected = toList("project1");
67+
List<String> rejected = toList("project1");
68+
w.filterAndAddIssues(issues, selected, rejected, null, null);
6969

7070
assertThat(w.getIssues()).hasSize(1);
7171
}
7272

7373
@Test
7474
public void allIssuesIncludedWithEmptyFilters() {
7575
Issues issues = buildIssuesWithProjects("project1", null, "project2", "project3");
76-
List<String> selected = projects();
77-
List<String> rejected = projects();
78-
w.filterAndAddIssues(issues, selected, rejected, null);
76+
List<String> selected = toList();
77+
List<String> rejected = toList();
78+
w.filterAndAddIssues(issues, selected, rejected, null, null);
7979

8080
assertThat(w.getIssues()).hasSize(4);
8181
}
8282

8383
@Test
8484
public void multipleExcluded() {
8585
Issues issues = buildIssuesWithProjects("project1", null, "project2", "project3");
86-
List<String> selected = projects();
87-
List<String> rejected = projects("project1", "project3");
88-
w.filterAndAddIssues(issues, selected, rejected, null);
86+
List<String> selected = toList();
87+
List<String> rejected = toList("project1", "project3");
88+
w.filterAndAddIssues(issues, selected, rejected, null, null);
8989

9090
assertThat(w.getIssues()).extracting("project").containsExactlyInAnyOrder(null, "project2");
9191
}
9292

9393
@Test
9494
public void bothFilters1() {
9595
Issues issues = buildIssuesWithProjects("project1", null, "project2", "project3", "project5");
96-
List<String> selected = projects("project2");
97-
List<String> rejected = projects("project1", "project3");
98-
w.filterAndAddIssues(issues, selected, rejected, null);
96+
List<String> selected = toList("project2");
97+
List<String> rejected = toList("project1", "project3");
98+
w.filterAndAddIssues(issues, selected, rejected, null, null);
9999

100100
assertThat(w.getIssues()).extracting("project").containsExactlyInAnyOrder(null, "project2");
101101
}
102102

103-
private List<String> projects(String... s) {
103+
@Test
104+
public void issueWithSelectedUserList() {
105+
Issues issues = buildIssuesWithAssignees("johndoe", "pepe");
106+
List<String> selectedUsers = toList("pepe");
107+
w.filterAndAddIssues(issues, null, null, selectedUsers, null);
108+
109+
assertThat(w.getIssues()).hasSize(1);
110+
}
111+
112+
@Test
113+
public void issueWithAllSelectedUsersList() {
114+
Issues issues = buildIssuesWithAssignees("johndoe", "pepe");
115+
List<String> selectedUsers = toList("pepe", "johndoe");
116+
w.filterAndAddIssues(issues, null, null, selectedUsers, null);
117+
118+
assertThat(w.getIssues()).hasSize(2);
119+
}
120+
121+
private List<String> toList(String... s) {
104122
List<String> selected = new ArrayList<>();
105123
if(s != null) {
106124
selected.addAll(Arrays.asList(s));
@@ -118,4 +136,15 @@ private Issues buildIssuesWithProjects(String... projects) {
118136
}
119137
return issues;
120138
}
139+
140+
private Issues buildIssuesWithAssignees(String... assignees) {
141+
Issues issues = new Issues();
142+
issues.setIssues(new ArrayList<>());
143+
for (String assignee : assignees) {
144+
Issue i = new Issue();
145+
i.setAssignee(assignee);
146+
issues.getIssues().add(i);
147+
}
148+
return issues;
149+
}
121150
}

src/test/java/io/tpd/quboo/sonarplugin/settings/QubooPropertiesTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ public class QubooPropertiesTest {
1515
public void allPropertiesConfigured() {
1616
final List<PropertyDefinition> properties = QubooProperties.getProperties();
1717

18-
assertThat(properties.size()).isEqualTo(5);
18+
assertThat(properties.size()).isEqualTo(6);
1919
assertThat(properties.stream().map(PropertyDefinition::key).collect(Collectors.toList()))
2020
.containsExactly(QubooProperties.ACCESS_KEY, QubooProperties.SECRET_KEY, QubooProperties.TOKEN_KEY,
21-
QubooProperties.SELECTED_PROJECTS_KEY, QubooProperties.REJECTED_PROJECTS_KEY);
21+
QubooProperties.SELECTED_PROJECTS_KEY, QubooProperties.REJECTED_PROJECTS_KEY,
22+
QubooProperties.SELECTED_USERS_KEY);
2223
assertThat(properties.get(2).type()).isEqualTo(PropertyType.PASSWORD);
2324
}
2425
}

0 commit comments

Comments
 (0)