mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-01-01 13:19:53 +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.ObjectMapper;
|
||||
import sonia.scm.plugin.Extension;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.web.JsonEnricherBase;
|
||||
import sonia.scm.web.JsonEnricherContext;
|
||||
|
||||
@@ -11,31 +13,43 @@ import javax.inject.Provider;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static sonia.scm.web.VndMediaType.REPOSITORY;
|
||||
import static sonia.scm.web.VndMediaType.REPOSITORY_COLLECTION;
|
||||
|
||||
@Extension
|
||||
public class GitRepositoryConfigEnricher extends JsonEnricherBase {
|
||||
|
||||
private final Provider<ScmPathInfoStore> scmPathInfoStore;
|
||||
private final RepositoryManager manager;
|
||||
|
||||
@Inject
|
||||
public GitRepositoryConfigEnricher(Provider<ScmPathInfoStore> scmPathInfoStore, ObjectMapper objectMapper) {
|
||||
public GitRepositoryConfigEnricher(Provider<ScmPathInfoStore> scmPathInfoStore, ObjectMapper objectMapper, RepositoryManager manager) {
|
||||
super(objectMapper);
|
||||
this.scmPathInfoStore = scmPathInfoStore;
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enrich(JsonEnricherContext context) {
|
||||
if (resultHasMediaType(REPOSITORY, context)) {
|
||||
JsonNode repositoryNode = context.getResponseEntity();
|
||||
String namespace = repositoryNode.get("namespace").asText();
|
||||
String name = repositoryNode.get("name").asText();
|
||||
enrichRepositoryNode(repositoryNode);
|
||||
} 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")
|
||||
.parameters(namespace, name)
|
||||
.href();
|
||||
|
||||
JsonNode newPullRequestNode = createObject(singletonMap("href", value(newPullRequest)));
|
||||
JsonNode newPullRequestNode = createObject(singletonMap("href", value(repositoryConfigLink)));
|
||||
|
||||
addPropertyNode(repositoryNode.get("_links"), "configuration", newPullRequestNode);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user