Search in sources :

Example 6 with ComplexIssueInputFieldValue

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

the class TestJiraConnector method testCreateIssue.

/**
 * Test issue creation with all fields
 * @throws URISyntaxException
 */
@Test(groups = { "ut" })
public void testCreateIssue() throws URISyntaxException {
    ArgumentCaptor<IssueInput> issueArgument = ArgumentCaptor.forClass(IssueInput.class);
    ArgumentCaptor<File> screenshotCaptor = ArgumentCaptor.forClass(File.class);
    JiraConnector jiraConnector = new JiraConnector("http://foo/bar", PROJECT_KEY, "user", "password", jiraOptions);
    Map<String, String> fields = new HashMap<>();
    // field allowed by jira
    fields.put("application", "myApp");
    // field allowed by jira
    fields.put("step", "step2");
    JiraBean jiraBean = new JiraBean(null, "issue 1", "issue 1 descr", "P1", "Bug", "myTest", null, "me", "you", Arrays.asList(screenshot), detailedResult, fields, Arrays.asList("comp1"));
    jiraConnector.createIssue(jiraBean);
    Assert.assertEquals(jiraBean.getId(), "ISSUE-1");
    verify(issueRestClient).createIssue(issueArgument.capture());
    // check issue has all data defined in jiraBean
    IssueInput issueInput = ((IssueInput) issueArgument.getValue());
    Assert.assertEquals(issueInput.getField("summary").getValue(), "issue 1");
    Assert.assertEquals(((ComplexIssueInputFieldValue) (issueInput.getField("issuetype").getValue())).getValuesMap().get("id"), "1");
    Assert.assertEquals(ImmutableList.copyOf(((Iterable<ComplexIssueInputFieldValue>) (issueInput.getField("components").getValue()))).size(), 1);
    Assert.assertEquals(ImmutableList.copyOf(((Iterable<ComplexIssueInputFieldValue>) (issueInput.getField("components").getValue()))).get(0).getValuesMap().get("name"), "comp1");
    // Assert.assertEquals(issueInput.getField("duedate").getValue().toString().split("-")[0], Integer.toString(LocalDateTime.now().getYear()));
    Assert.assertEquals(((ComplexIssueInputFieldValue) (issueInput.getField("project").getValue())).getValuesMap().get("key"), PROJECT_KEY);
    Assert.assertEquals(issueInput.getField("description").getValue(), "issue 1 descr");
    Assert.assertEquals(((ComplexIssueInputFieldValue) (issueInput.getField("reporter").getValue())).getValuesMap().get("name"), "you");
    Assert.assertEquals(((ComplexIssueInputFieldValue) (issueInput.getField("assignee").getValue())).getValuesMap().get("name"), "user1");
    Assert.assertEquals(((ComplexIssueInputFieldValue) (issueInput.getField("priority").getValue())).getValuesMap().get("id"), "1");
    // custom field
    Assert.assertEquals(issueInput.getField("13").getValue(), "myApp");
    // custom field
    Assert.assertEquals(((ComplexIssueInputFieldValue) (issueInput.getField("33").getValue())).getValuesMap().get("value"), "step2");
    // check attachments have been added (screenshot + detailed results)
    verify(issueRestClient, times(2)).addAttachments(eq(new URI("http://foo/bar/i/1/attachments")), screenshotCaptor.capture());
    Assert.assertTrue(((File) screenshotCaptor.getAllValues().get(0)).getName().endsWith(".png"));
    Assert.assertTrue(((File) screenshotCaptor.getAllValues().get(1)).getName().endsWith(".zip"));
}
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) HashMap(java.util.HashMap) JiraBean(com.seleniumtests.connectors.bugtracker.jira.JiraBean) JiraConnector(com.seleniumtests.connectors.bugtracker.jira.JiraConnector) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) URI(java.net.URI) Test(org.testng.annotations.Test) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with ComplexIssueInputFieldValue

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

the class JiraService method addCascadingSelect.

private void addCascadingSelect(IssueInputBuilder issueBuilder, com.checkmarx.flow.dto.Field f, String customField, String value) {
    // expected value format is "parent;child"
    // neither can be empty; enclose in quotes if spaces/special characters
    // must match case
    String[] selectedValues = StringUtils.split(value, CASCADE_PARENT_CHILD_DELIMITER);
    if (selectedValues.length == 2) {
        Map<String, Object> cascadingValues = new HashMap<>();
        cascadingValues.put(VALUE_FIELD_TYPE, selectedValues[0].trim());
        cascadingValues.put(CHILD_FIELD_TYPE, ComplexIssueInputFieldValue.with(VALUE_FIELD_TYPE, selectedValues[1].trim()));
        issueBuilder.setFieldValue(customField, new ComplexIssueInputFieldValue(cascadingValues));
    } else {
        log.warn("Invalid value for jira field type {}", f.getJiraFieldType());
    }
}
Also used : ComplexIssueInputFieldValue(com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JSONObject(org.json.JSONObject)

Example 8 with ComplexIssueInputFieldValue

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

the class JiraService method createIssue.

public String createIssue(ScanResults.XIssue issue, ScanRequest request) throws JiraClientException {
    log.debug("Retrieving issuetype object for project {}, type {}", request.getBugTracker().getProjectKey(), request.getBugTracker().getIssueType());
    try {
        BugTracker bugTracker = request.getBugTracker();
        String assignee = bugTracker.getAssignee();
        String projectKey = bugTracker.getProjectKey();
        String application = request.getApplication();
        String namespace = request.getNamespace();
        String repoName = request.getRepoName();
        String branch = request.getBranch();
        String filename = issue.getFilename();
        String vulnerability = issue.getVulnerability();
        String severity = issue.getSeverity();
        IssueType issueType = this.getIssueType(projectKey, bugTracker.getIssueType());
        IssueInputBuilder issueBuilder = new IssueInputBuilder(projectKey, issueType.getId());
        String issuePrefix = jiraProperties.getIssuePrefix();
        String issuePostfix = jiraProperties.getIssuePostfix();
        if (issuePrefix == null) {
            issuePrefix = "";
        }
        if (issuePostfix == null) {
            issuePostfix = "";
        }
        String summary;
        boolean useBranch = isUseBranch(request);
        List<ScanResults.ScaDetails> scaDetails = issue.getScaDetails();
        if (scaDetails != null) {
            summary = ScanUtils.getScaSummaryIssueKey(request, issue, issuePrefix, issuePostfix);
        } else {
            if (useBranch) {
                summary = formatSastIssueSummary(jiraProperties.getSastIssueSummaryBranchFormat(), issue, request);
            } else {
                summary = formatSastIssueSummary(jiraProperties.getSastIssueSummaryFormat(), issue, request);
            }
        }
        String fileUrl = ScanUtils.getFileUrl(request, issue.getFilename());
        summary = checkSummaryLength(summary);
        issueBuilder.setSummary(HTMLHelper.getScanRequestIssueKeyWithDefaultProductValue(request, summary, jiraProperties.getLabelPrefix()));
        issueBuilder.setDescription(this.getBody(issue, request, fileUrl));
        if (assignee != null && !assignee.isEmpty()) {
            ComplexIssueInputFieldValue jiraAssignee = getAssignee(assignee, projectKey);
            if (jiraAssignee != null) {
                issueBuilder.setFieldInput(new FieldInput(IssueFieldId.ASSIGNEE_FIELD, jiraAssignee));
            }
        }
        String scannerTypeSeverity = getScannerTypeSeverity(issue, severity, scaDetails);
        if (bugTracker.getPriorities() != null && bugTracker.getPriorities().containsKey(scannerTypeSeverity)) {
            issueBuilder.setFieldValue(PRIORITY_FIELD_TYPE, ComplexIssueInputFieldValue.with("name", bugTracker.getPriorities().get(scannerTypeSeverity)));
        }
        /*Add labels for tracking existing issues*/
        List<String> labels = new ArrayList<>();
        if (useBranch) {
            labels.add(request.getProduct().getProduct());
            labels.add(jiraProperties.getOwnerLabelPrefix().concat(":").concat(namespace));
            labels.add(jiraProperties.getRepoLabelPrefix().concat(":").concat(repoName));
            labels.add(jiraProperties.getBranchLabelPrefix().concat(":").concat(branch));
        } else if (!ScanUtils.anyEmpty(application, repoName)) {
            labels.add(request.getProduct().getProduct());
            labels.add(jiraProperties.getAppLabelPrefix().concat(":").concat(application));
            labels.add(jiraProperties.getRepoLabelPrefix().concat(":").concat(repoName));
        } else if (!ScanUtils.empty(application)) {
            labels.add(request.getProduct().getProduct());
            labels.add(jiraProperties.getAppLabelPrefix().concat(":").concat(application));
        }
        if (null != scaDetails) {
            labels.add(JIRA_ISSUE_LABEL_SCA);
        } else {
            labels.add(JIRA_ISSUE_LABEL_SAST);
        }
        log.debug("Adding tracker labels: {} - {}", jiraProperties.getLabelTracker(), labels);
        if (!jiraProperties.getLabelTracker().equals(LABEL_FIELD_TYPE)) {
            String customField = getCustomFieldByName(projectKey, bugTracker.getIssueType(), jiraProperties.getLabelTracker());
            issueBuilder.setFieldValue(customField, labels);
        } else {
            issueBuilder.setFieldValue(LABEL_FIELD_TYPE, labels);
        }
        log.debug("Creating JIRA issue");
        mapCustomFields(request, issue, issueBuilder, false);
        log.debug("Creating JIRA issue");
        BasicIssue basicIssue = this.issueClient.createIssue(issueBuilder.build()).claim();
        log.debug("JIRA issue {} created", basicIssue.getKey());
        return basicIssue.getKey();
    } catch (RestClientException e) {
        log.error("Error occurred while creating JIRA issue.", e);
        throw new JiraClientException();
    }
}
Also used : JiraClientException(com.checkmarx.flow.exception.JiraClientException) 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) IssueInputBuilder(com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder)

Example 9 with ComplexIssueInputFieldValue

use of com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue in project jira-plugin by jenkinsci.

the class JiraRestService method createIssue.

public BasicIssue createIssue(String projectKey, String description, String assignee, Iterable<String> components, String summary, @NonNull Long issueTypeId, @Nullable Long priorityId) {
    IssueInputBuilder builder = new IssueInputBuilder();
    builder.setProjectKey(projectKey).setDescription(description).setIssueTypeId(issueTypeId).setSummary(summary);
    if (priorityId != null) {
        builder.setPriorityId(priorityId);
    }
    if (StringUtils.isNotBlank(assignee)) {
        final Map<String, Object> valuesMap = new HashMap<>(2);
        // server
        valuesMap.put("name", assignee);
        // cloud
        valuesMap.put("accountId", assignee);
        // Need to use "accountId" as specified here:
        // https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-user-privacy-api-migration-guide/
        // 
        // See upstream fix for setAssigneeName:
        // https://bitbucket.org/atlassian/jira-rest-java-client/pull-requests/104/change-field-name-from-name-to-id-for/diff
        builder.setFieldInput(new FieldInput(IssueFieldId.ASSIGNEE_FIELD, new ComplexIssueInputFieldValue(valuesMap)));
    }
    if (StreamSupport.stream(components.spliterator(), false).count() > 0) {
        builder.setComponentsNames(components);
    }
    final IssueInput issueInput = builder.build();
    try {
        return jiraRestClient.getIssueClient().createIssue(issueInput).get(timeout, TimeUnit.SECONDS);
    } catch (Exception e) {
        LOGGER.log(WARNING, "Jira REST createIssue error: " + e.getMessage(), e);
        return null;
    }
}
Also used : IssueInput(com.atlassian.jira.rest.client.api.domain.input.IssueInput) ComplexIssueInputFieldValue(com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue) HashMap(java.util.HashMap) IssueInputBuilder(com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder) FieldInput(com.atlassian.jira.rest.client.api.domain.input.FieldInput) TimeoutException(java.util.concurrent.TimeoutException) RestClientException(com.atlassian.jira.rest.client.api.RestClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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