Slack notification when a task is added/moved to a specific kanban column

Hello, I want to receive notification in Slack when a task is added/moved to a specific kanban column, I have tried to use workflow constructor but constructor is not suitable for that, if i am understanding correctly, using javascript is the only way, are there any code examples of working with specific kanban column and slack notifications? 

9 comments
Comment actions Permalink

Hi! 

You can build a custom Slack integration to accommodate your case. A column change is just a state value change, so you can create a workflow (a custom script) that would send a request to Slack when an issue's value is changed. You can see a detailed guide on building a custom Slack integration here: https://www.jetbrains.com/help/youtrack/devportal/Slack-Integration-HTML.html

Also, if you are not familiar with the workflow functionality, make sure to check our tutorial.

1
Comment actions Permalink

Hi, i have tried to add a new line of code to slack js workflow, i've added this line: (i found it on this forum)

issue.fields.oldValue(ctx.State).name = 'column name'

so, now my code look like this 

exports.rule = entities.Issue.onChange({
title: 'Send notification to slack when an issue is reported, resolved, or reopened',
guard: (ctx) => {
return ctx.issue.becomesReported || ctx.issue.becomesResolved || ctx.issue.becomesUnresolved;
},
action: (ctx) => {
const issue = ctx.issue;

const issueLink = '<' + issue.url + "|" + issue.id + '>';
let message, isNew;

if (issue.becomesReported) {
message = "Created: ";
isNew = true; issue.fields.oldValue(ctx.State).name = 'Doing';
} else if (issue.becomesResolved) {
message = "Resolved: ";
isNew = false;
} else if (issue.becomesUnresolved) {
message = "Reopened: ";
isNew = false;
}
message += issue.summary;

However, now the whole kanban project doesnt work, what i am doing wrong?

0
Comment actions Permalink

Thanks for your reply. 

now the whole kanban project doesnt work

Do you mean you get a workflow error when you try to change something in the project's issues? If so, check the workflow's console that will show a related error. Feel free to share the error if you don't know what it means. 

Generally, if the workflow itself doesn't work and you don't know why, then you can add console.log() for related variables to debug your code. If you use an IDE, then you can use advanced debugging tools via remote debugging.

0
Comment actions Permalink

Hi, i have checked console and found this error:

Processing issue MANAGER-47:
TypeError: issue.fields.Value is not a function
this error happened when i added a new line of code:
issue.fields.oldValue(ctx.State).name = 'Doing';
I am not sure if this line of code is the right one in my case (i just want notification when an issue is created/moved to a specific column)
 
0
Comment actions Permalink

The error should also point you to the exact line of the code that triggers this error. Please check it. 

when an issue is created/moved to a specific column

In the tutorial I shared, there's an example of a rule when a specific field value changes (the value is the same thing as the column in your case), so you can use it as a base: https://www.jetbrains.com/help/youtrack/devportal/Quick-Start-Guide-Workflows-JS.html#write-first-rule-quick-start

In your current rule that you sent, the column (value) is not checked at all. 

0
Comment actions Permalink

Hi, I have used your example of a rule, and now my column value is being checked, however now i get notification even if i drag a card to any column (i want notifications only when a card is dragged to the column ''Doing')

 

exports.rule = entities.Issue.onChange({
title: 'Send notification to slack when an issue is reported, resolved, or reopened',
guard: (ctx) => {
return ctx.issue.fields.isChanged(ctx.State, ctx.State.Doing);
},
action: (ctx) => {
const issueFields = ctx.issue.fields;
const issue = ctx.issue;

const issueLink = '<' + issue.url + "|" + issue.id + '>';
let message, isNew;

if (issueFields.becomes(ctx.State, ctx.State.Doing)) {
message = "Created: ";
isNew = true;
}
message += issue.summary;

let changedByTitle = '',
changedByName = '';

if (isNew) {
changedByTitle = "Created By";
changedByName = issue.reporter.fullName;
} else {
changedByTitle = "Updated By";
changedByName = issue.updatedBy.fullName;
}

const payload = {
"attachments": [{
"fallback": message + " (" + issueLink + ")",
"pretext": message + " (" + issueLink + ")",
"color": issue.fields.Priority.backgroundColor || "#edb431",
"fields": [{
"title": "State",
"value": issue.fields.State.name,
"short": true
},
{
"title": "Priority",
"value": issue.fields.Priority.name,
"short": true
},
{
"title": "Assignee",
"value": issue.fields.Assignee ? issue.fields.Assignee.fullName : '',
"short": true
},
{
"title": changedByTitle,
"value": changedByName,
"short": true
}
]
}]
};

const connection = new http.Connection(SLACK_WEBHOOK_URL, null, 2000);
const response = connection.postSync('', null, JSON.stringify(payload));
if (!response.isSuccess) {
console.warn('Failed to post notification to Slack. Details: ' + response.toString());
}
},
requirements: {
Priority: {
type: entities.EnumField.fieldType
},
State: {
type: entities.State.fieldType,
Doing: {}
},
Assignee: {
type: entities.User.fieldType
}
}
});

0
Comment actions Permalink

Hello,

Thank you for sharing your code.

I inspected it and saw that you use the isChanged() method in the `guard` section. This method accepts one parameter and checks if the field is changed in general. If you wish to check the new value of this field, I recommend you use the becomes() method. According to it, the `guard` section will look as follows:

 


guard: (ctx) => {
   return ctx.issue.fields.becomes(ctx.State, ctx.State.Doing);
},

 

0
Comment actions Permalink

It worked, thanks a lot for the help!

0

Please sign in to leave a comment.