Subtasks
This workflow automatically sets the state of a parent task when the state changes in issues that are linked as subtasks.
Use Case
You want to automatically update the state of a parent task when changes are applied to subtasks.
When any subtask is set to an unresolved state, the state of the parent task changes to Open.
When all subtasks are set to a resolved state, the state of the parent task changes to Fixed.
Modules
This workflow includes two modules.
Open parent task when subtask changes to an unresolved state
When the state of an issue is set to an unresolved state, this rule checks whether the issue is linked as a subtask of a parent task. If the parent task belongs to an active project and has a resolved state, the state of the parent task is changed to Open.
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.rule = entities.Issue.onChange({
title: workflow.i18n('Open parent task when subtask changes to an unresolved state'),
guard: function(ctx) {
return ctx.issue.isReported && ctx.issue.becomesUnresolved;
},
action: function (ctx) {
var processParent = function(issue) {
if (issue.links['subtask of'].isEmpty()) {
return;
}
var parent = issue.links['subtask of'].first();
if (parent && parent.project && !parent.project.isArchived && parent.isReported && parent.isResolved) {
var field = parent.project.findFieldByName(ctx.State.name);
if (field) {
var value = field.findValueByName(ctx.State.Open.name);
if (value) {
parent.State = value;
workflow.message(workflow.i18n('Automatically reopen {0}', parent.id));
return parent;
}
}
}
};
var issue = ctx.issue;
while (issue) {
issue = processParent(issue);
}
},
requirements: {
State: {
type: entities.State.fieldType,
Open: {}
}
}
});
Fix parent task when all subtasks are resolved
When the state of an issue is set to a resolved state, this rule checks whether the issue is linked as a subtask of a parent task. If the parent task is unresolved, the rule checks the current state for all other issues that are linked to the parent task as subtasks. If all other subtasks are resolved, the state of the parent task is set to Fixed.
The rule then checks the parent task to see if it is linked as a subtask to a parent task and performs the same operation.
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.rule = entities.Issue.onChange({
title: workflow.i18n('Fix parent when all subtasks are resolved'),
guard: function(ctx) {
return ctx.issue.isReported && ctx.issue.becomesResolved;
},
action: function(ctx) {
var processParent = function(issue) {
if (issue.links['subtask of'].isEmpty()) {
return;
}
var parent = issue.links['subtask of'].first();
if (parent && parent.project && !parent.project.isArchived &&
parent.isReported && !parent.isResolved) {
var unresolvedSubtask = parent.links['parent for'].find(function(subtask) {
return subtask.isReported && !subtask.fields.State.isResolved;
});
if (!unresolvedSubtask) {
var field = parent.project.findFieldByName(ctx.State.name);
if (field) {
var value = field.findValueByName(ctx.State.Fixed.name);
if (value) {
parent.State = value;
workflow.message(workflow.i18n('Automatically set {0} as Done', parent.id));
return parent;
}
}
}
}
};
var issue = ctx.issue;
while (issue) {
issue = processParent(issue);
}
},
requirements: {
State: {
type: entities.State.fieldType,
Fixed: {}
}
}
});
Last modified: 30 March 2021