Skip to content

Commit 1dd204d

Browse files
committed
NCL7800 Implement Build Artifact Graph REST API
1 parent 8140274 commit 1dd204d

File tree

6 files changed

+159
-1
lines changed

6 files changed

+159
-1
lines changed

facade/src/main/java/org/jboss/pnc/facade/providers/BuildProviderImpl.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.fasterxml.jackson.core.JsonProcessingException;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
2222
import org.jboss.pnc.auth.KeycloakServiceClient;
23+
import org.jboss.pnc.common.graph.VertexNeighbor;
24+
import org.jboss.pnc.common.graph.WeightedGraphBuilder;
2325
import org.jboss.pnc.common.scm.ScmException;
2426
import org.jboss.pnc.common.graph.GraphBuilder;
2527
import org.jboss.pnc.common.graph.GraphUtils;
@@ -77,6 +79,7 @@
7779
import org.jboss.pnc.spi.exception.MissingDataException;
7880
import org.jboss.pnc.spi.exception.RemoteRequestException;
7981
import org.jboss.pnc.spi.exception.ValidationException;
82+
import org.jboss.util.graph.Vertex;
8083
import org.slf4j.Logger;
8184
import org.slf4j.LoggerFactory;
8285

@@ -85,6 +88,7 @@
8588
import javax.ejb.EJBAccessException;
8689
import javax.ejb.Stateless;
8790
import javax.inject.Inject;
91+
import javax.persistence.Tuple;
8892
import java.math.BigInteger;
8993
import java.net.URI;
9094
import java.net.URISyntaxException;
@@ -747,6 +751,41 @@ public Page<BuildRecordInsights> getAllBuildRecordInsightsNewerThanTimestamp(
747751
return new Page<>(pageIndex, pageSize, totalPages, count, content);
748752
}
749753

754+
@Override
755+
public Graph<Build> getBuildArtifactDependencyGraph(String buildId, Integer depthLimit) {
756+
org.jboss.util.graph.Graph<Build> buildArtifactDependencyGraph = createBuildArtifactDependencyGraph(
757+
buildId,
758+
depthLimit);
759+
760+
return GraphDtoBuilder.from(buildArtifactDependencyGraph, Build.class, Vertex::getData);
761+
}
762+
763+
private org.jboss.util.graph.Graph<Build> createBuildArtifactDependencyGraph(String buildId, Integer depthLimit) {
764+
var graph = new org.jboss.util.graph.Graph<Build>();
765+
var graphBuilder = new WeightedGraphBuilder<Build, String>(this::getSpecific, node -> {
766+
var tuples = buildRecordRepository
767+
.getBuildsProducingArtifactDependencies(buildMapper.getIdMapper().toEntity(node.getId()));
768+
return parseVertexNeighborsFromTuples(tuples);
769+
}, node -> {
770+
var tuples = buildRecordRepository
771+
.getBuildsDependingOnProducedArtifacts(buildMapper.getIdMapper().toEntity(node.getId()));
772+
return parseVertexNeighborsFromTuples(tuples);
773+
});
774+
775+
graphBuilder.buildGraph(graph, buildId, depthLimit);
776+
return graph;
777+
}
778+
779+
private List<VertexNeighbor<String>> parseVertexNeighborsFromTuples(List<Tuple> tuples) {
780+
return tuples.stream()
781+
.map(
782+
tuple -> VertexNeighbor.<String> builder()
783+
.neighborId(tuple.get(0, Base32LongID.class).toString())
784+
.cost(tuple.get(1, Integer.class))
785+
.build())
786+
.collect(Collectors.toList());
787+
}
788+
750789
private DefaultPageInfo toPageInfo(BuildPageInfo buildPageInfo) {
751790
return new DefaultPageInfo(
752791
buildPageInfo.getPageIndex() * buildPageInfo.getPageSize(),

facade/src/main/java/org/jboss/pnc/facade/providers/api/BuildProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.jboss.pnc.dto.response.Page;
2626
import org.jboss.pnc.dto.response.RunningBuildCount;
2727
import org.jboss.pnc.dto.response.SSHCredentials;
28-
import org.jboss.pnc.enums.BuildStatus;
2928
import org.jboss.pnc.facade.validation.EmptyEntityException;
3029
import org.jboss.pnc.model.Base32LongID;
3130

@@ -116,4 +115,6 @@ Page<BuildRecordInsights> getAllBuildRecordInsightsNewerThanTimestamp(
116115
int pageIndex,
117116
int pageSize,
118117
Date lastupdatetime);
118+
119+
Graph<Build> getBuildArtifactDependencyGraph(String buildId, Integer depthLimit);
119120
}

rest-api/pnc-openapi.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,6 +3034,55 @@
30343034
}
30353035
}
30363036
},
3037+
"/builds/{id}/artifacts/dependency-graph" : {
3038+
"get" : {
3039+
"tags" : [ "Builds" ],
3040+
"summary" : "Finds Builds which produced Artifact dependencies and Builds which depend on produced Artifacts of a requested Build, and recursively so for those Builds to create a graph of build-time artifact dependencies. Maximum depth limit is 5.",
3041+
"operationId" : "getBuildArtifactDependencyGraph",
3042+
"parameters" : [ {
3043+
"name" : "id",
3044+
"in" : "path",
3045+
"description" : "ID of the build",
3046+
"required" : true,
3047+
"schema" : {
3048+
"type" : "string"
3049+
}
3050+
}, {
3051+
"name" : "depthLimit",
3052+
"in" : "query",
3053+
"schema" : {
3054+
"type" : "integer",
3055+
"format" : "int32",
3056+
"default" : 5
3057+
}
3058+
} ],
3059+
"responses" : {
3060+
"200" : {
3061+
"description" : "Success with results"
3062+
},
3063+
"400" : {
3064+
"description" : "Invalid input parameters or validation error",
3065+
"content" : {
3066+
"application/json" : {
3067+
"schema" : {
3068+
"$ref" : "#/components/schemas/ErrorResponse"
3069+
}
3070+
}
3071+
}
3072+
},
3073+
"500" : {
3074+
"description" : "Server error",
3075+
"content" : {
3076+
"application/json" : {
3077+
"schema" : {
3078+
"$ref" : "#/components/schemas/ErrorResponse"
3079+
}
3080+
}
3081+
}
3082+
}
3083+
}
3084+
}
3085+
},
30373086
"/builds/{id}/attributes" : {
30383087
"post" : {
30393088
"tags" : [ "Builds" ],

rest-api/pnc-openapi.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,43 @@ paths:
21702170
application/json:
21712171
schema:
21722172
$ref: '#/components/schemas/ErrorResponse'
2173+
/builds/{id}/artifacts/dependency-graph:
2174+
get:
2175+
tags:
2176+
- Builds
2177+
summary: "Finds Builds which produced Artifact dependencies and Builds which\
2178+
\ depend on produced Artifacts of a requested Build, and recursively so for\
2179+
\ those Builds to create a graph of build-time artifact dependencies. Maximum\
2180+
\ depth limit is 5."
2181+
operationId: getBuildArtifactDependencyGraph
2182+
parameters:
2183+
- name: id
2184+
in: path
2185+
description: ID of the build
2186+
required: true
2187+
schema:
2188+
type: string
2189+
- name: depthLimit
2190+
in: query
2191+
schema:
2192+
type: integer
2193+
format: int32
2194+
default: 5
2195+
responses:
2196+
"200":
2197+
description: Success with results
2198+
"400":
2199+
description: Invalid input parameters or validation error
2200+
content:
2201+
application/json:
2202+
schema:
2203+
$ref: '#/components/schemas/ErrorResponse'
2204+
"500":
2205+
description: Server error
2206+
content:
2207+
application/json:
2208+
schema:
2209+
$ref: '#/components/schemas/ErrorResponse'
21732210
/builds/{id}/attributes:
21742211
post:
21752212
tags:

rest-api/src/main/java/org/jboss/pnc/rest/api/endpoints/BuildEndpoint.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@
4848
import org.jboss.pnc.rest.configuration.SwaggerConstants;
4949

5050
import javax.validation.Valid;
51+
import javax.validation.constraints.Max;
52+
import javax.validation.constraints.Min;
5153
import javax.validation.constraints.NotNull;
5254
import javax.ws.rs.BeanParam;
5355
import javax.ws.rs.Consumes;
5456
import javax.ws.rs.DELETE;
57+
import javax.ws.rs.DefaultValue;
5558
import javax.ws.rs.GET;
5659
import javax.ws.rs.POST;
5760
import javax.ws.rs.PUT;
@@ -836,4 +839,29 @@ Page<BuildRecordInsights> getAllBuildRecordInsightsNewerThanTimestamp(
836839
@Parameter(description = SwaggerConstants.PAGE_INDEX_DESCRIPTION) @QueryParam("pageIndex") int pageIndex,
837840
@Parameter(description = TIMESTAMP_PARAM) @QueryParam("timestamp") long timestamp);
838841

842+
static final String GET_BUILD_ARTIFACT_DEPENDENCY_GRAPH = "Finds Builds which produced Artifact dependencies and Builds which depend on produced Artifacts of a requested Build, and recursively so for those Builds to create a graph of build-time artifact dependencies. Maximum depth limit is 5.";
843+
844+
/**
845+
* {@value GET_BUILD_ARTIFACT_DEPENDENCY_GRAPH}
846+
*
847+
* @param buildId
848+
* @param depthLimit
849+
* @return
850+
*/
851+
@Operation(
852+
summary = GET_BUILD_ARTIFACT_DEPENDENCY_GRAPH,
853+
responses = { @ApiResponse(responseCode = SUCCESS_CODE, description = SUCCESS_DESCRIPTION),
854+
@ApiResponse(
855+
responseCode = INVALID_CODE,
856+
description = INVALID_DESCRIPTION,
857+
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
858+
@ApiResponse(
859+
responseCode = SERVER_ERROR_CODE,
860+
description = SERVER_ERROR_DESCRIPTION,
861+
content = @Content(schema = @Schema(implementation = ErrorResponse.class))) })
862+
@GET
863+
@Path("/{id}/artifacts/dependency-graph")
864+
Graph<Build> getBuildArtifactDependencyGraph(
865+
@Parameter(description = B_ID) @PathParam("id") String buildId,
866+
@QueryParam("depthLimit") @Min(0) @Max(5) @DefaultValue("5") Integer depthLimit);
839867
}

rest/src/main/java/org/jboss/pnc/rest/endpoints/BuildEndpointImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,4 +446,8 @@ public Page<BuildRecordInsights> getAllBuildRecordInsightsNewerThanTimestamp(
446446
return provider.getAllBuildRecordInsightsNewerThanTimestamp(pageIndex, pageSize, new Date(timestamp));
447447
}
448448

449+
@Override
450+
public Graph<Build> getBuildArtifactDependencyGraph(String buildId, Integer depthLimit) {
451+
return provider.getBuildArtifactDependencyGraph(buildId, depthLimit);
452+
}
449453
}

0 commit comments

Comments
 (0)