use of com.checkmarx.flow.dto.Sources in project cx-flow by checkmarx-ltd.
the class GitLabService method getRepoLanguagePercentages.
private Sources getRepoLanguagePercentages(ScanRequest request) {
Sources sources = new Sources();
Map<String, Integer> langs = new HashMap<>();
HttpHeaders headers = createAuthHeaders(request);
try {
ResponseEntity<String> response = restTemplate.exchange(scmConfigOverrider.determineConfigApiUrl(properties, request).concat(LANGUAGE_TYPES), HttpMethod.GET, new HttpEntity(headers), String.class, request.getRepoProjectId());
if (response.getBody() == null) {
log.warn(HTTP_BODY_WARN_MESSAGE);
} else {
JSONObject json = new JSONObject(response.getBody());
Iterator<String> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next();
double bytes = json.getDouble(key);
langs.put(key, (int) Math.ceil(bytes));
}
sources.setLanguageStats(langs);
}
} catch (NullPointerException e) {
log.warn(CONTENT_NOT_FOUND_ERROR_MESSAGE, e);
} catch (HttpClientErrorException e) {
log.error(ERROR_OCCURRED, e);
}
return sources;
}
use of com.checkmarx.flow.dto.Sources in project cx-flow by checkmarx-ltd.
the class GitHubService method getRepoLanguagePercentages.
private Sources getRepoLanguagePercentages(ScanRequest request) {
// "/{namespace}/{repo}/languages"
Sources sources = new Sources();
Map<String, Long> langs = new HashMap<>();
Map<String, Integer> langsPercent = new HashMap<>();
HttpHeaders headers = createAuthHeaders(request);
String urlTemplate = scmConfigOverrider.determineConfigApiUrl(properties, request).concat(LANGUAGE_TYPES);
String url = new DefaultUriBuilderFactory().expand(urlTemplate, request.getNamespace(), request.getRepoName()).toString();
log.info("Getting repo languages from {}", url);
try {
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), String.class);
if (response.getBody() == null) {
log.warn(HTTP_BODY_IS_NULL);
} else {
JSONObject json = new JSONObject(response.getBody());
Iterator<String> keys = json.keys();
long total = 0L;
while (keys.hasNext()) {
String key = keys.next();
long bytes = json.getLong(key);
langs.put(key, bytes);
total += bytes;
}
for (Map.Entry<String, Long> entry : langs.entrySet()) {
Long bytes = entry.getValue();
double percentage = 0;
if (total != 0L) {
percentage = (Double.valueOf(bytes) / (double) total * 100);
}
langsPercent.put(entry.getKey(), (int) percentage);
}
sources.setLanguageStats(langsPercent);
}
} catch (NullPointerException e) {
log.warn(CONTENT_NOT_FOUND_IN_RESPONSE);
} catch (HttpClientErrorException.NotFound e) {
String error = "Got 404 'Not Found' error. GitHub endpoint: " + getGitHubEndPoint(request) + " is invalid.";
log.warn(error);
} catch (HttpClientErrorException e) {
log.error(ExceptionUtils.getRootCauseMessage(e));
}
return sources;
}
use of com.checkmarx.flow.dto.Sources in project cx-flow by checkmarx-ltd.
the class ScanRequestConverter method setPresetBasedOnSources.
private void setPresetBasedOnSources(ScanRequest request) {
log.debug("Setting scan preset based on the source repo.");
Sources sources = getRepoContent(request);
String preset = helperService.getPresetFromSources(sources);
if (!StringUtils.isEmpty(preset)) {
request.setScanPreset(preset);
} else {
log.warn("Unable to get preset from the source repo.");
}
}
use of com.checkmarx.flow.dto.Sources in project cx-flow by checkmarx-ltd.
the class ADOService method getRepoContent.
public Sources getRepoContent(ScanRequest request) {
log.debug("Auto profiling is enabled");
if (ScanUtils.anyEmpty(request.getNamespace(), request.getRepoName(), request.getBranch())) {
return null;
}
Sources sources = getRepoLanguagePercentages(request);
browseRepoEndpoint = getADOEndPoint(request);
scanGitContent(0, browseRepoEndpoint, sources, request);
return sources;
}
use of com.checkmarx.flow.dto.Sources in project cx-flow by checkmarx-ltd.
the class ADOService method getRepoLanguagePercentages.
private Sources getRepoLanguagePercentages(ScanRequest request) {
Sources sources = new Sources();
Map<String, Integer> languagePercent = new HashMap<>();
HttpHeaders headers = ADOUtils.createAuthHeaders(scmConfigOverrider.determineConfigToken(properties, request.getScmInstance()));
String projectUrl = request.getAdditionalMetadata(PROJECT_SELF_URL);
String urlTemplate = projectUrl.concat(LANGUAGE_METRICS);
log.info("Getting repo languages from {}", urlTemplate);
try {
ResponseEntity<String> response = restTemplate.exchange(urlTemplate, HttpMethod.GET, new HttpEntity<>(headers), String.class);
if (response.getBody() == null) {
log.warn(HTTP_RESPONSE_BODY_IS_NULL);
} else {
JSONObject jsonBody = new JSONObject(response.getBody());
JSONArray repoLanguageStats = jsonBody.getJSONArray("repositoryLanguageAnalytics");
for (Object repo : repoLanguageStats) {
String repoId = ((JSONObject) repo).getString("id");
if (repoId.equals(request.getAdditionalMetadata(REPO_ID))) {
JSONArray languageBreakdown = ((JSONObject) repo).getJSONArray("languageBreakdown");
for (Object language : languageBreakdown) {
String key = ((JSONObject) language).getString("name");
if (((JSONObject) language).has("languagePercentage")) {
Integer percentage = ((JSONObject) language).getInt("languagePercentage");
languagePercent.put(key, percentage);
}
}
}
}
sources.setLanguageStats(languagePercent);
}
} catch (NullPointerException e) {
log.warn(NO_CONTENT_FOUND_IN_RESPONSE);
} catch (HttpClientErrorException.NotFound e) {
String error = "Got 404 'Not Found' error. Azure endpoint: " + urlTemplate + " is invalid.";
log.warn(error);
} catch (HttpClientErrorException e) {
log.error(ExceptionUtils.getRootCauseMessage(e));
}
return sources;
}
Aggregations