-
Notifications
You must be signed in to change notification settings - Fork 8
/
routes.js
153 lines (137 loc) · 4.39 KB
/
routes.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
//
// An example integrate Slack with JFrog Xray
// Author: @greenido
// Date: April 2019
//
//
// This defines three routes that our API is going to use.
//
const fs = require("fs");
const https = require("https");
const dotenv = require("dotenv");
// Loads environment variables from the .env file into process.env
dotenv.config();
//
// Main End points of our app
//
var routes = function(app) {
//
// The home page end-point return basic explanation
//
app.get("/", function(req, res) {
res.send(
"<h1>🐸 An example integrate Slack with JFrog Xray ☀️</h1> <br> <br> \
<p>This projects send messages to Slack when JFrog Xray will send it notifications base on the policy.<br> \
It makes use of Express.js, a minimal and flexible Node.js framework that includes a myriad of HTTP utility methods for quickly <br> creating robust APIs. We also use the Body Parser package, which is Node.js middleware that allows us to process any POST requests we receive.<br> \
<h4>🎯 More explanations can be found in this <a href='https://greenido.wordpress.com/?p=9820' target='_blank'> blog post</a> </h4> \
<h4> <a href='https://github.com/greenido/jfrog-xray-2-slack-example' target='_blank'>🛠 The GitHub Repo </a> </h4> <hr> <h3>🎫 This is what the message will look like:</h3> <img src='https://cdn.glitch.com/18f97c3f-b8ef-44ba-a661-e915b310696d%2FScreen%20Shot%202020-03-28%20at%204.14.10%20PM.png?v=1585437298767' alt='image of notification at slack' /> \
<h4>🎬 You can also see this project: <a href='https://glitch.com/edit/#!/xray-2-slack?path=README.md:23:0'>README.md</a> </h4>"
);
});
//
// The API end-point that get the notifications from Xray and send them as messages to Slack
//
app.post("/xray/api/", function(req, res) {
let payload = req.body;
let totalIssues = payload.issues.length;
// send each component to Slack
let tmpStr =
"🔔 Policy:" +
payload.policy_name +
" \nWatch: " +
payload.watch_name +
" \nCreated: " +
payload.created +
" \nNumber Of Issues: " +
payload.issues.length +
"\n ℹ️ Below is the first issue:";
// let's see what are we going to send to Slack
console.log(tmpStr + " --> sending to Slack ");
// Build a nice msg
const xrayNotification = {
username: "Xray notifier",
text: tmpStr, // text
icon_emoji: ":bangbang:",
attachments: [
{
color: "#eed140",
// You can add more fields as the data from Xray contains more information
fields: [
{
title: "Type",
value: payload.issues[0].type,
short: true
},
{
title: "Severity",
value: payload.issues[0].severity,
short: true
},
{
title: "Created",
value: payload.issues[0].created,
short: true
},
{
title: "Provider",
value: payload.issues[0].provider,
short: true
},
{
title: "Summary",
value: payload.issues[0].summary
}
]
}
]
};
sendSlackMessage(xrayNotification);
// just in case you wish to monitor this API end point
return res.json({ status: "All Good", msg_sent: totalIssues });
});
};
/**
* Send formated messages to Slack
*
* @param messageBody
* @return {Promise}
*/
function sendSlackMessage(messageBody) {
try {
//console.log("=== " + messageBody);
messageBody = JSON.stringify(messageBody);
} catch (e) {
console.log("Got ERR with sending msg to slack ");
console.log(e);
}
// Promisify the https.request
return new Promise((resolve, reject) => {
const requestOptions = {
method: "POST",
header: {
"Content-Type": "application/json"
}
};
// actual request
const req = https.request(
process.env.SLACK_WEBHOOK_URL,
requestOptions,
res => {
let response = "";
res.on("data", d => {
response += d;
});
res.on("end", () => {
resolve(response);
});
}
);
req.on("error", e => {
reject(e);
});
// send our message body (was parsed to JSON beforehand)
req.write(messageBody);
req.end();
});
}
module.exports = routes;