Subtask Inherit Subsystem
This workflow automatically updates the subsystem of a subtask to the subsystem set for a parent task.
Use Case
This workflow helps you manage the subsystem set for tasks and subtasks.
Modules
This workflow includes two modules.
Copy subsystem from parent task when issue is linked as a subtask
This rule checks a subtask when it is linked to a parent task.
If the subtask belongs to a different project from the parent task and the projects use different sets of values for the Subsystem field, the subsystem is set for the parent task only.
If the subtask and parent task belong to the same project, the subsystem assigned to the parent task is set as the subsystem for any unresolved subtasks.
const entities = require('@jetbrains/youtrack-scripting-api/entities');
exports.rule = entities.Issue.onChange({
title: 'Copy subsystem from parent task when issue is linked as a subtask',
guard: (ctx) => {
return ctx.issue.links['parent for'].added.isNotEmpty() && ctx.issue.fields.Subsystem;
},
action: (ctx) => {
const issue = ctx.issue;
const safeSetSubsystem = function (subtask) {
if (subtask.project && !subtask.project.isArchived) {
if (subtask.project.key === issue.project.key || subtask.project.findFieldByName(ctx.Subsystem.name)) {
if (!subtask.fields.Subsystem) {
const value = subtask.project.findFieldByName(ctx.Subsystem.name).findValueByName(issue.fields.Subsystem.name);
if (value) {
subtask.fields.Subsystem = value;
}
}
}
}
};
issue.links['parent for'].added.forEach(safeSetSubsystem);
},
requirements: {
Subsystem: {
type: entities.EnumField.fieldType
},
SubtaskOf: {
type: entities.IssueLinkPrototype,
name: 'Subtask',
outward: 'parent for',
inward: 'subtask of'
}
}
});
Set subsystem for subtasks when subsystem is set in parent task
The next rule checks for subtasks when a subsystem is set for an issue.
If the subtasks are in an archived project or belong to a different project from the parent issue and the projects use different sets of values for the Subsystem field, the subsystem is set for the parent task only.
If the subtasks are in an active project and belong to the same project as the parent task, the subsystem for each unresolved subtask is set to the subsystem set for the parent task.
const entities = require('@jetbrains/youtrack-scripting-api/entities');
exports.rule = entities.Issue.onChange({
title: 'Set subsystem for subtasks when subsystem is set in parent task',
guard: (ctx) => {
return ctx.issue.fields.isChanged(ctx.Subsystem) && ctx.issue.fields.Subsystem;
},
action: (ctx) => {
const issue = ctx.issue;
const safeSetSubsystem = function (subtask) {
if (subtask.project && subtask.isReported && !subtask.project.isArchived) {
if (subtask.project.key === issue.project.key || subtask.project.findFieldByName(ctx.Subsystem.name)) {
if ((issue.fields.oldValue(ctx.Subsystem) || {}).name === (subtask.fields.Subsystem || {}).name) {
const value = subtask.project.findFieldByName(ctx.Subsystem.name).findValueByName(issue.fields.Subsystem.name);
if (value) {
subtask.fields.Subsystem = value;
}
}
}
}
};
issue.links['parent for'].forEach(safeSetSubsystem);
},
requirements: {
Subsystem: {
type: entities.EnumField.fieldType
},
SubtaskOf: {
type: entities.IssueLinkPrototype,
name: 'Subtask',
outward: 'parent for',
inward: 'subtask of'
}
}
});
Last modified: 7 November 2024