Hello All,
I am trying to achieve the following:
If User tries to Link the Unresolved Sub-task Issues to the Resolved Parent issues.
- It should throw an error i.e (Linking is disallowed as this parent is closed) and it should cancel the link.
- And, Add auto comment as same as above error.
- And, Email the assignee with same error.
Can anyone suggest me how to achieve this?
Note: This requirement is for Jira Data Center 8.20 version
I have tried with couple of listeners using Scriprunner. There are no errors in the scripts but no exact output as per my requirement.
Here are the 3 to 4 scripts which I tried:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
def issue = event.issue as Issue
def change = event.getChangeLog()?.getRelated("ChildChangeItem")?.find { it.field == "parent" }
// Check if the issue is a sub-task, not resolved, and parent is resolved
def changeItems = event?.getChangeLog()?.getRelated("ChildChangeItem")
def oldParentValue = changeItems?.find { it.field == "parent" }?.oldstring
def newParentValue = changeItems?.find { it.field == "parent" }?.newstring
// Check if the issue is a sub-task, not resolved, and parent is resolved
if (issue.issueType.name == "Sub-task" && !issue.resolution && oldParentValue != newParentValue) {
}
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def commentManager = ComponentAccessor.commentManager
def userManager = ComponentAccessor.userManager
def mailServerManager = ComponentAccessor.getComponent(MailServerManager)
def mailServer = mailServerManager.getDefaultSMTPMailServer()
// Add a comment to the person making the change
def commentBody = "This change is disallowed as the parent issue is resolved."
commentManager.create(issue, currentUser, commentBody, false)
// Email the Assignee (if present)
def assignee = issue.assignee
if (assignee) {
def assigneeEmail = userManager.getUserByKey(assignee.key)?.emailAddress
if (assigneeEmail) {
def email = new Email(assigneeEmail)
email.setSubject("Change Disallowed")
email.setBody("Dear ${assignee.displayName},\n\nThis change is disallowed as the parent issue is resolved.")
mailServer?.send(email)
}
}
-------------------------------------------------------------------------------------------------------------
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def issue = event.issue as MutableIssue
def subTask = issue
def parentIssue = issue.getParentObject()
def linkTypeName = "blocks" // Replace with the actual link type name
def linkType = issueLinkTypeManager.getIssueLinkTypes().find { it.name == linkTypeName }
def isSubTaskUnresolved = !subTask.resolution
if (parentIssue && isSubTaskUnresolved && linkType) {
def existingLinks = issueLinkManager.getIssueLinks(parentIssue.id)
existingLinks.each { existingLink ->
if (existingLink.issueLinkType == linkType.name) {
log.error("Cannot link an Unresolved sub-task to a Resolved parent.")
}
}
}
------------------------------------------------------------------------------------------------------------
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.status.Status
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
def issue = event.issue as MutableIssue
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def userManager = ComponentAccessor.userManager
def mailServerManager = ComponentAccessor.getComponent(MailServerManager)
def mailServer = mailServerManager.getDefaultSMTPMailServer()
// Check conditions for execution
if (issue.isSubTask() && issue.getStatus() && issue.getParentObject().resolutionDate != null) {
// Actions:
// 1. Add comment to user
issue.addComment("This change is disallowed: Linking unresolved sub-tasks to resolved parent issues is not permitted.")
// 2. Email assignee (if present)
def assignee = issue.assignee
if (assignee) {
def assigneeEmail = userManager.getUserByKey(assignee.key)?.emailAddress
if (assigneeEmail) {
def email = new Email(assigneeEmail)
email.setSubject("Change Disallowed")
email.setBody("Dear ${assignee.displayName},\n\nThis change is disallowed as the parent issue is resolved ${issue.key}")
mailServer?.send(email)
// Prevent further execution of the event
throw new RuntimeException("Linking is disallowed as this parent issue is closed")
}}}
-------------------------------------------------------------------------------------------------------------
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def issue = event.issue as MutableIssue
def subTask = issue
def parentIssue = issue.getParentObject()
def linkTypeName = "blocks" // Replace with the actual link type name
def linkType = issueLinkTypeManager.getIssueLinkTypes().find { it.name == linkTypeName }
def isSubTaskUnresolved = !subTask.resolution
if (parentIssue && isSubTaskUnresolved && linkType) {
def existingLinks = issueLinkManager.getIssueLinks(parentIssue.id)
existingLinks.each { existingLink ->
if (existingLink.issueLinkType == linkType.name)
if (parentIssue) {
throw new RuntimeException("Cannot link an Unresolved sub-task to a Resolved parent.")
}
}
}
Thanks,
Venu