A Post-Function to automatically link an issue type (for example a Story) to a specific Epic when the issue is being created. This will take effect only if the Epic Link field in that issue is left blank.
In the sample code below, an issue type is automatically linked to an Epic if the Epic Link is not added when the issue is being created.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
//This requires name of the Issue Type that is to be linked to the Epic
final def issueTypeName = '<ISSUE_TYPE_NAME>'
//This requires key of the Epic that will be linked the issues
final def epicIssueKey = '<EPIC_ISSUE_KEY>'
def epic = issueManager.getIssueByCurrentKey(epicIssueKey)
def epicLinkCustomField = customFieldManager.getCustomFieldObjects(issue).findByName('Epic Link')
if (issue.issueType.name != issueTypeName) {
return
}
if (!epicLinkCustomField.getValue(issue)) {
issue.setCustomFieldValue(epicLinkCustomField, epic)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
}