JiraCreateIssue-example

This script is used to simplify the process of creating a new Issue in Jira. You can specify custom fields using the `customFields` argument.

python · Atlassian Jira

Details

IDJiraCreateIssue-example
Languagepython
From Version5.0.0
Docker Imagedemisto/python3:3.12.13.10116658
Tagsjira example

README

This script is used to simplify the process of creating a new Issue in Jira.
You can specify custom fields using the customFields argument.

Script Data


Name Description
Script Type python3
Tags jira, example
Cortex XSOAR Version 5.0.0

Dependencies


This script uses the following commands and scripts.

  • jira-create-issue

Used In


This script is used in the following playbooks and scripts.

  • Indeni Demo

Inputs


Argument Name Description
summary Summary of the issue, a mandatory field
projectKey Project key to associate the issue
issueTypeName Choose issue type by name - e.g. Problem
issueTypeId Choose issue type by its numeric ID
projectName Project name to associate the issue
description Issue description
labels comma separated list of labels
priority priority name, e.g. High/Medium.
dueDate Due date for the issue, in format YYYY-MM-DD
assignee assignee name
reporter reporter name
parentIssueKey Parent issue key if you create a sub-task
parentIssueId Parent issue ID if you create a sub-task
customFields Comma-separated custom field keys and values to include in the created incident, e.g. `customfield_10101=foo,customfield_10102=bar`

Outputs


Path Description Type
Ticket.Id Id of ticket Unknown
Ticket.Key Key of ticket Unknown
import re
from datetime import datetime
from typing import Any

import demistomock as demisto  # noqa: F401
from CommonServerPython import *  # noqa: F401

"""
This script is used to simplify the process of creating a new Issue in Jira,
including using custom fields.
"""

INTEGRATION_COMMAND = "jira-create-issue"
DATE_FORMAT = "%Y-%m-%d"


def validate_date_field(date_str: str):
    """
    Private method to validate the date field is in expected format
    YYYY-MM-DD.

    Args:
        - `date_str` (`str`): The date field to validate
    """

    # This raises a ValueError when the parsing fails
    datetime.strptime(date_str, DATE_FORMAT)


def parse_custom_fields(custom_fields: list[str]) -> dict[str, Any]:
    """
    Parse the custom fields into a dictionary.
    The custom fields arrive as comma-separated values:
        `customfield_10101=foo,customfield_10102=bar`

    And are returned as a dict:
        `'customfield_10101': 'foo', 'customfield_10101': 'bar'`

    Args:
        - `custom_fields` (`List[str]`): List of custom fields.s

    Returns:
        - `Dict[str, Any]` representing the custom fields.
    """

    result: dict[str, Any] = {}
    regex = r"(customfield_\d{5,})={1}(\w+)"

    for custom_field in custom_fields:
        field_regex_match = re.search(regex, custom_field)

        if field_regex_match:
            field_key, field_value = re.findall(regex, custom_field)[0]

            if field_value.isnumeric() and not field_value.startswith("0"):
                field_value = int(field_value)  # type: ignore

            result[field_key] = field_value

    return result


def add_custom_fields(args: dict[str, Any], custom_fields: dict[str, Any]) -> dict[str, Any]:
    """
    Method to generate the payload representing the Jira issue custom fields and add it to the script arguments.

    Args:
        - `custom_fields` (`Dict[str, Any]`): A dicto of custom fields
    Returns:
        - A `Dict[str, Any]` with the Jira issue payload
    """

    args["issueJson"] = {}
    args["issueJson"]["fields"] = custom_fields

    return args


def main():  # pragma: no cover
    try:
        args = demisto.args()

        demisto.debug(f"Arguments provided: \n{args}")

        if "dueDate" in args:
            validate_date_field(args.get("dueDate"))

        if "customFields" in args:
            demisto.debug("Found customFields arguments. Attempting to parse them...")
            custom_fields = parse_custom_fields(argToList(args.get("customFields")))

            # supplied custom fields might not parse correctly
            if custom_fields:
                demisto.debug(f"Custom fields parsed: {custom_fields}. Removing 'customFields' argument...")

                # `jira-create-issue`` doesn't include `customFields` arg so we need to remove it and replace it with `issueJson`.
                del args["customFields"]
                demisto.debug("'customFields' removed. Adding custom field payload to the rest of the command arguments...")
                args = add_custom_fields(args, custom_fields)
                demisto.debug("Custom fields added to command arguments")

        demisto.debug(f"Executing {INTEGRATION_COMMAND} with arguments: \n{args}")
        create_issue_result = demisto.executeCommand(INTEGRATION_COMMAND, args)

        return_results(create_issue_result)
    except Exception as e:
        return_error(f"Failed to JiraCreateIssueExample command. Error: {e!s}")


if __name__ in ("__main__", "__builtin__", "builtins"):  # pragma: no cover
    main()