Dynamic Forms Examples
Create an Issue
The following example shows the use of dynamic form annotations in a script which creates a new issue. A short text annotator is used for Project Key and Summary, select list annotator for Issue Type and Priority, and user picker for User.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.parameters.annotation.Select
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
import com.onresolve.scriptrunner.parameters.annotation.UserPicker
import com.onresolve.scriptrunner.parameters.annotation.meta.Option
@ShortTextInput(description = "Project key", label = "Project")
String projectKey
@Select(
description = "The issue type for the new issue",
label = "Issue type",
options =
[
@Option(label = "Bug", value = "Bug"),
@Option(label = "Story", value = "Story")
]
)
String issueTypeName
@Select(
description = "The priority of the new issue",
label = "Priority",
options =
[
@Option(label = "Major", value = "Major"),
@Option(label = "Minor", value = "Minor")
]
)
String priorityName
@UserPicker(label = "User", description = "User with that user key will be the reporter of the issue")
ApplicationUser user
def reporterKey = user.name
@ShortTextInput(description = "The summary for the new issue", label = "Summary")
String summary
def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)
assert project: "Could not find project with key $projectKey"
def issueType = constantsManager.allIssueTypeObjects.findByName(issueTypeName)
assert issueType: "Could not find issue type with name $issueTypeName"
// if we cannot find user with the specified key or this is null, then set as a reporter the logged in user
def reporter = ComponentAccessor.userManager.getUserByKey(reporterKey) ?: loggedInUser
// if we cannot find the priority with the given name or if this is null, then set the default priority
def priority = constantsManager.priorities.findByName(priorityName) ?: constantsManager.defaultPriority
def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(project.id)
setIssueTypeId(issueType.id)
setReporterId(reporter.key)
setSummary(summary)
setPriorityId(priority.id)
}
def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.isValid(): validationResult.errorCollection
def result = issueService.create(loggedInUser, validationResult)
assert result.isValid(): result.errorCollection
"New issue key: $result.issue.key"
Change Issue Priority
The following example shows the use of dynamic form annotations in a script that changes the priority of an issue. A short text annotator is used for the Issue Key field, select list annotator for Priority, and checkbox annotator for Send Email.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.onresolve.scriptrunner.parameters.annotation.Checkbox
import com.onresolve.scriptrunner.parameters.annotation.Select
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
import com.onresolve.scriptrunner.parameters.annotation.meta.Option
@ShortTextInput(description = "The key of the issue to be updated", label = "Issue Key")
String issueKey
@Select(
description = "The name of the priority to set",
label = "Priority",
options =
[
@Option(label = "High", value = "High"),
@Option(label = "Medium", value = "Medium"),
@Option(label = "Low", value = "Low")
]
)
String priorityName
@Checkbox(description = "Check to send notification email", label = "Send Email")
boolean sendMail
def issueService = ComponentAccessor.issueService
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueKey)
def availablePriorities = ComponentAccessor.constantsManager.priorities
def priority = availablePriorities.find { it.name == priorityName }
def issueInputParams = issueService.newIssueInputParameters()
issueInputParams.with {
priorityId = priority.id
}
def updateValidationResult = issueService.validateUpdate(loggedInUser, issue?.id, issueInputParams)
assert updateValidationResult.isValid(): updateValidationResult.errorCollection
def updateResult = issueService.update(loggedInUser, updateValidationResult, EventDispatchOption.ISSUE_UPDATED, sendMail)
"Issue priority changed to: $updateResult.issue.priority.name"