Bhavya-org / Scripts

0 stars 0 forks source link

Send email for ticket creation - RCM #46

Open sani-d opened 1 week ago

sani-d commented 1 week ago

def issueKey = issue.key // Fetch the issue details from the Jira REST API def result = get('/rest/api/2/issue/' + issueKey) .header('Content-Type', 'application/json') .asObject(Map)

// Ensure the request was successful
if (result.status == 200) {
    def issueDetails = result.body
    def summary = issueDetails.fields.summary
    def subtaskType = issueDetails.fields.issuetype.subtask
    def issueType = issueDetails.fields.issuetype.name
    def reporterId = issueDetails.fields.reporter?.accountId
    def customNumber = issueDetails.fields?.customfield_11347
    def customRuleName = issueDetails.fields?.customfield_11494

    logger.warn("Subtask type: ${subtaskType}, Issue type: ${issueType}, Reporter ID: ${reporterId}, Custom number: ${customNumber}, Custom rule name: ${customRuleName}")
    // Find the first comma in the summary
    def commaIndex = summary.indexOf(',')

    if (commaIndex != -1) {
        // Get the part after the first comma, trimming leading and trailing whitespace
        def summaryAfterComma = summary.substring(commaIndex + 1).trim()    

        // Check the conditions for sending the notification
        if ((issueType == "TM alert ") &&
        (!subtaskType) &&
        (reporterId == '5b51cdecce51f259edf233a3') && (customNumber == 4.0 || customNumber == 3.101 || customNumber == 3.102) ) {
            // 5d0cb4b1c6e0880bc03fee7c
            // Send the notification to a specific email address // ongoingmonitoringl1
            def notifyResponse = post("/rest/api/2/issue/${issueKey}/notify")
                .header("Content-Type", "application/json")
                .body([
                    "subject": summaryAfterComma + " Blocked Faster Payment",
                    "htmlBody": issueDetails.fields.description,
                    "to": [
                        "users": [
                            ["emailAddress": "mohd.farooquie@tide.co"]
                        ]
                    ]
                ])
                .asString()

        } else {
            logger.warn("Conditions for sending the notification were not met")
        }
    } else {
        // No comma in the summary
        logger.warn("Summary does not contain a comma")
    }
} else {
    // Log error details for failed requests
   logger.warn("Failed to fetch issue details: Status ${result.status} - ${result.body}")
}
Bhavya-ss commented 1 week ago

def issueKey = issue.key

// Fetch the issue details from the Jira REST API def result = get("/rest/api/2/issue/${issueKey}") .header("Content-Type", "application/json") .asObject(Map)

// Ensure the request was successful if (result.status == 200) { def issueDetails = result.body def summary = issueDetails.fields.summary def subtaskType = issueDetails.fields.issuetype.subtask def issueType = issueDetails.fields.issuetype.name def reporterId = issueDetails.fields.reporter?.accountId def customNumber = issueDetails.fields?.customfield_10097 // number field

logger.warn("Subtask type: ${subtaskType}, Issue type: ${issueType}, Reporter ID: ${reporterId}, Custom number: ${customNumber}")

// Find the first comma in the summary
def commaIndex = summary.indexOf(',')

if (commaIndex != -1) {
    // Get the part after the first comma, trimming leading and trailing whitespace
    def summaryAfterComma = summary.substring(commaIndex + 1).trim()

    // Check the conditions for sending the notification
    if (issueType == "Story" &&
        !subtaskType &&
        reporterId == '6305b88c958ffd78c6e0e59e' &&
        (customNumber == 4.0 || customNumber == 3.101 || customNumber == 3.102)) {

        // Attempt to send the notifications directly with email addresses

            def notifyResponse = post("/rest/api/2/issue/${issueKey}/notify")
            .header("Content-Type", "application/json")
            .body([
                subject: "${summaryAfterComma} Blocked Faster Payment",
                htmlBody: "issueDetails.fields.description,",
                to: [
                    users: [
                    [
                        accountId: '6305b88c958ffd78c6e0e59e'
                    ],
                    [
                        accountId: "63d0b9efa197e05f9dadb169"
                    ]
                    ]
                ]
            ])
                .asString()

        if (notifyResponse.status == 204) {
            logger.info("Notifications sent successfully")
        } else {
            logger.warn("Failed to send notifications: Status ${notifyResponse.status} - ${notifyResponse.body}")
        }
    } else {
        logger.warn("Conditions for sending the notifications were not met")
    }
} else {
    // No comma in the summary
    logger.warn("Summary does not contain a comma")
}

} else { // Log error details for failed requests logger.warn("Failed to fetch issue details: Status ${result.status} - ${result.body}") }