Getting comment ID in workflow

Hello, i'm currently working on integrating our company case management system with youtrack, and so i need to update comment content in our system when youtrack comment changes. In order to do this, i store youtrack comment id in external system by sending http request inside workflow.

However, i have problem with getting youtrack comment id from workflow code. The solution i found is extracting it from comment.url property.

It seem to work fine, but i have another workflow, which is triggered upon comment edit (guard condition is ctx.issue.editedComments.isNotEmpty() ).

And that workflow is triggered upon posting comment, yet it's pointed out in documentation, that editedComments is empty on new comment - https://www.jetbrains.com/help/youtrack/standalone/2017.4/v1-Issue.html

Turned out that accessing comment.url inside add comment workflow also triggers edit comment workflow.

The add comment workflow is:

ctx.issue.comments.added.forEach(function(comment) {
    console.log(comment.url);
});

So i need help -

whether to get comment id in add workflow

or to avoid triggering edit workflow

3 comments
Comment actions Permalink

Hello, could you please share the full code of both rules in question? Thank you!

0
Comment actions Permalink

On add:

This code can actually be reduced to only accessing comment.url property ( for example console.log(comment.url) ) inside forEach

/**
* 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/standalone/2017.3/Quick-Start-Guide-Workflows-JS.html
*/

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var http = require('@jetbrains/youtrack-scripting-api/http');
var util = require('./get_comment_id');

exports.rule = entities.Issue.onChange({
// TODO: give the rule a human-readable title
title: 'comment_to_acm',
guard: function(ctx) {
if (ctx.currentUser.login=='admin') {
return false;
}

return ctx.issue.fields["АСМ"] && ctx.issue.comments.added.isNotEmpty();
},
action: function(ctx) {
var issue = ctx.issue;
var connection = new http.Connection('http://10.12.1.189');
ctx.issue.comments.added.forEach(function(comment) {
var commentId = util.getCommentId(comment);
if (!commentId) {
return;
}


//connection.postSync('/wp-admin/admin-ajax.php', {
//action: 'ytrack_post_comment',
//id: issue.id,
//comment_id: commentId,
//content: encodeURIComponent(comment.text),
//user: ctx.currentUser.login
//});
});

// TODO: specify what to do when a change is applied to an issue
},
requirements: {
"АСМ": {
type: entities.Field.integerType
}
}
});

On edit:

There you can add console.log( ctx.issue.editedComments.isNotEmpty() ) inside guard

/**
* 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/standalone/2017.3/Quick-Start-Guide-Workflows-JS.html
*/

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var http = require('@jetbrains/youtrack-scripting-api/http');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');
var util = require('./get_comment_id');

exports.rule = entities.Issue.onChange({
// TODO: give the rule a human-readable title
title: 'comment_update_to_acm',
guard: function(ctx) {
if (ctx.currentUser.login=='admin') {
return false;
}
return ctx.issue.fields["АСМ"] && ctx.issue.editedComments.isNotEmpty();
},
action: function(ctx) {
var issue = ctx.issue;

var connection = new http.Connection('http://10.12.1.189');
ctx.issue.editedComments.forEach(function(comment) {
if (comment.author.login=='admin') {
return;
}
var commentId = util.getCommentId(comment);
if (!commentId) {
return;
}
//connection.postSync('/wp-admin/admin-ajax.php', {
//action: 'ytrack_update_comment',
//id: issue.id,
//comment_id: commentId,
//content: encodeURIComponent(comment.text),
//user: ctx.currentUser.login
//});
});
// TODO: specify what to do when a change is applied to an issue
},
requirements: {
"АСМ": {
type: entities.Field.integerType
}
}
});

get_comment_id file:

exports.getCommentId = function(comment) {
var regex = /#comment=([\w-]+)$/;
var result = comment.url.match(regex);
if (result) {
return result[1];
}

return null;
};
0
Comment actions Permalink

Which YouTrack build number are you using? Are there any particular details about how and where in the UI you're adding comments? Unfortunately, at the moment we can't reproduce this issue ourselves.

0

Please sign in to leave a comment.