-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_shared.js
More file actions
102 lines (89 loc) · 2.64 KB
/
Copy path_shared.js
File metadata and controls
102 lines (89 loc) · 2.64 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
const http = require("@jetbrains/youtrack-scripting-api/http");
const DISCORD_WEBHOOK_URL = "";
function sendDiscordPayload(payloadString) {
const connection = new http.Connection(DISCORD_WEBHOOK_URL, null, 2000);
connection.addHeader("Content-Type", "application/json");
const response = connection.postSync("", null, payloadString);
if (!response.isSuccess) {
console.warn("Failed to post notification to Discord. Details: " + response.toString());
}
}
function createPayloadCommented(comment) {
return {
embeds: [
{
type: "rich",
title: `${comment.author.fullName} commented on ${comment.issue.id} ${comment.issue.summary}`,
description: comment.text,
color: "16466332",
footer: {
text: comment.author.fullName,
icon_url: comment.author.avatarUrl
},
url: comment.url
}
]
};
}
function createPayloadCreated(issue) {
let msg = `:new: New Issue opened by ${issue.updatedBy.fullName}`;
if(issue.fields.Assignee) {
const discordId = getDiscordUserId(issue.fields.Assignee);
const ping = _createUserPing(discordId, issue.fields.Assignee.fullName);
msg +=`, assigned to ${ping}`;
}
return {
embeds: [_createIssueEmbed(issue)],
content: msg
};
}
function createPayloadResolved(issue) {
return {
embeds: [_createIssueEmbed(issue)],
content: `:white_check_mark: Resolved by ${issue.updatedBy.fullName} :tada:`
};
}
function createPayloadReopened(issue) {
return {
embeds: [_createIssueEmbed(issue)],
content: `:repeat: Reopened by ${issue.updatedBy.fullName}`
};
}
function createPayloadReassigned(issue, assignee, discordId) {
const ping = _createUserPing(discordId, assignee.fullName);
return {
embeds: [_createIssueEmbed(issue)],
content: `Reassigned to ${ping}`,
};
}
function _createIssueEmbed(issue) {
return {
type: "rich",
title: `${issue.id} ${issue.summary}`,
description: issue.summary,
color: "16466332",
url: issue.url,
footer: {
text: `Assignee: ${issue.fields.Assignee.fullName}`,
icon_url: issue.fields.Assignee.avatarUrl
},
}
}
function _createUserPing(discordUserId, discordUserName) {
return discordUserId ? `<@${discordUserId}>` : `${discordUserName}`;
}
function getDiscordUserId(user) {
return user.attributes["Discord ID"];
}
function _createSilentPing() {
return { allowed_mentions: {parse: []} };
}
module.exports = {
sendDiscordPayload,
createPayloadCommented,
createPayloadCreated,
createPayloadResolved,
createPayloadReopened,
createPayloadReassigned,
getDiscordUserId
}