Bhavya-org / Scripts

0 stars 0 forks source link

Send slack notification when an Immediate issue is open for more than 1 day #62

Open pramodhm112 opened 5 days ago

pramodhm112 commented 5 days ago

project = "Aha Support" and resolution is EMPTY and created <= -1d and priority = "Immediate"

slack msg : The following immediate issue has been unresolved for more than 1 day {{issue.url}}

Bhavya-ss commented 4 days ago

import groovy.json.JsonSlurper import groovy.json.JsonOutput import java.net.URLEncoder

def jqlQuery = 'project = "AM" AND resolution is EMPTY AND priority = "Medium" AND created <= -1d'

// URL encode the JQL query for use in the API request def encodedQuery = URLEncoder.encode(jqlQuery, 'UTF-8') def searchUrl = "/rest/api/3/search?jql=${encodedQuery}"

def searchResult = get(searchUrl).asObject(Map)

// Print the raw response for debugging println "Search Result Status: ${searchResult.status}" println "Search Result Body: ${searchResult.body}"

// Check if the search was successful and there are issues found if (searchResult.status == 200 && searchResult.body.containsKey('issues')) { def issues = searchResult.body.issues println "Issues Found: ${issues.size()}"

issues.each { issue ->
    def webhookUrl = 'https://hooks.slack.com/services/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'
    def msg = [
        text: """The following issue has been added to the current sprint: https://tideaccount.atlassian.net/browse/${issue.key}"""
    ]

    // Assuming you have a method to make an HTTP POST request
    def response = post(webhookUrl)
        .header('Content-Type', 'application/json')
        .body(JsonOutput.toJson(msg))
        .asString()

    println "Slack Response Status: ${response.status}"
    println "Slack Response Body: ${response.body}"
}

} else { println "No issues found or API request failed." }