-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify-when-mention-in-comment.js
108 lines (103 loc) · 3.25 KB
/
notify-when-mention-in-comment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* This notify via Slack when user was mention in comment. Auto match user email from YT to Slack user id
*/
const entities = require('@jetbrains/youtrack-scripting-api/entities');
const http = require('@jetbrains/youtrack-scripting-api/http');
const SLACK_BOT_TOKEN = 'PUT_HERE_BOT_TOKEN';
const SLACK_API = 'https://slack.com/api/';
exports.rule = entities.Issue.onChange({
title: 'Send notification to slack when an issue is reported Assignee change',
guard: (ctx) => {
return ctx.issue.comments.added.isNotEmpty();
},
action: (ctx) => {
const issue = ctx.issue;
var issueLink = '<' + issue.url + "|" + issue.id + '>';
var blocks = function (author, comment) {
return [{
"type": "header",
"text": {
"type": "plain_text",
"text": ":speech_balloon: " + author + " mention You",
"emoji": true
}
}, {
"type": "section",
"text": {
"type": "mrkdwn",
"text": issue.summary + " [" + issueLink + "]"
}
},
{
"type": "context",
"elements": [{
"type": "mrkdwn",
"text": "> "+comment
}]
},
{
"type": "section",
"fields": [{
"type": "mrkdwn",
"text": "*State:*\n" + issue.fields.State.name
},
{
"type": "mrkdwn",
"text": "*Priority:*\n" + issue.fields.Priority.name
},
{
"type": "mrkdwn",
"text": "*Assignee:*\n" + (issue.fields.Assignee ? issue.fields.Assignee.fullName : '')
},
{
"type": "mrkdwn",
"text": "*Updated by:*\n" + author
}
]
}
];
};
issue.comments.added.forEach(function(comment) {
//var commentLink = '<' + comment.url + "|" + issue.summary + '>';
var author = comment.author.fullName;
var commentText = comment.text;
var regexp = /(^|\s)@([A-ź]+)/g;
const result = commentText.matchAll(regexp);
for (const match of result) {
var user = entities.User.findByLogin(match[2]);
if (user) {
var connection = new http.Connection(SLACK_API, null, 2000);
var userResponse = connection.postSync('users.lookupByEmail', [{
name: 'token',
value: SLACK_BOT_TOKEN
}, {
name: 'email',
value: user.email
}]);
if (userResponse.isSuccess) {
userResponse = JSON.parse(userResponse.response);
if (userResponse.ok) {
connection.addHeader({
name: 'Content-Type',
value: 'application/json'
});
connection.addHeader({
name: 'Authorization',
value: 'Bearer ' + SLACK_BOT_TOKEN
});
var messageResponse = connection.postSync('chat.postMessage', '', JSON.stringify({
'channel': userResponse.user.id,
'as_user': 'true',
'blocks': blocks(author, commentText),
'text': ":speech_balloon: " + author + " mention You in YouTrack",
'pretty': '1'
}));
}
}
}
}
});
},
requirements: {
}
});