Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vikrantyadav11/b05033818ed203e25b66ae441239400c to your computer and use it in GitHub Desktop.
Save vikrantyadav11/b05033818ed203e25b66ae441239400c to your computer and use it in GitHub Desktop.
Jira Cloud Scripted Listner to set Assignee based on Select List Field
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def eventIssue = Issues.getByKey(issue.key as String)
def issueKey = eventIssue.key
// Correctly extract the custom field value
def rawProductArea = eventIssue.getCustomFieldValue("Product Area")
def productAreaValue = rawProductArea instanceof Map ? rawProductArea.value : rawProductArea?.toString()
logger.info("Product Area = ${productAreaValue}")
def productAreaMap = [
"Marketing - Social Advertising": ["[email protected]"],
"Marketing - Governance & Workflow Automation": ["[email protected]"]
]
def mapping = productAreaMap[productAreaValue]
if (mapping) {
def assigneeEmail = mapping[0]
logger.info("Assignee Email = ${assigneeEmail}")
// Step 1: Get accountId from email
def assigneeResp = get("/rest/api/2/user/search")
.queryString("query", assigneeEmail)
.asString()
logger.info("Assignee API Response: ${assigneeResp.body}")
def parsed = (List) new JsonSlurper().parseText(assigneeResp.body)
if (parsed && parsed.size() > 0) {
def accountId = (parsed[0] as Map).accountId
logger.info("Assignee Account ID: ${accountId}")
// Step 2: Assign the issue using PUT /issue/{key}/assignee
def assignResp = put("/rest/api/3/issue/${issueKey}/assignee")
.header("Content-Type", "application/json")
.body(JsonOutput.toJson([accountId: accountId]))
.asString()
if (assignResp.status == 204) {
logger.info("✅ Successfully assigned issue to ${assigneeEmail}")
} else {
logger.error("❌ Failed to assign issue. Status: ${assignResp.status}, Body: ${assignResp.body}")
}
} else {
logger.warn("No user found for email: ${assigneeEmail}")
}
} else {
logger.warn("No mapping found for Product Area: ${productAreaValue}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment