Bhavya-org / Scripts

0 stars 0 forks source link

when label changes send slack plus edit slack #34

Open Bhavya-ss opened 3 weeks ago

Bhavya-ss commented 3 weeks ago

def issueKey = issue.key

for (item in changelog.items) { logger.info(item.field) if (item.field == "labels") { def labels = issue.fields.labels?.join(", ") println(labels)

    if (labels.contains("lvl2-business")) {
        def jiraResult = get("/rest/api/2/issue/${issueKey}")
            .header('Content-Type', 'application/json')
            .asObject(Map)

        if (jiraResult.status == 200) {
            def jiraFields = jiraResult.body.fields
            def webhookUrl = 'https://hooks.slack.com/services/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'

            def msg = [
                text: """${jiraFields.reporter.displayName} raised a ticket on PSO that we think should be handled by @bs2:

https://tideaccount.atlassian.net/servicedesk/customer/portal/20/${issueKey} - ${jiraFields.summary}""" ]

            def slackResponse = post(webhookUrl)
                .header('Content-Type', 'application/json')
                .body(msg)
                .asString()

            if (slackResponse.status >= 200 && slackResponse.status < 300) {
                // Add the 'slack_notified' label
                def updatedLabels = jiraFields.labels ?: []
                updatedLabels << 'slack_notified'

                def result = put("/rest/api/2/issue/${issueKey}")
                    .header('Content-Type', 'application/json')
                    .body([fields: [labels: updatedLabels]])
                    .asString()

                if (result.status == 204) {
                    println("Label 'slack_notified' added to the issue.")
                } else {
                    println("Failed to update issue labels: Status: ${result.status} ${result.body}")
                }
            } else {
                println("Failed to send Slack notification: Status: ${slackResponse.status} ${slackResponse.body}")
            }
        } else {
            println("Failed to find issue: Status: ${jiraResult.status} ${jiraResult.body}")
        }
    }
}

}