Skip to content

Commit e03dc42

Browse files
author
Vladimir Kotal
committed
add API to get RepositoryInfo fields
fixes #2960
1 parent 73485e9 commit e03dc42

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

apiary.apib

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,13 @@ will be used for repository paths.
238238

239239
["sc-2","sc-1"]
240240

241-
## Repository type [/repositories/type{?repository}]
241+
## Repository type [/repositories/property/{field}{?repository}]
242242

243-
### return type of the repository [GET]
243+
### return the field value [GET]
244244

245245
The repository path is relative to source root.
246246

247-
In the response the repository type is separated from the repository path
248-
with a colon. If the type cannot be determined "N/A" is returned.
249-
250-
+ Response 200 (text/plain)
251-
+ Body
252-
253-
/opengrok-master:git
247+
+ Response 200 (application/json)
254248

255249
+ Parameters
256250
+ repository - repository path with native path separators (of the machine

opengrok-indexer/src/main/java/org/opengrok/indexer/history/RepositoryInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public boolean isWorking() {
193193
*
194194
* @param working is repository working
195195
*/
196-
public void setWorking(Boolean working) {
196+
public void setWorking(boolean working) {
197197
this.working = working;
198198
}
199199

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,45 @@
2424

2525
import org.opengrok.indexer.configuration.RuntimeEnvironment;
2626
import org.opengrok.indexer.history.RepositoryInfo;
27+
import org.opengrok.indexer.util.ClassUtil;
2728

2829
import javax.ws.rs.GET;
2930
import javax.ws.rs.Path;
31+
import javax.ws.rs.PathParam;
3032
import javax.ws.rs.Produces;
3133
import javax.ws.rs.QueryParam;
34+
import javax.ws.rs.WebApplicationException;
3235
import javax.ws.rs.core.MediaType;
36+
import javax.ws.rs.core.Response;
37+
import java.io.IOException;
3338

3439
@Path("/repositories")
3540
public class RepositoriesController {
3641

3742
private RuntimeEnvironment env = RuntimeEnvironment.getInstance();
3843

39-
@GET
40-
@Path("/type")
41-
@Produces(MediaType.TEXT_PLAIN)
42-
public String getType(@QueryParam("repository") final String repositoryPath) {
44+
private RepositoryInfo getRepositoryInfo(String repositoryPath) {
4345
for (RepositoryInfo ri : env.getRepositories()) {
4446
if (ri.getDirectoryNameRelative().equals(repositoryPath)) {
45-
return repositoryPath + ":" + ri.getType();
47+
return ri;
4648
}
4749
}
48-
return repositoryPath + ":N/A";
50+
51+
return null;
4952
}
5053

54+
@GET
55+
@Path("/property/{field}")
56+
@Produces(MediaType.APPLICATION_JSON)
57+
public Object get(@QueryParam("repository") final String repositoryPath, @PathParam("field") final String field)
58+
throws IOException {
59+
60+
RepositoryInfo ri = getRepositoryInfo(repositoryPath);
61+
if (ri == null) {
62+
throw new WebApplicationException("cannot find repository with path: " + repositoryPath,
63+
Response.Status.NOT_FOUND);
64+
}
65+
66+
return ClassUtil.getFieldValue(ri, field);
67+
}
5168
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ public void tearDown() throws Exception {
8888
repository.destroy();
8989
}
9090

91-
@Test
92-
public void testGetRepositoryTypeOfNonExistenRepository() throws Exception {
93-
assertEquals(Paths.get("/totally-nonexistent-repository").toString() + ":N/A",
94-
getRepoType(Paths.get("/totally-nonexistent-repository").toString()));
91+
@Test(expected = javax.ws.rs.NotFoundException.class)
92+
public void testGetRepositoryTypeOfNonExistentRepository() throws Exception {
93+
getRepoType(Paths.get("/totally-nonexistent-repository").toString());
9594
}
9695

9796
@Test
@@ -111,17 +110,17 @@ public void testGetRepositoryType() throws Exception {
111110
null, // subFiles - needed when refreshing history partially
112111
null); // repositories - needed when refreshing history partially
113112

114-
assertEquals(Paths.get("/mercurial").toString() + ":Mercurial",
113+
assertEquals("Mercurial",
115114
getRepoType(Paths.get("/mercurial").toString()));
116-
assertEquals(Paths.get("/mercurial/closed").toString() + ":Mercurial",
115+
assertEquals("Mercurial",
117116
getRepoType(Paths.get("/mercurial/closed").toString()));
118-
assertEquals(Paths.get("/git").toString() + ":git",
117+
assertEquals("git",
119118
getRepoType(Paths.get("/git").toString()));
120119
}
121120

122121
private String getRepoType(final String repository) {
123122
return target("repositories")
124-
.path("type")
123+
.path("property/type")
125124
.queryParam("repository", repository)
126125
.request()
127126
.get(String.class);

0 commit comments

Comments
 (0)