Skip to content

Commit e1208d8

Browse files
committed
add group match endpoint
1 parent 216d94f commit e1208d8

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

apiary.apib

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,23 @@ This includes also projects of any sub-groups.
325325

326326
^foo-.*
327327

328+
## Group match [/groups/{group}/match]
329+
330+
### check if (project) name matches group pattern [POST]
331+
332+
+ Request (text/plain)
333+
+ Body
334+
335+
name of the project
336+
337+
+ Response 200
338+
339+
match
340+
341+
+ Response 204
342+
343+
no match
344+
328345
## Projects [/projects]
329346

330347
### returns a list of all projects [GET]

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Group.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,20 @@ public void setFlag(int flag) {
234234
* Test group for a match.
235235
*
236236
* @param p project
237-
* @return true if project's description matches the group pattern
237+
* @return true if project's name matches the group pattern
238238
*/
239239
public boolean match(Project p) {
240-
return compiledPattern.matcher(p.getName()).matches();
240+
return match(p.getName());
241+
}
242+
243+
/**
244+
* Test group for a match.
245+
*
246+
* @param name string to match
247+
* @return true if given name matches the group pattern
248+
*/
249+
public boolean match(String name) {
250+
return compiledPattern.matcher(name).matches();
241251
}
242252

243253
@Override

opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/GroupsController.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
*/
2323
package org.opengrok.web.api.v1.controller;
2424

25+
import jakarta.ws.rs.Consumes;
2526
import jakarta.ws.rs.GET;
27+
import jakarta.ws.rs.POST;
2628
import jakarta.ws.rs.Path;
2729
import jakarta.ws.rs.PathParam;
2830
import jakarta.ws.rs.Produces;
@@ -58,6 +60,7 @@ public List<String> listGroups() {
5860
@Path("/{group}/allprojects")
5961
@Produces(MediaType.APPLICATION_JSON)
6062
public Response getAllProjectsForGroup(@PathParam("group") String groupName) {
63+
// Avoid classification as a taint bug.
6164
groupName = Laundromat.launderInput(groupName);
6265
Group group;
6366
group = Group.getByName(groupName);
@@ -78,6 +81,7 @@ public Response getAllProjectsForGroup(@PathParam("group") String groupName) {
7881
@Path("/{group}/pattern")
7982
@Produces(MediaType.TEXT_PLAIN)
8083
public Response getPattern(@PathParam("group") String groupName) {
84+
// Avoid classification as a taint bug.
8185
groupName = Laundromat.launderInput(groupName);
8286
Group group;
8387
group = Group.getByName(groupName);
@@ -87,4 +91,26 @@ public Response getPattern(@PathParam("group") String groupName) {
8791

8892
return Response.ok().entity(group.getPattern()).build();
8993
}
94+
95+
@POST
96+
@Consumes(MediaType.TEXT_PLAIN)
97+
@Path("/{group}/match")
98+
public Response matchProject(@PathParam("group") String groupName, String projectNameParam) {
99+
// Avoid classification as a taint bug.
100+
final String projectName = Laundromat.launderInput(projectNameParam);
101+
102+
// Avoid classification as a taint bug.
103+
groupName = Laundromat.launderInput(groupName);
104+
Group group;
105+
group = Group.getByName(groupName);
106+
if (group == null) {
107+
return Response.status(Response.Status.NOT_FOUND).build();
108+
}
109+
110+
if (group.match(projectName)) {
111+
return Response.ok().build();
112+
} else {
113+
return Response.status(Response.Status.NO_CONTENT).build();
114+
}
115+
}
90116
}

opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/GroupsControllerTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
package org.opengrok.web.api.v1.controller;
2424

2525
import jakarta.ws.rs.NotFoundException;
26+
import jakarta.ws.rs.client.Entity;
2627
import jakarta.ws.rs.core.Application;
2728
import jakarta.ws.rs.core.GenericType;
29+
import jakarta.ws.rs.core.Response;
2830
import org.glassfish.jersey.server.ResourceConfig;
2931
import org.junit.jupiter.api.Test;
3032
import org.opengrok.indexer.configuration.Group;
@@ -154,6 +156,22 @@ void testGetAllProjects() {
154156
groupsResult);
155157
}
156158

159+
@Test
160+
void testGetPatternNoGroup() {
161+
String groupName = "group-pattern";
162+
final String groupPattern = "^project-.*";
163+
setGroup(groupName, groupPattern);
164+
165+
GenericType<String> type = new GenericType<>() {
166+
};
167+
168+
assertThrows(NotFoundException.class, () -> target("groups")
169+
.path(groupName + "1")
170+
.path("pattern")
171+
.request()
172+
.get(type));
173+
}
174+
157175
@Test
158176
void testGetPattern() {
159177
String groupName = "group-pattern";
@@ -171,4 +189,46 @@ void testGetPattern() {
171189
assertNotNull(pattern);
172190
assertEquals(groupPattern, pattern);
173191
}
192+
193+
@Test
194+
void testMatchNoGroup() {
195+
String groupName = "group-pattern-positive";
196+
final String groupPattern = "^project-.*";
197+
setGroup(groupName, groupPattern);
198+
199+
Response response = target("groups")
200+
.path(groupName + "bar")
201+
.path("match")
202+
.request()
203+
.post(Entity.text("project-foo"));
204+
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
205+
}
206+
207+
@Test
208+
void testMatchPositive() {
209+
String groupName = "group-pattern-positive";
210+
final String groupPattern = "^project-.*";
211+
setGroup(groupName, groupPattern);
212+
213+
Response response = target("groups")
214+
.path(groupName)
215+
.path("match")
216+
.request()
217+
.post(Entity.text("project-foo"));
218+
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
219+
}
220+
221+
@Test
222+
void testMatchNegative() {
223+
String groupName = "group-pattern-negative";
224+
final String groupPattern = "^project-.*";
225+
setGroup(groupName, groupPattern);
226+
227+
Response response = target("groups")
228+
.path(groupName)
229+
.path("match")
230+
.request()
231+
.post(Entity.text("proj"));
232+
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
233+
}
174234
}

0 commit comments

Comments
 (0)