Skip to content
This repository was archived by the owner on Feb 26, 2021. It is now read-only.

Commit feaa76e

Browse files
authored
Merge pull request #206 from secureCodeBox/fix/productName
2 parents 15c0253 + 9d37e68 commit feaa76e

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

scb-persistenceproviders/defectdojo-persistenceprovider/src/main/java/io/securecodebox/persistence/DefectDojoService.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,28 @@ public Long retrieveUserId(String username){
125125
}
126126
}
127127

128-
public long retrieveProductId(String product){
128+
public long retrieveProductId(String productName) {
129+
return retrieveProductId(productName, 0L);
130+
}
131+
public long retrieveProductId(String productName, long offset) {
132+
final long PAGE_SIZE = 50;
129133
RestTemplate restTemplate = new RestTemplate();
130134

131-
String uri = defectDojoUrl + "/api/v2/products/?name=" + product;
135+
String uri = defectDojoUrl + "/api/v2/products/?limit=" + String.valueOf(PAGE_SIZE) + "&offset=" + offset + "&name=" + productName;
132136
HttpEntity productRequest = new HttpEntity(getHeaders());
133137
ResponseEntity<DefectDojoResponse<DefectDojoProduct>> productResponse = restTemplate.exchange(uri, HttpMethod.GET, productRequest, new ParameterizedTypeReference<DefectDojoResponse<DefectDojoProduct>>(){});
134-
if(productResponse.getBody().getCount() == 1){
135-
return productResponse.getBody().getResults().get(0).getId();
138+
if(productResponse.getBody().getCount() >= 1) {
139+
// Since DefectDojo 1.8/1.9 name searches for inclusion and not match
140+
for(DefectDojoProduct product : productResponse.getBody().getResults()) {
141+
if(product.getName() != null && product.getName().equals(productName)) {
142+
return product.getId();
143+
}
144+
}
136145
}
137-
else {
138-
throw new DefectDojoProductNotFound(MessageFormat.format("Could not find product: \"{0}\" in DefectDojo", product));
146+
if(productResponse.getBody().getNext() != null) {
147+
return retrieveProductId(productName, offset + PAGE_SIZE);
139148
}
149+
throw new DefectDojoProductNotFound(MessageFormat.format("Could not find product: \"{0}\" in DefectDojo", productName));
140150
}
141151
private long retrieveOrCreateProduct(String productName, String productDescription, List<String> productTags, int productType) {
142152
long productId = 0;

scb-persistenceproviders/defectdojo-persistenceprovider/src/main/java/io/securecodebox/persistence/models/DefectDojoProduct.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ public DefectDojoProduct(String productName, String productDescription) {
2828
name = productName;
2929
description = productDescription;
3030
}
31+
public long getId() {
32+
return id;
33+
}
34+
public String getName() {
35+
return name;
36+
}
3137
}

0 commit comments

Comments
 (0)