mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-15 09:46:16 +01:00
Enrich repository collection, too
This commit is contained in:
@@ -3,6 +3,8 @@ package sonia.scm.api.v2.resources;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import sonia.scm.plugin.Extension;
|
import sonia.scm.plugin.Extension;
|
||||||
|
import sonia.scm.repository.NamespaceAndName;
|
||||||
|
import sonia.scm.repository.RepositoryManager;
|
||||||
import sonia.scm.web.JsonEnricherBase;
|
import sonia.scm.web.JsonEnricherBase;
|
||||||
import sonia.scm.web.JsonEnricherContext;
|
import sonia.scm.web.JsonEnricherContext;
|
||||||
|
|
||||||
@@ -11,31 +13,43 @@ import javax.inject.Provider;
|
|||||||
|
|
||||||
import static java.util.Collections.singletonMap;
|
import static java.util.Collections.singletonMap;
|
||||||
import static sonia.scm.web.VndMediaType.REPOSITORY;
|
import static sonia.scm.web.VndMediaType.REPOSITORY;
|
||||||
|
import static sonia.scm.web.VndMediaType.REPOSITORY_COLLECTION;
|
||||||
|
|
||||||
@Extension
|
@Extension
|
||||||
public class GitRepositoryConfigEnricher extends JsonEnricherBase {
|
public class GitRepositoryConfigEnricher extends JsonEnricherBase {
|
||||||
|
|
||||||
private final Provider<ScmPathInfoStore> scmPathInfoStore;
|
private final Provider<ScmPathInfoStore> scmPathInfoStore;
|
||||||
|
private final RepositoryManager manager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public GitRepositoryConfigEnricher(Provider<ScmPathInfoStore> scmPathInfoStore, ObjectMapper objectMapper) {
|
public GitRepositoryConfigEnricher(Provider<ScmPathInfoStore> scmPathInfoStore, ObjectMapper objectMapper, RepositoryManager manager) {
|
||||||
super(objectMapper);
|
super(objectMapper);
|
||||||
this.scmPathInfoStore = scmPathInfoStore;
|
this.scmPathInfoStore = scmPathInfoStore;
|
||||||
|
this.manager = manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void enrich(JsonEnricherContext context) {
|
public void enrich(JsonEnricherContext context) {
|
||||||
if (resultHasMediaType(REPOSITORY, context)) {
|
if (resultHasMediaType(REPOSITORY, context)) {
|
||||||
JsonNode repositoryNode = context.getResponseEntity();
|
JsonNode repositoryNode = context.getResponseEntity();
|
||||||
String namespace = repositoryNode.get("namespace").asText();
|
enrichRepositoryNode(repositoryNode);
|
||||||
String name = repositoryNode.get("name").asText();
|
} else if (resultHasMediaType(REPOSITORY_COLLECTION, context)) {
|
||||||
|
JsonNode repositoryCollectionNode = context.getResponseEntity().get("_embedded").withArray("repositories");
|
||||||
|
repositoryCollectionNode.elements().forEachRemaining(this::enrichRepositoryNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String newPullRequest = new LinkBuilder(scmPathInfoStore.get().get(), GitConfigResource.class)
|
private void enrichRepositoryNode(JsonNode repositoryNode) {
|
||||||
|
String namespace = repositoryNode.get("namespace").asText();
|
||||||
|
String name = repositoryNode.get("name").asText();
|
||||||
|
|
||||||
|
if ("git".equals(manager.get(new NamespaceAndName(namespace, name)).getType())) {
|
||||||
|
String repositoryConfigLink = new LinkBuilder(scmPathInfoStore.get().get(), GitConfigResource.class)
|
||||||
.method("getRepositoryConfig")
|
.method("getRepositoryConfig")
|
||||||
.parameters(namespace, name)
|
.parameters(namespace, name)
|
||||||
.href();
|
.href();
|
||||||
|
|
||||||
JsonNode newPullRequestNode = createObject(singletonMap("href", value(newPullRequest)));
|
JsonNode newPullRequestNode = createObject(singletonMap("href", value(repositoryConfigLink)));
|
||||||
|
|
||||||
addPropertyNode(repositoryNode.get("_links"), "configuration", newPullRequestNode);
|
addPropertyNode(repositoryNode.get("_links"), "configuration", newPullRequestNode);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,152 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.google.common.io.Resources;
|
||||||
|
import com.google.inject.Provider;
|
||||||
|
import com.google.inject.util.Providers;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import sonia.scm.repository.NamespaceAndName;
|
||||||
|
import sonia.scm.repository.Repository;
|
||||||
|
import sonia.scm.repository.RepositoryManager;
|
||||||
|
import sonia.scm.repository.api.Command;
|
||||||
|
import sonia.scm.repository.api.RepositoryService;
|
||||||
|
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||||
|
import sonia.scm.web.JsonEnricherContext;
|
||||||
|
import sonia.scm.web.VndMediaType;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class GitRepositoryConfigEnricherTest {
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
private GitRepositoryConfigEnricher linkEnricher;
|
||||||
|
private JsonNode rootNode;
|
||||||
|
@Mock
|
||||||
|
private RepositoryManager manager;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void globalSetUp() {
|
||||||
|
ScmPathInfoStore pathInfoStore = new ScmPathInfoStore();
|
||||||
|
pathInfoStore.set(() -> URI.create("/"));
|
||||||
|
Provider<ScmPathInfoStore> pathInfoStoreProvider = Providers.of(pathInfoStore);
|
||||||
|
|
||||||
|
linkEnricher = new GitRepositoryConfigEnricher(pathInfoStoreProvider, objectMapper, manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
class ForSingleRepository {
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() throws IOException {
|
||||||
|
URL resource = Resources.getResource("sonia/scm/repository/repository-001.json");
|
||||||
|
rootNode = objectMapper.readTree(resource);
|
||||||
|
|
||||||
|
when(manager.get(new NamespaceAndName("scmadmin", "web-resources"))).thenReturn(new Repository("id", "git", "scmadmin", "web-resources"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldEnrichGitRepositories() {
|
||||||
|
JsonEnricherContext context = new JsonEnricherContext(
|
||||||
|
URI.create("/"),
|
||||||
|
MediaType.valueOf(VndMediaType.REPOSITORY),
|
||||||
|
rootNode
|
||||||
|
);
|
||||||
|
|
||||||
|
linkEnricher.enrich(context);
|
||||||
|
|
||||||
|
String configLink = context.getResponseEntity()
|
||||||
|
.get("_links")
|
||||||
|
.get("configuration")
|
||||||
|
.get("href")
|
||||||
|
.asText();
|
||||||
|
|
||||||
|
assertThat(configLink).isEqualTo("/v2/config/git/scmadmin/web-resources");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldNotEnrichOtherRepositories() {
|
||||||
|
when(manager.get(new NamespaceAndName("scmadmin", "web-resources"))).thenReturn(new Repository("id", "hg", "scmadmin", "web-resources"));
|
||||||
|
|
||||||
|
JsonEnricherContext context = new JsonEnricherContext(
|
||||||
|
URI.create("/"),
|
||||||
|
MediaType.valueOf(VndMediaType.REPOSITORY),
|
||||||
|
rootNode
|
||||||
|
);
|
||||||
|
|
||||||
|
linkEnricher.enrich(context);
|
||||||
|
|
||||||
|
JsonNode configLink = context.getResponseEntity()
|
||||||
|
.get("_links")
|
||||||
|
.get("configuration");
|
||||||
|
|
||||||
|
assertThat(configLink).isNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
class ForRepositoryCollection {
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() throws IOException {
|
||||||
|
URL resource = Resources.getResource("sonia/scm/repository/repository-collection-001.json");
|
||||||
|
rootNode = objectMapper.readTree(resource);
|
||||||
|
|
||||||
|
when(manager.get(new NamespaceAndName("scmadmin", "web-resources"))).thenReturn(new Repository("id", "git", "scmadmin", "web-resources"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldEnrichAllRepositories() {
|
||||||
|
JsonEnricherContext context = new JsonEnricherContext(
|
||||||
|
URI.create("/"),
|
||||||
|
MediaType.valueOf(VndMediaType.REPOSITORY_COLLECTION),
|
||||||
|
rootNode
|
||||||
|
);
|
||||||
|
|
||||||
|
linkEnricher.enrich(context);
|
||||||
|
|
||||||
|
context.getResponseEntity()
|
||||||
|
.get("_embedded")
|
||||||
|
.withArray("repositories")
|
||||||
|
.elements()
|
||||||
|
.forEachRemaining(node -> {
|
||||||
|
String configLink = node
|
||||||
|
.get("_links")
|
||||||
|
.get("configuration")
|
||||||
|
.get("href")
|
||||||
|
.asText();
|
||||||
|
|
||||||
|
assertThat(configLink).isEqualTo("/v2/config/git/scmadmin/web-resources");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldNotModifyObjectsWithUnsupportedMediaType() throws IOException {
|
||||||
|
URL resource = Resources.getResource("sonia/scm/repository/repository-001.json");
|
||||||
|
rootNode = objectMapper.readTree(resource);
|
||||||
|
JsonEnricherContext context = new JsonEnricherContext(
|
||||||
|
URI.create("/"),
|
||||||
|
MediaType.valueOf(VndMediaType.USER),
|
||||||
|
rootNode
|
||||||
|
);
|
||||||
|
|
||||||
|
linkEnricher.enrich(context);
|
||||||
|
|
||||||
|
boolean hasNewPullRequestLink = context.getResponseEntity()
|
||||||
|
.get("_links")
|
||||||
|
.has("configuration");
|
||||||
|
|
||||||
|
assertThat(hasNewPullRequestLink).isFalse();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
{
|
||||||
|
"creationDate": "2018-11-09T09:48:32.732Z",
|
||||||
|
"description": "Handling static webresources made easy",
|
||||||
|
"healthCheckFailures": [],
|
||||||
|
"lastModified": "2018-11-09T09:49:20.973Z",
|
||||||
|
"namespace": "scmadmin",
|
||||||
|
"name": "web-resources",
|
||||||
|
"archived": false,
|
||||||
|
"type": "git",
|
||||||
|
"_links": {
|
||||||
|
"self": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources"
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources"
|
||||||
|
},
|
||||||
|
"update": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources"
|
||||||
|
},
|
||||||
|
"permissions": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/permissions/"
|
||||||
|
},
|
||||||
|
"protocol": [
|
||||||
|
{
|
||||||
|
"href": "http://localhost:8081/scm/repo/scmadmin/web-resources",
|
||||||
|
"name": "http"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"tags": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/tags/"
|
||||||
|
},
|
||||||
|
"branches": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/branches/"
|
||||||
|
},
|
||||||
|
"changesets": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/changesets/"
|
||||||
|
},
|
||||||
|
"sources": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/sources/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
{
|
||||||
|
"page": 0,
|
||||||
|
"pageTotal": 1,
|
||||||
|
"_links": {
|
||||||
|
"self": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/?page=0&pageSize=10"
|
||||||
|
},
|
||||||
|
"first": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/?page=0&pageSize=10"
|
||||||
|
},
|
||||||
|
"last": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/?page=0&pageSize=10"
|
||||||
|
},
|
||||||
|
"create": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"_embedded": {
|
||||||
|
"repositories": [
|
||||||
|
{
|
||||||
|
"creationDate": "2018-11-09T09:48:32.732Z",
|
||||||
|
"description": "Handling static webresources made easy",
|
||||||
|
"healthCheckFailures": [],
|
||||||
|
"lastModified": "2018-11-09T09:49:20.973Z",
|
||||||
|
"namespace": "scmadmin",
|
||||||
|
"name": "web-resources",
|
||||||
|
"archived": false,
|
||||||
|
"type": "git",
|
||||||
|
"_links": {
|
||||||
|
"self": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources"
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources"
|
||||||
|
},
|
||||||
|
"update": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources"
|
||||||
|
},
|
||||||
|
"permissions": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/permissions/"
|
||||||
|
},
|
||||||
|
"protocol": [
|
||||||
|
{
|
||||||
|
"href": "http://localhost:8081/scm/repo/scmadmin/web-resources",
|
||||||
|
"name": "http"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"tags": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/tags/"
|
||||||
|
},
|
||||||
|
"branches": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/branches/"
|
||||||
|
},
|
||||||
|
"changesets": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/changesets/"
|
||||||
|
},
|
||||||
|
"sources": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/sources/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"creationDate": "2018-11-09T09:48:32.732Z",
|
||||||
|
"description": "Handling static webresources made easy",
|
||||||
|
"healthCheckFailures": [],
|
||||||
|
"lastModified": "2018-11-09T09:49:20.973Z",
|
||||||
|
"namespace": "scmadmin",
|
||||||
|
"name": "web-resources",
|
||||||
|
"archived": false,
|
||||||
|
"type": "git",
|
||||||
|
"_links": {
|
||||||
|
"self": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources"
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources"
|
||||||
|
},
|
||||||
|
"update": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources"
|
||||||
|
},
|
||||||
|
"permissions": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/permissions/"
|
||||||
|
},
|
||||||
|
"protocol": [
|
||||||
|
{
|
||||||
|
"href": "http://localhost:8081/scm/repo/scmadmin/web-resources",
|
||||||
|
"name": "http"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"tags": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/tags/"
|
||||||
|
},
|
||||||
|
"branches": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/branches/"
|
||||||
|
},
|
||||||
|
"changesets": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/changesets/"
|
||||||
|
},
|
||||||
|
"sources": {
|
||||||
|
"href": "http://localhost:8081/scm/api/v2/repositories/scmadmin/web-resources/sources/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user