Search in sources :

Example 1 with ComplexIssueInputFieldValue

use of com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue in project cx-flow by checkmarx-ltd.

the class JiraService method mapCustomFields.

/**
 * Map custom JIRA fields to specific values (Custom Cx fields, Issue result
 * fields, static fields
 */
private void mapCustomFields(ScanRequest request, ScanResults.XIssue issue, IssueInputBuilder issueBuilder, boolean update) {
    BugTracker bugTracker = request.getBugTracker();
    log.debug("Handling custom field mappings");
    if (bugTracker.getFields() == null) {
        return;
    }
    String projectKey = bugTracker.getProjectKey();
    String issueTypeStr = bugTracker.getIssueType();
    for (com.checkmarx.flow.dto.Field f : bugTracker.getFields()) {
        String customField = getCustomFieldByName(projectKey, issueTypeStr, f.getJiraFieldName());
        String value = "";
        String fieldName;
        if (update && f.isSkipUpdate()) {
            log.debug("Skip update to field {}", f.getName());
            continue;
        }
        if (!ScanUtils.empty(customField)) {
            if (customField.equalsIgnoreCase("Labels")) {
                log.warn("Configuring the Labels parameter would affect issue tracking and might result in duplicate bug creation or bugs not closing or opening.");
            }
            /*cx | static | other - specific values that can be linked from scan request or the issue details*/
            String fieldType = f.getType();
            if (ScanUtils.empty(fieldType)) {
                log.warn("Field type not supplied for custom field: {}. Using 'result' by default.", customField);
                // use default = result
                fieldType = "result";
            }
            switch(fieldType) {
                case FlowConstants.MAIN_MDC_ENTRY:
                    log.debug("Checkmarx custom field {}", f.getName());
                    if (request.getCxFields() != null) {
                        log.debug("Checkmarx custom field");
                        value = request.getCxFields().get(f.getName());
                        log.debug("Cx Field value: {}", value);
                        if (ScanUtils.empty(value) && !ScanUtils.empty(f.getJiraDefaultValue())) {
                            value = f.getJiraDefaultValue();
                            log.debug("JIRA default Value is {}", value);
                        }
                    } else {
                        log.debug("No value found for {}", f.getName());
                        value = "";
                    }
                    break;
                case "sca-results":
                    if (issue.getScaDetails() == null) {
                        log.debug("Sca details not available");
                        break;
                    }
                    fieldName = f.getName();
                    switch(fieldName) {
                        case "package-name":
                            log.debug("package-name: {}", issue.getScaDetails().get(0).getVulnerabilityPackage().getId());
                            value = issue.getScaDetails().get(0).getVulnerabilityPackage().getId();
                            break;
                        case "current-version":
                            log.debug("current-version: {}", issue.getScaDetails().get(0).getVulnerabilityPackage().getVersion());
                            value = issue.getScaDetails().get(0).getVulnerabilityPackage().getVersion();
                            break;
                        case "fixed-version":
                            log.debug("fixed-version: {}", issue.getScaDetails().get(0).getFinding().getFixResolutionText());
                            value = issue.getScaDetails().get(0).getFinding().getFixResolutionText();
                            break;
                        case "newest-version":
                            log.debug(issue.getScaDetails().get(0).getVulnerabilityPackage().getNewestVersion());
                            value = issue.getScaDetails().get(0).getVulnerabilityPackage().getNewestVersion();
                            break;
                        case "locations":
                            List<String> locations = issue.getScaDetails().get(0).getVulnerabilityPackage().getLocations();
                            String location = null;
                            for (String l : locations) {
                                location = l + ",";
                            }
                            log.debug("locations: {}", location);
                            value = location.substring(0, location.length() - 1);
                            break;
                        case "dev-dependency":
                            log.debug("dev-dependency: {}", issue.getScaDetails().get(0).getVulnerabilityPackage().isDevelopment());
                            value = String.valueOf(issue.getScaDetails().get(0).getVulnerabilityPackage().isDevelopment()).toUpperCase();
                            break;
                        case "direct-dependency":
                            log.debug("direct-dependency: {}", issue.getScaDetails().get(0).getVulnerabilityPackage().isDirectDependency());
                            value = String.valueOf(issue.getScaDetails().get(0).getVulnerabilityPackage().isDirectDependency()).toUpperCase();
                            break;
                        case "risk-score":
                            log.debug("risk score: {}", issue.getScaDetails().get(0).getVulnerabilityPackage().getRiskScore());
                            value = String.valueOf(issue.getScaDetails().get(0).getVulnerabilityPackage().getRiskScore());
                            break;
                        case "outdated":
                            log.debug("outdated: {}", issue.getScaDetails().get(0).getVulnerabilityPackage().isOutdated());
                            value = String.valueOf(issue.getScaDetails().get(0).getVulnerabilityPackage().isOutdated()).toUpperCase();
                            break;
                        case "violates-policy":
                            log.debug("Violates-Policy: {}", issue.getScaDetails().get(0).getFinding().isViolatingPolicy());
                            value = String.valueOf(issue.getScaDetails().get(0).getFinding().isViolatingPolicy()).toUpperCase();
                    }
                    break;
                case "static":
                    log.debug("Static value {} - {}", f.getName(), f.getJiraDefaultValue());
                    value = f.getJiraDefaultValue();
                    break;
                default:
                    // result
                    fieldName = f.getName();
                    if (fieldName == null) {
                        log.warn("Field name not supplied for custom field: {}. Skipping.", customField);
                        /* there is no default, move on to the next field */
                        continue;
                    }
                    /*known values we can use*/
                    switch(fieldName) {
                        case "application":
                            log.debug("application: {}", request.getApplication());
                            value = request.getApplication();
                            break;
                        case "project":
                            log.debug("project: {}", request.getProject());
                            value = request.getProject();
                            break;
                        case "namespace":
                            log.debug("namespace: {}", request.getNamespace());
                            value = request.getNamespace();
                            break;
                        case "repo-name":
                            log.debug("repo-name: {}", request.getRepoName());
                            value = request.getRepoName();
                            break;
                        case "repo-url":
                            log.debug("repo-url: {}", request.getRepoUrl());
                            value = request.getRepoUrl();
                            break;
                        case "branch":
                            log.debug("branch: {}", request.getBranch());
                            value = request.getBranch();
                            break;
                        case "severity":
                            if (issue.getScaDetails() != null) {
                                log.debug("severity: {}", issue.getScaDetails().get(0).getFinding().getSeverity());
                                value = ScanUtils.toProperCase(String.valueOf(issue.getScaDetails().get(0).getFinding().getSeverity()));
                            } else {
                                log.debug("severity: {}", issue.getSeverity());
                                value = ScanUtils.toProperCase(issue.getSeverity());
                            }
                            break;
                        case "category":
                            log.debug("category: {}", issue.getVulnerability());
                            value = issue.getVulnerability();
                            break;
                        case "cwe":
                            log.debug("cwe: {}", issue.getCwe());
                            value = issue.getCwe();
                            break;
                        case "cve":
                            if (issue.getScaDetails() != null) {
                                log.debug("cve: {}", issue.getScaDetails().get(0).getFinding().getId());
                                value = issue.getScaDetails().get(0).getFinding().getId();
                            } else {
                                log.debug("cve: {}", issue.getCve());
                                value = issue.getCve();
                            }
                            break;
                        case "system-date":
                            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                            LocalDateTime now = LocalDateTime.now().plusDays(f.getOffset());
                            value = dtf.format(now);
                            log.debug("system date: {}", value);
                            break;
                        case "recommendation":
                            StringBuilder recommendation = new StringBuilder();
                            if (issue.getLink() != null && !issue.getLink().isEmpty()) {
                                recommendation.append("Checkmarx Link: ").append(issue.getLink()).append(HTMLHelper.CRLF);
                            }
                            if (!ScanUtils.anyEmpty(flowProperties.getMitreUrl(), issue.getCwe())) {
                                recommendation.append("Mitre Details: ").append(String.format(flowProperties.getMitreUrl(), issue.getCwe())).append(HTMLHelper.CRLF);
                            }
                            if (!ScanUtils.empty(flowProperties.getCodebashUrl())) {
                                recommendation.append("Training: ").append(issue.getAdditionalDetails().get(FlowConstants.CODE_BASHING_LESSON)).append(HTMLHelper.CRLF);
                            }
                            if (!ScanUtils.empty(flowProperties.getWikiUrl())) {
                                recommendation.append("Guidance: ").append(flowProperties.getWikiUrl()).append(HTMLHelper.CRLF);
                            }
                            value = recommendation.toString();
                            break;
                        case "loc":
                            value = "";
                            if (issue.getDetails() != null) {
                                List<Integer> lines = issue.getDetails().entrySet().stream().filter(x -> x.getKey() != null && x.getValue() != null && !x.getValue().isFalsePositive()).map(Map.Entry::getKey).collect(Collectors.toList());
                                if (!lines.isEmpty()) {
                                    Collections.sort(lines);
                                    value = StringUtils.join(lines, ",");
                                    log.debug("loc: {}", value);
                                }
                            }
                            break;
                        case "not-exploitable":
                            value = "";
                            List<Integer> fpLines;
                            if (issue.getDetails() != null) {
                                fpLines = issue.getDetails().entrySet().stream().filter(x -> x.getKey() != null && x.getValue() != null && x.getValue().isFalsePositive()).map(Map.Entry::getKey).collect(Collectors.toList());
                                if (!fpLines.isEmpty()) {
                                    Collections.sort(fpLines);
                                    value = StringUtils.join(fpLines, ",");
                                    log.debug("loc: {}", value);
                                }
                            }
                            break;
                        case "site":
                            log.debug("site: {}", request.getSite());
                            value = request.getSite();
                            break;
                        case "issue-link":
                            if (issue.getScaDetails() != null) {
                                log.debug("issue-link: {}", issue.getScaDetails().get(0).getVulnerabilityLink());
                                value = issue.getScaDetails().get(0).getVulnerabilityLink();
                            } else {
                                log.debug("issue-link: {}", issue.getLink());
                                value = issue.getLink();
                            }
                            break;
                        case "filename":
                            log.debug("filename: {}", issue.getFilename());
                            value = issue.getFilename();
                            break;
                        case "language":
                            log.debug("language: {}", issue.getLanguage());
                            value = issue.getLanguage();
                            break;
                        case "comment":
                            value = "";
                            StringBuilder comments = new StringBuilder();
                            String commentFmt = "[Line %s]: [%s]".concat(HTMLHelper.CRLF);
                            if (issue.getDetails() != null) {
                                issue.getDetails().entrySet().stream().filter(x -> x.getKey() != null && x.getValue() != null && x.getValue().getComment() != null && !x.getValue().getComment().isEmpty()).forEach(c -> comments.append(String.format(commentFmt, c.getKey(), c.getValue().getComment())));
                                value = comments.toString();
                            }
                            break;
                        default:
                            log.warn("field value for {} not found", f.getName());
                            value = "";
                    }
                    /*If the value is missing, check if a default value was specified*/
                    if (ScanUtils.empty(value)) {
                        log.debug("Value is empty, defaulting to configured default (if applicable)");
                        if (!ScanUtils.empty(f.getJiraDefaultValue())) {
                            value = f.getJiraDefaultValue();
                            log.debug("Default value is {}", value);
                        }
                    }
                    break;
            }
            /*Determine the expected custom field type within JIRA*/
            if (!ScanUtils.empty(value)) {
                String jiraFieldType = f.getJiraFieldType();
                if (ScanUtils.empty(jiraFieldType)) {
                    log.warn("JIRA field type not supplied for custom field: {}. Using 'text' by default.", f.getName());
                    // use default = text
                    jiraFieldType = "text";
                }
                List<String> list;
                switch(jiraFieldType) {
                    case SECURITY_FIELD_TYPE:
                        log.debug("Security field");
                        SecurityLevel securityLevel = getSecurityLevel(projectKey, issueTypeStr, value);
                        if (securityLevel != null) {
                            log.warn("JIRA Security level was not found: {}", value);
                            issueBuilder.setFieldValue(SECURITY_FIELD_TYPE, securityLevel);
                        }
                        break;
                    case "text":
                        log.debug("text field");
                        issueBuilder.setFieldValue(customField, value);
                        break;
                    case "component":
                        log.debug("component field");
                        issueBuilder.setComponentsNames(Collections.singletonList(value));
                        break;
                    case "label":
                        /*csv to array | replace space with _ */
                        log.debug("label field");
                        String[] l = StringUtils.split(value, ",");
                        list = new ArrayList<>();
                        for (String x : l) {
                            list.add(x.replaceAll("[^a-zA-Z0-9:\\-_]+", "_"));
                        }
                        if (!ScanUtils.empty(list)) {
                            issueBuilder.setFieldValue(customField, list);
                        }
                        break;
                    case "single-select":
                        log.debug("single select field");
                        issueBuilder.setFieldValue(customField, ComplexIssueInputFieldValue.with(VALUE_FIELD_TYPE, value));
                        break;
                    case "radio":
                        log.debug("radio field");
                        issueBuilder.setFieldValue(customField, ComplexIssueInputFieldValue.with(VALUE_FIELD_TYPE, value));
                        break;
                    case "multi-select":
                        log.debug("multi select field");
                        String[] selected = StringUtils.split(value, ",");
                        List<ComplexIssueInputFieldValue> fields = new ArrayList<>();
                        for (String s : selected) {
                            ComplexIssueInputFieldValue fieldValue = ComplexIssueInputFieldValue.with(VALUE_FIELD_TYPE, s.trim());
                            fields.add(fieldValue);
                        }
                        issueBuilder.setFieldValue(customField, fields);
                        break;
                    case "cascading-select":
                        log.debug("cascading select list field");
                        log.debug("cascading values {}", value);
                        addCascadingSelect(issueBuilder, f, customField, value);
                        break;
                    case "single-version-picker":
                        log.debug("single version picker");
                        issueBuilder.setFieldValue(customField, ComplexIssueInputFieldValue.with(NAME_FIELD_TYPE, value));
                        break;
                    case "multi-version-picker":
                        log.debug("multi version picker");
                        String[] selectedVersions = StringUtils.split(value, ",");
                        List<ComplexIssueInputFieldValue> versionList = new ArrayList<>();
                        for (String version : selectedVersions) {
                            ComplexIssueInputFieldValue fieldValue = ComplexIssueInputFieldValue.with(NAME_FIELD_TYPE, version.trim());
                            versionList.add(fieldValue);
                        }
                        issueBuilder.setFieldValue(customField, versionList);
                        break;
                    default:
                        log.warn("{} not a valid option for jira field type", f.getJiraFieldType());
                }
            }
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) ScanRequest(com.checkmarx.flow.dto.ScanRequest) java.util(java.util) org.springframework.http(org.springframework.http) CustomAsynchronousJiraRestClientFactory(com.atlassian.jira.rest.client.internal.async.CustomAsynchronousJiraRestClientFactory) ScanResults(com.checkmarx.sdk.dto.ScanResults) URISyntaxException(java.net.URISyntaxException) LocalDateTime(java.time.LocalDateTime) FlowProperties(com.checkmarx.flow.config.FlowProperties) StringUtils(org.apache.commons.lang3.StringUtils) HTMLHelper(com.checkmarx.flow.utils.HTMLHelper) ComplexIssueInputFieldValue(com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue) BugTracker(com.checkmarx.flow.dto.BugTracker) JSONObject(org.json.JSONObject) Service(org.springframework.stereotype.Service) com.atlassian.jira.rest.client.api(com.atlassian.jira.rest.client.api) Sca(com.checkmarx.sdk.dto.sca.Sca) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory) URI(java.net.URI) JiraClientException(com.checkmarx.flow.exception.JiraClientException) JiraClientRunTimeException(com.checkmarx.flow.exception.JiraClientRunTimeException) RestTemplate(org.springframework.web.client.RestTemplate) MapUtils(org.apache.commons.collections4.MapUtils) CliMode(com.checkmarx.flow.config.CliMode) JiraConstants(com.checkmarx.flow.constants.JiraConstants) com.atlassian.jira.rest.client.api.domain(com.atlassian.jira.rest.client.api.domain) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ScanDetails(com.checkmarx.flow.dto.ScanDetails) JiraTicketsReport(com.checkmarx.flow.dto.report.JiraTicketsReport) Promise(io.atlassian.util.concurrent.Promise) IssueInputBuilder(com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder) TransitionInput(com.atlassian.jira.rest.client.api.domain.input.TransitionInput) SCATicketingConstants(com.checkmarx.flow.constants.SCATicketingConstants) Collectors(java.util.stream.Collectors) JiraProperties(com.checkmarx.flow.config.JiraProperties) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Slf4j(lombok.extern.slf4j.Slf4j) MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) Paths(java.nio.file.Paths) ScanUtils(com.checkmarx.flow.utils.ScanUtils) DateTimeFormatter(java.time.format.DateTimeFormatter) FlowConstants(com.checkmarx.flow.constants.FlowConstants) PostConstruct(javax.annotation.PostConstruct) JSONArray(org.json.JSONArray) FieldInput(com.atlassian.jira.rest.client.api.domain.input.FieldInput) BugTracker(com.checkmarx.flow.dto.BugTracker) ComplexIssueInputFieldValue(com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue) DateTimeFormatter(java.time.format.DateTimeFormatter) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with ComplexIssueInputFieldValue

use of com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue in project opennms by OpenNMS.

the class FieldMapperRegistryTest method verifyCustomFieldMapping.

// Custom fields (one of each type)
@Test
public void verifyCustomFieldMapping() {
    // Date field
    Assert.assertEquals("2011-10-03", getValue(new FieldSchemaBuilder().withType("date").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:datepicker").build(), "2011-10-03"));
    // Date time field
    Assert.assertEquals("2011-10-19T10:29:29.908+1100", getValue(new FieldSchemaBuilder().withType("datetime").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:datetime").build(), "2011-10-19T10:29:29.908+1100"));
    // Number field
    Assert.assertEquals(3.1415, getValue(new FieldSchemaBuilder().withType("number").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:float").build(), "3.1415"));
    // Text field (Single line)
    Assert.assertEquals("I am a single text line", getValue(new FieldSchemaBuilder().withType("string").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:textfield").build(), "I am a single text line"));
    // Text field (Multi line)
    Assert.assertEquals("I am a \nmulti\nline\ntext", getValue(new FieldSchemaBuilder().withType("string").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:textarea").build(), "I am a \nmulti\nline\ntext"));
    // URL
    Assert.assertEquals("http://www.opennms.org", getValue(new FieldSchemaBuilder().withType("string").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:url").build(), "http://www.opennms.org"));
    // Labels
    Assert.assertEquals(Lists.newArrayList("label1", "label2", "label with space"), getValue(new FieldSchemaBuilder().withItems("string").withType("array").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:labels").build(), "label1,label2,label with space"));
    // Project picker
    Assert.assertEquals(ComplexIssueInputFieldValue.with("key", "NMS"), getValue(new FieldSchemaBuilder().withType("project").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:project").build(), "NMS"));
    // Radio button
    Assert.assertEquals(ComplexIssueInputFieldValue.with("value", "red"), getValue(new FieldSchemaBuilder().withType("string").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons").build(), "red"));
    // Single select
    Assert.assertEquals(ComplexIssueInputFieldValue.with("value", "blue"), getValue(new FieldSchemaBuilder().withType("string").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:select").build(), "blue"));
    // Multi select
    Assert.assertEquals(Lists.newArrayList(ComplexIssueInputFieldValue.with("value", "red"), ComplexIssueInputFieldValue.with("value", "blue"), ComplexIssueInputFieldValue.with("value", "item with space")), getValue(new FieldSchemaBuilder().withItems("string").withType("array").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:multiselect").build(), "red,blue,item with space"));
    // Checkboxes
    Assert.assertEquals(Lists.newArrayList(ComplexIssueInputFieldValue.with("value", "red"), ComplexIssueInputFieldValue.with("value", "item with space!")), getValue(new FieldSchemaBuilder().withItems("string").withType("array").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:multicheckboxes").build(), "red,item with space!"));
    // Cascading Select
    Assert.assertEquals(new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>builder().put("value", "parent value").put("child", ComplexIssueInputFieldValue.with("value", "child value")).build()), getValue(new FieldSchemaBuilder().withType("array").withItems("string").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:cascadingselect").build(), "parent value,child value"));
    // Group picker (single)
    Assert.assertEquals(ComplexIssueInputFieldValue.with("name", "jira-users"), getValue(new FieldSchemaBuilder().withType("group").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:grouppicker").build(), "jira-users"));
    // Group picker (multi)
    Assert.assertEquals(Lists.newArrayList(ComplexIssueInputFieldValue.with("name", "jira-users"), ComplexIssueInputFieldValue.with("name", "jira-developers"), ComplexIssueInputFieldValue.with("name", "jira-administrators")), getValue(new FieldSchemaBuilder().withType("array").withItems("group").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:multigrouppicker").build(), "jira-users,jira-developers,jira-administrators"));
    // Version picker (single)
    Assert.assertEquals(ComplexIssueInputFieldValue.with("name", "20.0.0"), getValue(new FieldSchemaBuilder().withType("version").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:version").build(), "20.0.0"));
    // Version picker (multiple)
    Assert.assertEquals(Lists.newArrayList(ComplexIssueInputFieldValue.with("name", "18.0.0"), ComplexIssueInputFieldValue.with("name", "18.0.1"), ComplexIssueInputFieldValue.with("name", "19.0.0")), getValue(new FieldSchemaBuilder().withType("array").withItems("version").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:multiversion").build(), "18.0.0,18.0.1,19.0.0"));
    // User picker (single)
    Assert.assertEquals(ComplexIssueInputFieldValue.with("name", "ulf"), getValue(new FieldSchemaBuilder().withType("user").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:userpicker").build(), "ulf"));
    // User picker (multiple)
    Assert.assertEquals(Lists.newArrayList(ComplexIssueInputFieldValue.with("name", "mvr"), ComplexIssueInputFieldValue.with("name", "ulf")), getValue(new FieldSchemaBuilder().withType("array").withItems("user").withCustom("com.atlassian.jira.plugin.system.customfieldtypes:multiuserpicker").build(), "mvr,ulf"));
}
Also used : ComplexIssueInputFieldValue(com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue) Test(org.junit.Test)

Example 3 with ComplexIssueInputFieldValue

use of com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue in project opennms by OpenNMS.

the class CascadingSelectFieldMapper method mapToFieldValue.

@Override
public Object mapToFieldValue(String fieldId, FieldSchema schema, String attributeValue) {
    if (!Strings.isNullOrEmpty(attributeValue)) {
        // split by ","
        final String[] split = attributeValue.split(",");
        if (split != null && split.length >= 1) {
            // we have at least one value
            final String optionKey = getOptionKey(fieldId, "value");
            final Map<String, Object> parentValueMap = new HashMap<>();
            // set first value
            parentValueMap.put(optionKey, split[0]);
            final ComplexIssueInputFieldValue parentValue = new ComplexIssueInputFieldValue(parentValueMap);
            if (split.length >= 2) {
                // if we have a 2nd value, set it as child of value 1
                parentValue.getValuesMap().put("child", ComplexIssueInputFieldValue.with(optionKey, split[1]));
            }
            return parentValue;
        }
    }
    return null;
}
Also used : ComplexIssueInputFieldValue(com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue) HashMap(java.util.HashMap)

Example 4 with ComplexIssueInputFieldValue

use of com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue in project seleniumRobot by bhecquet.

the class TestJiraConnector method testCreateIssueWithMinimalFields.

/**
 * Test issue creation without fields, components, screenshots, ...
 * @throws URISyntaxException
 */
@Test(groups = { "ut" })
public void testCreateIssueWithMinimalFields() throws URISyntaxException {
    ArgumentCaptor<IssueInput> issueArgument = ArgumentCaptor.forClass(IssueInput.class);
    JiraConnector jiraConnector = new JiraConnector("http://foo/bar", PROJECT_KEY, "user", "password", jiraOptions);
    JiraBean jiraBean = new JiraBean(null, "issue 1", "issue 1 descr", "P1", "Bug", null, null, null, null, new ArrayList<>(), null, new HashMap<>(), new ArrayList<>());
    jiraConnector.createIssue(jiraBean);
    verify(issueRestClient).createIssue(issueArgument.capture());
    // check issue has only mandatory fields
    IssueInput issueInput = ((IssueInput) issueArgument.getValue());
    Assert.assertEquals(issueInput.getField("summary").getValue(), "issue 1");
    Assert.assertEquals(issueInput.getField("description").getValue(), "issue 1 descr");
    Assert.assertEquals(((ComplexIssueInputFieldValue) (issueInput.getField("issuetype").getValue())).getValuesMap().get("id"), "1");
    Assert.assertEquals(ImmutableList.copyOf(((Iterable<ComplexIssueInputFieldValue>) (issueInput.getField("components").getValue()))).size(), 0);
    Assert.assertEquals(((ComplexIssueInputFieldValue) (issueInput.getField("project").getValue())).getValuesMap().get("key"), PROJECT_KEY);
    Assert.assertNull(issueInput.getField("reporter"));
    Assert.assertNull(issueInput.getField("assignee"));
    // check attachments have been added (screenshot + detailed results)
    verify(issueRestClient, never()).addAttachments(eq(new URI("http://foo/bar/i/1/attachments")), any(File.class));
}
Also used : IssueInput(com.atlassian.jira.rest.client.api.domain.input.IssueInput) ComplexIssueInputFieldValue(com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue) OptionalIterable(com.atlassian.jira.rest.client.api.OptionalIterable) JiraBean(com.seleniumtests.connectors.bugtracker.jira.JiraBean) JiraConnector(com.seleniumtests.connectors.bugtracker.jira.JiraConnector) URI(java.net.URI) File(java.io.File) Test(org.testng.annotations.Test) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with ComplexIssueInputFieldValue

use of com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue in project seleniumRobot by bhecquet.

the class TestJiraConnector method testCreateIssueWithMinimalFieldsOtherEmpty.

/**
 * Test issue creation without fields, components, screenshots, ...
 * @throws URISyntaxException
 */
@Test(groups = { "ut" })
public void testCreateIssueWithMinimalFieldsOtherEmpty() throws URISyntaxException {
    ArgumentCaptor<IssueInput> issueArgument = ArgumentCaptor.forClass(IssueInput.class);
    JiraConnector jiraConnector = new JiraConnector("http://foo/bar", PROJECT_KEY, "user", "password", jiraOptions);
    JiraBean jiraBean = new JiraBean(null, "issue 1", "issue 1 descr", "", "Bug", "", null, "", "", new ArrayList<>(), null, new HashMap<>(), new ArrayList<>());
    jiraConnector.createIssue(jiraBean);
    verify(issueRestClient).createIssue(issueArgument.capture());
    // check issue has only mandatory fields
    IssueInput issueInput = ((IssueInput) issueArgument.getValue());
    Assert.assertEquals(issueInput.getField("summary").getValue(), "issue 1");
    Assert.assertEquals(issueInput.getField("description").getValue(), "issue 1 descr");
    Assert.assertEquals(((ComplexIssueInputFieldValue) (issueInput.getField("issuetype").getValue())).getValuesMap().get("id"), "1");
    Assert.assertEquals(ImmutableList.copyOf(((Iterable<ComplexIssueInputFieldValue>) (issueInput.getField("components").getValue()))).size(), 0);
    Assert.assertEquals(((ComplexIssueInputFieldValue) (issueInput.getField("project").getValue())).getValuesMap().get("key"), PROJECT_KEY);
    Assert.assertNull(issueInput.getField("reporter"));
    Assert.assertNull(issueInput.getField("assignee"));
    Assert.assertNull(issueInput.getField("priority"));
    // check attachments have been added (screenshot + detailed results)
    verify(issueRestClient, never()).addAttachments(eq(new URI("http://foo/bar/i/1/attachments")), any(File.class));
}
Also used : IssueInput(com.atlassian.jira.rest.client.api.domain.input.IssueInput) ComplexIssueInputFieldValue(com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue) OptionalIterable(com.atlassian.jira.rest.client.api.OptionalIterable) JiraBean(com.seleniumtests.connectors.bugtracker.jira.JiraBean) JiraConnector(com.seleniumtests.connectors.bugtracker.jira.JiraConnector) URI(java.net.URI) File(java.io.File) Test(org.testng.annotations.Test) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ComplexIssueInputFieldValue (com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue)9 IssueInput (com.atlassian.jira.rest.client.api.domain.input.IssueInput)4 URI (java.net.URI)4 OptionalIterable (com.atlassian.jira.rest.client.api.OptionalIterable)3 FieldInput (com.atlassian.jira.rest.client.api.domain.input.FieldInput)3 IssueInputBuilder (com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder)3 MockitoTest (com.seleniumtests.MockitoTest)3 JiraBean (com.seleniumtests.connectors.bugtracker.jira.JiraBean)3 JiraConnector (com.seleniumtests.connectors.bugtracker.jira.JiraConnector)3 File (java.io.File)3 HashMap (java.util.HashMap)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 BugTracker (com.checkmarx.flow.dto.BugTracker)2 JiraClientException (com.checkmarx.flow.exception.JiraClientException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 JSONObject (org.json.JSONObject)2 com.atlassian.jira.rest.client.api (com.atlassian.jira.rest.client.api)1 RestClientException (com.atlassian.jira.rest.client.api.RestClientException)1 com.atlassian.jira.rest.client.api.domain (com.atlassian.jira.rest.client.api.domain)1