Search in sources :

Example 1 with Sources

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;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Sources(com.checkmarx.flow.dto.Sources) JSONObject(org.json.JSONObject)

Example 2 with 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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Sources(com.checkmarx.flow.dto.Sources) JSONObject(org.json.JSONObject) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory)

Example 3 with 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.");
    }
}
Also used : Sources(com.checkmarx.flow.dto.Sources)

Example 4 with Sources

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;
}
Also used : Sources(com.checkmarx.flow.dto.Sources)

Example 5 with 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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) Sources(com.checkmarx.flow.dto.Sources) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject)

Aggregations

Sources (com.checkmarx.flow.dto.Sources)11 JSONObject (org.json.JSONObject)3 Test (org.junit.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)3 ScanRequest (com.checkmarx.flow.dto.ScanRequest)2 HashMap (java.util.HashMap)2 HttpHeaders (org.springframework.http.HttpHeaders)2 IfProfileValue (org.springframework.test.annotation.IfProfileValue)2 FlowProperties (com.checkmarx.flow.config.FlowProperties)1 JiraProperties (com.checkmarx.flow.config.JiraProperties)1 CxProfile (com.checkmarx.flow.dto.CxProfile)1 CxProperties (com.checkmarx.sdk.config.CxProperties)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 IOException (java.io.IOException)1 JSONArray (org.json.JSONArray)1 DefaultUriBuilderFactory (org.springframework.web.util.DefaultUriBuilderFactory)1