Hello -
I have hacked together the script below as a toy example of using the full functionality of script-runner - except - I don't know how to get it registered like the built in scripts from scriptrunner. Any help would be appreciated (I don't assume that everything in the script is currently correct - I am currently focused on deploy-time parameterization.
ackage com.onresolve.jira.groovy.CloseChildIssues
//these are the imports we need to do our functions.
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.comments.CommentManager
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
//these are the imports from onresolve that make life easier (allow us to structure nicely)
import com.onresolve.jira.groovy.canned.CannedScript
import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
import com.onresolve.jira.groovy.canned.utils.ConditionUtils
import com.onresolve.jira.groovy.canned.utils.WorkflowUtils
import com.opensymphony.workflow.loader.ActionDescriptor
import com.opensymphony.workflow.loader.StepDescriptor
class CloseChildISsues implements CannedScript{
//these are definitely not the fields we want to work with, but this is the fields section.
public static final String FIELD_SUBTASK_SUMMARY = 'FIELD_SUBTASK_SUMMARY'
public static final String FIELD_SUBTASK_ACTION = 'FIELD_SUBTASK_ACTION'
log = Category.getInstance("com.ezesoft.jira.groovy.AutoCloseChildIssues")
WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );
SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();
Collection subTasks = issue.getSubTaskObjects()
String getName() {
return "Close Sub-Tasks."
}
public String getHelpUrl() {
"http://ezenet"
}
String getDescription() {
return "Transition Sub-Tasks when Parent Task transitions"
}
List getCategories() {
["Function"]
}
//this is a helper method that can get the possible actions.
Integer getActionId(Issue issue, String actionName) {
JiraWorkflow workflow = componentManager.getWorkflowManager().getWorkflow(issue)
StepDescriptor step = workflow.getLinkedStep(issue.status)
ActionDescriptor ad = step.getActions().find {it.name == actionName} as ActionDescriptor
ad?.id
}
List getParameters(Map params) {
[
[
Name:FIELD_CHILDACTION,
Label:"Child action",
Description:"Choose the action to do on the child when the parent is resolved",
Type: "list",
Values: CannedScriptUtils.getAllWorkflowActions(false),
],
[
Label:"Resolution",
Name:FIELD_RESOLUTION_ID,
Type: "list",
Description:"Resolution to use on the child",
Values: CannedScriptUtils.getResolutionOptions(true),
],
]
}
//empty error collection on the validator.
public ErrorCollection doValidate(Map params, boolean forPreview) {
// todo: check this is on a sub-task type
null
}
Map doScript(Map params) {
log.debug ("TestCondition.doScript with params: ${params}");
String actionName = params[FIELD_CHILDACTION] as String
User user = WorkflowUtils.getUser(params)
String resolutionId = params[FIELD_RESOLUTION_ID] as String
if (subTaskManager.subTasksEnabled && !subTasks.empty) {
subTasks.each {
log.debug ("issue.statusObject.name: " + issue.statusObject.name)
//it has special meaning in groovy - "each subTask knows itself as 'it'"
workflowTransitionUtil.setIssue(it);
workflowTransitionUtil.setUsername(currentUser);
Integer actionId = actionName?.replaceAll(/ .*/, "") as Integer
if (WorkflowUtils.hasAction(it, actionId)) {
WorkflowUtils.resolveIssue(it, actionId, user, resolutionId, [:])
}
else {
log.warn("Action name: $actionName not found for this step.")
}
// Add a comment so people have a clue why the child has been closed
CommentManager commentManager = (CommentManager) ComponentManager.getComponentInstanceOfType(CommentManager.class);
String comment = "*Resolving* as a result of the *Resolve* action being applied to the parent.";
commentManager.create(it, currentUser, comment, true);
// validate and transition issue
workflowTransitionUtil.validate();
workflowTransitionUtil.progress();
}
}
return params
}
String getDescription(Map params, boolean forPreview) {
getName()
}
public Boolean isFinalParamsPage(Map params) {
true
}
}
Thanks in advance.