forked from TomDotBat/youtrack-discord-webhook
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
120 lines (97 loc) · 4.13 KB
/
main.js
File metadata and controls
120 lines (97 loc) · 4.13 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
const entities = require("@jetbrains/youtrack-scripting-api/entities");
const CONFIG = require("./config");
const COLORS = require("./config_colors");
const {Payload} = require("./payload");
const {EVENTS} = require("./config_events");
const {USERS_CONFIG, USERS} = require("./config_users");
const {
Embed,
Body,
Author,
Field,
Footer
} = require("./embed");
exports.rule = entities.Issue.onChange({
title: "Discord Webhook",
guard: (ctx) => {
return ctx.issue.isReported;
},
action: (ctx) => {
const issue = ctx.issue;
let changes = [];
for (let i = 0; i < EVENTS.length; i++) {
const event = EVENTS[i];
const issueKey = event.issueKey;
let oldValue;
let newValue;
if (issueKey) {
// IssueKey does not exist, was not changed or does not pass custom check
if (!(issue.fields[issueKey] && issue.isChanged(issueKey) || (event.customCheck && event.customCheck(issue)))) continue;
oldValue = issue.oldValue(issueKey);
newValue = issue.fields[issueKey];
} else {
// IssueKey does not exist and does not pass custom check
if (!(event.customCheck && event.customCheck(issue))) continue;
}
if (event.valueGetter) newValue = event.valueGetter(issue);
if (event.nameKey) {
if (oldValue) oldValue = oldValue[event.nameKey];
newValue = newValue[event.nameKey];
}
let description = oldValue ? event.changeDescription : event.newDescription;
changes.push({
title: event.title,
description: description.replace("$oldValue", oldValue).replace("$newValue", newValue),
thumbnail: event.thumbnail,
color: event.color
});
}
const changeCount = changes.length;
if (changeCount < 1) return;
const payload = new Payload(null, CONFIG.SENDER_NAME);
const embed = new Embed();
const body = new Body();
if (changeCount === 1) {
const change = changes[0];
body.title = change.title + " [" + issue.id + "]\n" + issue.summary;
body.description = change.description;
body.color = change.color ? change.color : COLORS.DEFAULT;
embed.thumbnailUrl = change.thumbnail;
} else {
body.title = changeCount + " New Changes To " + issue.id;
body.color = COLORS.DEFAULT;
for (let i = 0; i < changes.length; i++) embed.addField(new Field(changes[i].title, changes[i].description, false));
}
body.url = issue.url;
body.setDateToNow();
embed.body = body;
const user = ctx.currentUser;
embed.author = new Author(user.visibleName, CONFIG.YOUTRACK_URL + "/users/" + user.login);
embed.footer = new Footer(CONFIG.SITE_NAME + " " + issue.project.name);
let webhooks = [];
if(CONFIG.GENERAL_WEBHOOK_URL) webhooks.push(CONFIG.GENERAL_WEBHOOK_URL);
// Notify all watchers
var mentions = "";
issue.tags.forEach(function (tag) {
if (tag.name == "Star") {
var watcherUsername = tag.owner.login;
for (var i = 0; i < USERS.length; i++) {
var user = USERS[i];
// Only notify a user if they are in the list and not the current user
if (user.youtrackUsername == watcherUsername
&& (USERS_CONFIG.NOTIFY_CURRENT || user.youtrackUsername != issue.updatedBy.login)) {
if(user.discordUserID) mentions += "<@" + user.discordUserID + ">\n";
if(user.webhookUrl) webhooks.push(user.webhookUrl);
break;
}
}
}
});
if(mentions.length > 0)
embed.addField(new Field("Watchers", mentions, false));
payload.addEmbed(embed);
webhooks.forEach(function (url) {
payload.send(url);
});
}
});