Bhavya-org / Scripts

0 stars 0 forks source link

request participant and notify #13

Open Bhavya-ss opened 4 weeks ago

Bhavya-ss commented 4 weeks ago

def issueKey = 'JP-105' def requestType = issue.fields.customfield_10010?.requestType?.name?.toString()

// Check conditions
if (requestType == 'Fix an account problem') {
    // Define the request participants
    def requestParticipants = [
        [
            "accountId": "6305b88c958ffd78c6e0e59e"
        ],
        [
            "accountId": "630a0d15ec02b5f28b63a024"
        ],
        [
            "accountId": "63d0b9efa197e05f9dadb169"
        ]
    ]

    // Update the request participants field
    def updateResult = put('/rest/api/2/issue/' + issueKey)
        .header('Content-Type', 'application/json')
        .body([
            fields: [
                customfield_10127: requestParticipants // Use the correct custom field ID
            ]
        ])
        .asString()

    // Check if the update was successful
    if (updateResult.status == 204) {
        // Notify the users
        def notifyResult = post("/rest/api/2/issue/${issueKey}/notify")
            .header("Content-Type", "application/json")
            .body([
                subject: "Assigned issue - ${issueKey}",
                textBody: "Body",
                htmlBody: "test",
                to: [
                    users: requestParticipants
                ]
            ])
            .asString()

        // Return the notification status
        return notifyResult.status == 204 ? "Request participants updated and notifications sent successfully." : "Request participants updated, but failed to send notifications: Status ${notifyResult.status} ${notifyResult.body}"
    } else {
        // Return an error message if the update fails
        return "Failed to update request participants: Status ${updateResult.status} ${updateResult.body}"
    }
}