-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
111 lines (96 loc) · 4.32 KB
/
proxy.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
let http = require('http');
let https = require('https');
let fetch = require('node-fetch');
let config = require('./config.json');
http.createServer((req, res) => {
if(req.url != config.whitelist.path && config.whitelist.enabled) {
res.statusCode = 403;
res.statusMessage = "Forbidden";
return res.end();
}
req.on('data', (d) => {
let string = "";
string += d;
console.log(string);
let packet;
try {
packet = JSON.parse(d)
} catch(ex) {
console.error("JSON PARSING FAILED.");
return;
}
let embed = {
title: "",
color: 0xFFFFFF,
description: "",
fields: [],
footer: {
text: ""
}
};
if(!packet.domain == "build")
embed.fields.push({ name: "Repository", value: packet.data.repo_key, inline: true }, { name: "Path", value: packet.data.repo_key, inline: true });
if(packet.domain != "build")
embed.color = 0x41BF47;
switch(packet.domain) {
case 'artifact': // Artifact webhook.
embed.title = `Artifact has been ${packet.event_type}`;
let source_repo_path;
let target_repo_path;
if(packet.event_type == "moved" || packet.event_type == "copied") {
source_repo_path = packet.data.source_repo_path;
target_repo_path = packet.data.target_repo_path;
} else {
source_repo_path = "`None`";
target_repo_path = "`None`";
}
embed.description = `Artifact with name ${packet.data.name} \<${packet.data.size} bytes\> has been ${packet.event_type}.`;
embed.fields.push({ name: "Source Repository Path", value: source_repo_path});
embed.fields.push({ name: "Target Repository Path", value: target_repo_path, inline: true });
break;
case 'artifact_property':
embed.title = `Artifact Property has been ${packet.event_type}`;
embed.description = `Artifact's Properties were ${packet.event_type} to ${packet.data.name} \<${packet.data.size} bytes\>.`;
packet.data.property_values.forEach((el, i) => {
embed.fields.push({ name: `Property ${i}`, value: el });
});
break;
case 'docker':
embed.color = 0x2496ED;
embed.title = `Docker Tag has been ${packet.event_type}`;
embed.description = `A Tag has been ${packet.event_type} to Docker File ${packet.data.name}'s \<${packet.data.size} bytes\> [Image: ${packet.data.image_name}].`;
embed.fields.push({ name: 'Tag', value: packet.data.tag, inline: true });
packet.data.platforms.forEach((el, i) => {
embed.fields.push({ name: `Platform ${i}: ${el.architecture}`, value: el.os});
});
break;
case 'build':
embed.title = `Build has been ${packet.event_type}`;
embed.description = `Build with name of ${packet.data.build_name} has been ${packet.event_type} in ${packet.data.build_repo} repository.`;
embed.fields.push({ name: `Build Number`, value: packet.data.build_number, inline: true }, { name: `Build Start Time`, value: packet.data.build_started, inline: true });
break;
}
embed.footer.text = `Origin: ${packet.jpd_origin}`;
console.log('Webhook request inbound!');
config.webhooks.forEach(el => {
const discordpacket = JSON.stringify({
username: el.name,
//avatar_url: "https://somedevfox.jfrog.io/ui/img/jfrog.8f770bff.svg",
embeds: [embed]
});
console.log(discordpacket)
fetch(el.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: discordpacket
}).then(res => res.text())
.then(data => {
console.log(`Response from Discord: ${data}`);
})
})
res.statusCode = 200;
res.end();
})
}).listen(config.port);