forked from TomDotBat/youtrack-discord-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
159 lines (131 loc) · 5.33 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const entities = require("@jetbrains/youtrack-scripting-api/entities");
const CONFIG = require("./config");
const { Payload } = require("./payload");
const { Embed, Body, Author, Field, Footer } = require("./embed");
function send_embed(issue, payload, embed, body, ctx) {
body.description = issue.description;
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);
payload.addEmbed(embed);
payload.send(CONFIG.WEBHOOK_URL);
return;
}
const EVENTS = [
{
title: "Stage Changed",
newDescription: "Stage set to $newValue.",
changeDescription: "Stage changed from $oldValue to $newValue.",
issueKey: "Stage",
nameKey: "name"
},
{
title: "Assignee Changed",
newDescription: "Assignee set to $newValue.",
changeDescription: "Assignee changed from $oldValue to $newValue.",
issueKey: "Assignee",
nameKey: "visibleName"
},
{
title: "Comment Added",
newDescription: "$newValue",
customCheck: function (issue) {
return issue.comments.isChanged;
},
valueGetter: function (issue) {
if (issue.comments.added.size < 1) return "lmao i can't code";
return issue.comments.added.get(0).text;
}
},
{
title: "Priority Changed",
newDescription: "The issue priority was set to $newValue.",
changeDescription: "The issue priority was changed from $oldValue to $newValue.",
issueKey: "Priority",
nameKey: "name"
}
];
exports.rule = entities.Issue.onChange({
title: "Discord Webhook",
guard: (ctx) => {
return ctx.issue.isReported;
},
action: (ctx) => {
const issue = ctx.issue;
if (issue.becomesReported) {
const payload = new Payload(null, CONFIG.SENDER_NAME, CONFIG.AVATAR_URL);
const embed = new Embed();
const body = new Body();
body.title = "Issue " + issue.id + " が作成されました";
embed.fields = [{ 'name': '作成日', 'value': issue.created, 'inline': true }, { 'name': '作者', 'value': issue.reporter, 'inline': true }];
body.color = CONFIG.COLOR_NEGATIVE;
send_embed(issue, payload, embed, body, ctx);
return;
}
else if (issue.becomesResolved) {
const payload = new Payload(null, CONFIG.SENDER_NAME, CONFIG.AVATAR_URL);
const embed = new Embed();
const body = new Body();
body.title = "Issue " + issue.id + " を解決しました";
body.color = CONFIG.COLOR_POSITIVE;
send_embed(issue, payload, embed, body, ctx);
return;
}
else if (issue.becomesUnresolved) {
const payload = new Payload(null, CONFIG.SENDER_NAME, CONFIG.AVATAR_URL);
const embed = new Embed();
const body = new Body();
body.title = "Issue " + issue.id + " を再開しました";
body.color = CONFIG.COLOR_WARNING;
send_embed(issue, payload, embed, body, ctx);
}
let changes = [];
for (let i = 0; i < EVENTS.length; i++) {
const event = EVENTS[i];
const issueKey = event.issueKey;
if (!((issueKey && issue.fields[issueKey] && issue.isChanged(issueKey)) || (event.customCheck && event.customCheck(issue)))) continue;
let oldValue;
let newValue;
if (issueKey) {
oldValue = issue.oldValue(issueKey);
newValue = issue.fields[issueKey];
}
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)
});
}
const changeCount = changes.length;
if (changeCount < 1) return;
const payload = new Payload(null, CONFIG.SENDER_NAME, CONFIG.AVATAR_URL);
const embed = new Embed();
const body = new Body();
if (changeCount === 1) {
const change = changes[0];
body.title = change.title + " In " + issue.id;
body.description = change.description;
}
else {
body.title = changeCount + " New Changes To " + issue.id;
for (let i = 0; i < changes.length; i++) embed.addField(new Field(changes[i].title, changes[i].description, false));
}
body.url = issue.url;
body.color = CONFIG.COLOR_REGULAR;
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);
payload.addEmbed(embed);
payload.send(CONFIG.WEBHOOK_URL);
}
});