Custom search for issues by time for the workflow script
Hello,
I am thinking about migrating from Redmine to Youtrack, so i am testing all the functionality now.
Unfortunately, i was unable to do complete a searching query for issues.
I need to find all issues created X hours ago, so i've tried simple JS code like this:
>var now = new Date();
>var diff = -14*60;
>var PrevDateObj = new Date(now.getTime() + diff*60000);
But
search: 'created: {PrevDateObj} .. {now}' doesn't seems to be working. The
created: today
works perfectly, and all the workflow is working, but today is not what i am looking for.
The idea is simple - to create a notification about all new issues created and commented in the last X hours for the team-leader.
Any comments and highly appreciated.
Thank you.
Please sign in to leave a comment.
Hello,
If you need to specify a time, the format should be the following: yyyy-MM-ddThh:mm:ss. For example:
As for the notification digest, I suggest you create a separate anchor issue to set it in the search property of the onChange rule and iterate through required issues using search functionality and split all information to a single email. Please refer to the Make It Workflow — Part 11: Updating Batches of Issues blog post that illustrates the same idea.
I hope it will be helpful. Should you have any further questions, feel free to contact us.
Maybe there are some guidebooks how to correctly write a workflow?
Or you should be a pro in JS to correctly do it?
For example, i've tried to modify the basic overdue report so it could do something at least with the default query: Today or Yesterday like this:
exports.rule = entities.Issue.onSchedule({
title: 'Daily report',
search: 'created: Today or created: Yesterday or updated: Today or updated: Yesterday',
cron: '0 0 7 * * ?',
muteUpdateNotifications: false,
guard: function(ctx) {
// TODO: specify the conditions for executing the rule
return true;
},
action: function(ctx) {
var issue = ctx.issue;
var userToNotify = entities.User.findByLogin('Sbdd');
var notificationText = 'Dear Sbdd,<br>' + workflow.i18n('These issues were created: {0}', issue.title) +
' <a href="' +issue.url + '">' + issue.summary +'</a><p style="color: gray;font-size: 12px;margin-top: 1em;border-top: 1px solid #D4D5D6">' +
workflow.i18n('Sincerely yours, YouTrack') + '</p>';
userToNotify.notify(workflow.i18n('[YouTrack, Issues from night and yesterday]'), notificationText);
},
But this send me a separate email for each issue found, could i use some FOR cycle to add all issues to the one email and how?
There is no issue.size() or something like it?
I am sorry for such newbie questions but i really searched through all manuals, and RTFM didn't helped :(
Hello, thanks again for your help&links, here is what i've tried (also some copy-paste from your different tutorials):
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');
var dateTime = require('@jetbrains/youtrack-scripting-api/date-time');
var search = require('@jetbrains/youtrack-scripting-api/search');
//exports.DAY_IN_MS = 24 * 60 * 60 * 1000;
//var now = new Date();
//var diff = -14*60;
//var PrevDateObj = new Date(now.getTime() + diff*60000);
exports.rule = entities.Issue.onSchedule({
// TODO: give the rule a human-readable title
title: 'Daily report',
// TODO: define which issues are processed
search: '#TTMT-1',
// TODO: set the schedule
// For format details refer to http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html
cron: '0 0 7 * * ?',
// TODO: to mute notifications for changes that are applied by this rule, set to true
muteUpdateNotifications: false,
guard: function(ctx) {
// TODO: specify the conditions for executing the rule
return true;
},
action: function(ctx) {
var searchQuery='created: Today or created: Yesterday or updated: Today or updated: Yesterday';
var issues = search.search(ctx.issue.project, searchQuery);
var entries = issues.entries();
var i = entries.next();
var n = 0;
var firstIssueId = i.done ? '' : i.value.id;
var issuelist = [];
var linklist = [];
var summarylist = [];
var notificationText = 'Dear SBDD,<br>';
while (n < 1000 && !i.done) {
var issue = i.value;
notificationText = notificationText + workflow.i18n('This issue was created or updated recently: {0}', issue.fields[ctx.issue.title]) +
' <a href="' +issue.fields[ctx.issue.url] + '">' + issue.fields[ctx.issue.summary] +'</a><br>';
//issuelist.push(issue.fields[ctx.issue.title]);
//linklist.push(issue.fields[ctx.issue.url]);
//summarylist.push(issue.fields[ctx.issue.summary]);
n += 1;
i = entries.next();
}
var userToNotify = entities.User.findByLogin('SBDD');
userToNotify.notify(workflow.i18n('[YouTrack, Issues from night and yesterday]'), notificationText);
},
requirements: {
DueDate: {
type: entities.Field.dateType,
name: "Due Date"
}
}
});
The good news is that in the email there is exact number of strings (issues, that should be there). The bad news is that no links/names - they did't get the value - they got issue.fields[ctx.issue.url], but not its value.
Could you please have a look at it?
Hello,
Use the ctx.issue.url property instead of issue.fields[ctx.issue.url]:
I hope this helps.
Hello, Oleg.
Thank you for the quick reply. I've tried, but this points to an anchor-issue:
>search: '#TTMT-1'
so all resulting strings have link to this anchor issue, and not that issues that were searched by searchQuery. Also :
>while (n < 1000 && !i.done) {
>var issue = i.value;
So for iterating there should be a issue.something, so the variable is actually changes for each iteration.
Many thanks for your help and support&Best regards,
Dmitry
Thank you for the clarification. The main idea from the suggested example is how to use the anchor issue. It is not necessary to limit issues count. Instead, you can iterate through the issue using forEach method after the search, for example:
Hello, Oleg.
Thank you for your help, and sorry for my late reply. I was able to do it as you suggest, but now it looks like i need to create a separate workflow for each project, because i am still searching in the exact project:
And using somehow project.this - could really improve this - and one workflow could be used in all projects correctly.
Another option is to make this issue.project a variable, and somehow set for each project, so you still have only 1 workflow. But i have no idea if it is possible, for now only separate workflows working.
Is there a way of using a variable instead of:
as we discussed before?
Many thanks for your help and support!
Hello!
As a possible solution, you can create a separate array with the required projects and get the project with the findByName or findByKey method. As for the time variable, since search is a string, you can also replace this with the required variable.
I hope it will be helpful.