Search in sources :

Example 1 with CreateWorkItemAttr

use of com.checkmarx.flow.dto.azure.CreateWorkItemAttr in project cx-flow by checkmarx-ltd.

the class ADOService method endBlockMerge.

void endBlockMerge(ScanRequest request, ScanResults results, ScanDetails scanDetails) {
    if (properties.isBlockMerge()) {
        Integer projectId = Integer.parseInt(results.getProjectId());
        String url = request.getAdditionalMetadata("statuses_url");
        String statusId = request.getAdditionalMetadata("status_id");
        String threadUrl = null;
        if (request.getAdditionalMetadata("ado_thread_id") != null) {
            threadUrl = request.getMergeNoteUri().concat("/").concat(request.getAdditionalMetadata("ado_thread_id"));
        }
        if (statusId == null) {
            log.warn("No status Id found, skipping status update");
            return;
        }
        CreateWorkItemAttr item = new CreateWorkItemAttr();
        item.setOp("remove");
        item.setPath("/".concat(statusId));
        List<CreateWorkItemAttr> list = new ArrayList<>();
        list.add(item);
        HttpEntity<List<CreateWorkItemAttr>> httpEntity = new HttpEntity<>(list, ADOUtils.createPatchAuthHeaders(scmConfigOverrider.determineConfigToken(properties, request.getScmInstance())));
        if (ScanUtils.empty(url)) {
            log.error("statuses_url was not provided within the request object, which is required for blocking / unblocking pull requests");
            return;
        }
        // TODO remove preview once applicable
        log.info("Removing pending status from pull {}", url);
        restTemplate.exchange(getFullAdoApiUrl(url).concat("-preview"), HttpMethod.PATCH, httpEntity, Void.class);
        /*
                if the SAST server fails to scan a project it generates a result with ProjectId = -1
                This if statement adds a status of failed to the ADO PR, and sets the status of thread to
                CLOSED.
             */
        if (projectId == -1) {
            log.debug("SAST scan could not be processed due to some error. Creating status of failed to {}", url);
            createStatus("failed", "Checkmarx Scan could not be processed.", url, results.getLink(), request);
            if (threadUrl != null) {
                createThreadStatus(CLOSED, threadUrl, request);
            }
            return;
        }
        boolean isMergeAllowed = thresholdValidator.isMergeAllowed(results, properties, new PullRequestReport(scanDetails, request));
        if (!isMergeAllowed) {
            log.debug("Creating status of failed to {}", url);
            createStatus("failed", "Checkmarx Scan Completed", url, results.getLink(), request);
            if (threadUrl != null) {
                createThreadStatus(CLOSED, threadUrl, request);
            }
        } else {
            log.debug("Creating status of succeeded to {}", url);
            createStatus("succeeded", "Checkmarx Scan Completed", url, results.getLink(), request);
            if (threadUrl != null) {
                createThreadStatus(RESOLVED, threadUrl, request);
            }
        }
    }
}
Also used : PullRequestReport(com.checkmarx.flow.dto.report.PullRequestReport) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CreateWorkItemAttr(com.checkmarx.flow.dto.azure.CreateWorkItemAttr)

Example 2 with CreateWorkItemAttr

use of com.checkmarx.flow.dto.azure.CreateWorkItemAttr in project cx-flow by checkmarx-ltd.

the class ADOIssueTracker method updateIssue.

@Override
public Issue updateIssue(Issue issue, ScanResults.XIssue resultIssue, ScanRequest request) {
    log.debug("Executing update Azure API call");
    String endpoint = issue.getUrl().concat("?api-version=").concat(properties.getApiVersion());
    String issueBody = request.getAdditionalMetadata(Constants.ADO_ISSUE_BODY_KEY);
    String adoOpenedState = request.getAdditionalMetadata(Constants.ADO_OPENED_STATE_KEY);
    CreateWorkItemAttr state = new CreateWorkItemAttr();
    state.setOp("add");
    state.setPath(Constants.ADO_FIELD.concat(STATE_FIELD));
    state.setValue(adoOpenedState);
    CreateWorkItemAttr description = new CreateWorkItemAttr();
    description.setOp("add");
    description.setPath(Constants.ADO_FIELD.concat(FIELD_PREFIX.concat(issueBody)));
    description.setValue(HTMLHelper.getHTMLBody(resultIssue, request, flowProperties));
    List<CreateWorkItemAttr> body = new ArrayList<>(Arrays.asList(state, description));
    HttpEntity<List<CreateWorkItemAttr>> httpEntity = new HttpEntity<>(body, ADOUtils.createPatchAuthHeaders(scmConfigOverrider.determineConfigToken(properties, request.getScmInstance())));
    restTemplate.exchange(endpoint, HttpMethod.PATCH, httpEntity, String.class);
    return getIssue(issue.getUrl(), issueBody, request);
}
Also used : CreateWorkItemAttr(com.checkmarx.flow.dto.azure.CreateWorkItemAttr)

Example 3 with CreateWorkItemAttr

use of com.checkmarx.flow.dto.azure.CreateWorkItemAttr in project cx-flow by checkmarx-ltd.

the class ADOIssueTracker method closeIssue.

@Override
public void closeIssue(Issue issue, ScanRequest request) {
    log.debug("Executing closeIssue Azure API call");
    String endpoint = issue.getUrl().concat("?api-version=").concat(properties.getApiVersion());
    String adoClosedState = request.getAdditionalMetadata(Constants.ADO_CLOSED_STATE_KEY);
    CreateWorkItemAttr state = new CreateWorkItemAttr();
    state.setOp("add");
    state.setPath(Constants.ADO_FIELD.concat(STATE_FIELD));
    state.setValue(adoClosedState);
    List<CreateWorkItemAttr> body = new ArrayList<>(Collections.singletonList(state));
    HttpEntity<List<CreateWorkItemAttr>> httpEntity = new HttpEntity<>(body, ADOUtils.createPatchAuthHeaders(scmConfigOverrider.determineConfigToken(properties, request.getScmInstance())));
    restTemplate.exchange(endpoint, HttpMethod.PATCH, httpEntity, String.class);
}
Also used : CreateWorkItemAttr(com.checkmarx.flow.dto.azure.CreateWorkItemAttr)

Example 4 with CreateWorkItemAttr

use of com.checkmarx.flow.dto.azure.CreateWorkItemAttr in project cx-flow by checkmarx-ltd.

the class IssueRequestBuilder method getState.

private CreateWorkItemAttr getState(String stateValue) {
    CreateWorkItemAttr state = new CreateWorkItemAttr();
    state.setOp("add");
    state.setPath("/fields/System.State");
    state.setValue(stateValue);
    return state;
}
Also used : CreateWorkItemAttr(com.checkmarx.flow.dto.azure.CreateWorkItemAttr)

Example 5 with CreateWorkItemAttr

use of com.checkmarx.flow.dto.azure.CreateWorkItemAttr in project cx-flow by checkmarx-ltd.

the class ADOIssueTracker method createIssue.

@Override
public Issue createIssue(ScanResults.XIssue resultIssue, ScanRequest request) throws MachinaException {
    log.debug("Executing createIssue Azure API call");
    String issueBody = request.getAdditionalMetadata(Constants.ADO_ISSUE_BODY_KEY);
    String projectName = calculateProjectName(request);
    URI endpoint = getCreationEndpoint(projectName, request);
    /*Namespace/Repo/Branch provided*/
    StringBuilder tags = new StringBuilder();
    tags.append(request.getProduct().getProduct()).append("; ");
    if (!StringUtils.isEmpty(properties.getProjectName())) {
        if (!StringUtils.isEmpty(properties.getNamespace())) {
            tags = getNamespaceTag(properties.getNamespace()).append("; ");
        }
    } else if (!flowProperties.isTrackApplicationOnly() && !ScanUtils.empty(request.getNamespace()) && !ScanUtils.empty(request.getRepoName()) && !ScanUtils.empty(request.getBranch())) {
        tags.append(getNamespaceTag(request.getNamespace())).append("; ");
        tags.append(properties.getRepoTagPrefix()).append(":").append(request.getRepoName()).append("; ");
        tags.append(properties.getBranchLabelPrefix()).append(":").append(request.getBranch());
    } else /*Only application provided*/
    if (!ScanUtils.empty(request.getApplication())) {
        tags.append(properties.getAppTagPrefix()).append(":").append(request.getApplication());
    }
    log.debug("tags: {}", tags);
    CreateWorkItemAttr title = new CreateWorkItemAttr();
    title.setOp("add");
    title.setPath(Constants.ADO_FIELD.concat(TITLE_FIELD));
    title.setValue(HTMLHelper.getScanRequestIssueKeyWithDefaultProductValue(request, this, resultIssue));
    CreateWorkItemAttr description = new CreateWorkItemAttr();
    description.setOp("add");
    description.setPath(Constants.ADO_FIELD.concat(FIELD_PREFIX.concat(issueBody)));
    description.setValue(HTMLHelper.getHTMLBody(resultIssue, request, flowProperties));
    CreateWorkItemAttr tagsBlock = new CreateWorkItemAttr();
    tagsBlock.setOp("add");
    tagsBlock.setPath(Constants.ADO_FIELD.concat(TAGS_FIELD));
    tagsBlock.setValue(tags.toString());
    List<CreateWorkItemAttr> body = new ArrayList<>(Arrays.asList(title, description, tagsBlock));
    for (Map.Entry<String, String> map : request.getAltFields().entrySet()) {
        log.debug("Custom field: {},  value: {}", map.getKey(), map.getValue());
        CreateWorkItemAttr fieldBlock = new CreateWorkItemAttr();
        fieldBlock.setOp("add");
        fieldBlock.setPath(Constants.ADO_FIELD.concat(map.getKey()));
        fieldBlock.setValue(map.getValue());
        body.add(fieldBlock);
    }
    log.debug("Request body: {}", body);
    HttpEntity<List<CreateWorkItemAttr>> httpEntity = new HttpEntity<>(body, ADOUtils.createPatchAuthHeaders(scmConfigOverrider.determineConfigToken(properties, request.getScmInstance())));
    ResponseEntity<String> response = restTemplate.exchange(endpoint, HttpMethod.POST, httpEntity, String.class);
    try {
        String url = new JSONObject(response.getBody()).getJSONObject("_links").getJSONObject("self").getString("href");
        return getIssue(url, issueBody, request);
    } catch (NullPointerException e) {
        log.warn("Error occurred while retrieving new WorkItem url.  Returning null", e);
        return null;
    }
}
Also used : URI(java.net.URI) JSONObject(org.json.JSONObject) CreateWorkItemAttr(com.checkmarx.flow.dto.azure.CreateWorkItemAttr)

Aggregations

CreateWorkItemAttr (com.checkmarx.flow.dto.azure.CreateWorkItemAttr)9 PullRequestReport (com.checkmarx.flow.dto.report.PullRequestReport)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JSONObject (org.json.JSONObject)1 HttpEntity (org.springframework.http.HttpEntity)1