我想从> modifying 或添加 workitems 。他们只能在 Current 天中添加/修改工作源。
在YouTrack Workflows中,我可以检测到更改的花费时间事件,并防止用户从中添加 workitem。但是我想在JavaScript Workflows中修改WorkItem 时获得一个事件。这是我的代码:
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.rule = entities.Issue.onChange({
title: workflow.i18n('Disable editing workitems'),
guard: function(ctx) {
return ctx.issue.fields.isChanged(ctx.ST);
},
action: function(ctx) {
workflow.check(ctx.issue.workItems.added.isEmpty(), workflow.i18n('You can add/modify workitems only in current day.'));
},
requirements: {
ST: {
type: entities.Field.periodType,
name: 'Spent Time'
}
}
});
省略日期条件...
不确定它是否仍然相关,但我有完整的示例:
/**
* This is a template for an on-change rule. This rule defines what
* happens when a change is applied to an issue.
*
* For details, read the Quick Start Guide:
* https://www.jetbrains.com/help/youtrack/server/2022.2/Quick-Start-Guide-Workflows-JS.html
*/
const entities = require('@jetbrains/youtrack-scripting-api/entities');
const workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.rule = entities.Issue.onChange({
title: 'Prevent-workitem-updates-past',
guard: (ctx) => {
const issue = ctx.issue;
const fs = issue.fields;
// NOT using issue.workItems.added.isEmpty() , because we also want to detect EDITS as well as ADDS to the WorkItems array.
return fs.isChanged(ctx.ST) && !issue.becomesReported;
},
action: (ctx) => {
const issue = ctx.issue;
function checkWorkItemDate(currentWorkItem, currentEpochTimeStamp){
workflow.check(currentWorkItem.date == currentEpochTimeStamp, workflow.i18n('You can add/modify workitems only in current day.'));
}
//const splittedDate = new Date().toISOString().slice(0, 10).split('-');
//const currentEpoch = new Date(splittedDate[0], splittedDate[1] - 1, splittedDate[2]).valueOf();
const currentDate = new Date();
const currentEpoch = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()).valueOf();
// Check newly added elements
issue.workItems.added.forEach(workItem => {
checkWorkItemDate(workItem, currentEpoch);
});
// Check edited elements
issue.editedWorkItems.forEach(workItem => {
checkWorkItemDate(workItem, currentEpoch);
});
workflow.check(true);
},
requirements: {
ST: {
type: entities.Field.periodType,
name: 'Spent Time'
}
}
});
检查ctx.issue.workitems.ischanged,还检查ctx.issue.editedworkitems set。